I have a PHP mailer function set up to send a mail to a customer that purchases a file. IT works perfect if I use a static emailaddress in the AddAdress method. But if I use a variable it doesn't get sent.
function sendEmailToClient($name, $address, $postcode, $city, $email, $which, $orderid, $downloadlink){
$mail = new PHPMailer;
// $mail->isSMTP();
// $mail->Host = 'localhost';
$mail->setFrom('email#email.com', 'My Store');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Thanks for your order';
$mail->Body = '<body>
<p>Order:</p>
<p>Order ID: '.$orderid.'</p>
<p>Book: '.$which.'<br />
Name: '.$name.'<br />
Address: '.$address.'<br />
Postal code: '.$postcode.'<br />
City: '.$city.'<br/>
Email: '.$email.'</p>
<p>Download: '.$downloadlink.'</p>
<p>Have fun!!</p>
</body>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
I don't understand why the variable doesn't work. I checked the input from the function to see if the email is actually a valid email, and that is the case. But it's not working with the variable.
Anyone know a solution?
Related
I want to send an email using PHPMailer and I'm using Codeigniter
public function check_email(){
$response = array('error' => false);
$email = $this->input->post('email');
$check_email = $this->fp_m->check_email($email);
if($check_email){
$this->load->library('phpmailer_library');
$mail = $this->phpmailer_library->load();
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = "Reset Password";
$mail->Body = "
Hi,<br><br>
In order to reset your password, please click on the link below:<br>
<a href='
http://example.com/resetPassword.php?email=$email
'>http://example.com/resetPassword.php?email=$email</a><br><br>
Kind Regards,<br>
Kokushime
";
if($mail->send()){
$response['error'] = false;
$response['message'] = "The Email Sent. Please Chect Your Inbox";
}else{
$response['error'] = true;
$response['message'] = "There's Something wrong while sending the message". $mail->ErrorInfo;
;
}
}else{
$response['error'] = true;
$response['message'] = 'The email that you entered is not associated with admin account';
}
echo json_encode($response);
}
But it gives me error Could not instantiate mail function.
BTW, I'm not using SMTP because i don't need that..
I hope that you can help me :)
You did not include your PHPMailer config. Since you are not using SMTP do you have this set?
$mail->isSendmail();
Also, assuming you are using CI3 it would probably be easier if you installed PHPMailer with composer and had it autoload.
I just tested this and it works fine using sendmail.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Phpmailer_test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->isSendmail();
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
}
I'm using PHPMailer to send emails from my website but $mail->send() is returning true and mail is not sending. No error is reporting in my error log. I hosted my site in Bigrock. I didn't find any errors in my code.
<?php
if(isset($_POST['submit']))
{
require 'class.smtp.php';
require 'PHPMailerAutoload.php';
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'exmaple#gmail.com');
$fromrec=$_POST['from'];
$from="example#gmail.com";
$subject=$_POST['sf'];
$message=$_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->setFrom($from, 'Rahul');
$mail->addAddress("example1#gmail.com");
$mail->Subject = $subject;
$mail->Body = "From:".$fromrec."".$message;
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else if($mail->send()) {
echo 'Message has been sent.';
echo $mail->ErrorInfo;
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
?>
On this issue, I have consulted Bigrock customer care and I made a chat with them. It's simple in the above code the from address email id must be domain specific and to address would be anything. After changing the email id to domain specific and Host to mail.example.com. My problem has been solved. If anyone got the same problem please try this.
I am trying to send an email with a .PDF attached using PHPMailer Master.
I am receiving no email, seeing no errors and cannot understand what's going on.
Here is my code:
$url = ''. $_SERVER['DOCUMENT_ROOT'] .'/free-quote/';
$mail = new PHPMailer;
$mail->setFrom('removed#email.com', 'Company Name');
$mail->addAddress(''. $email2 .'', ''. $full_name .'');
$mail->Subject = 'Website Quotation ('. $quotation_ref .')';
$mail->addStringAttachment(file_get_contents($url), ''. $quotation_ref .'.pdf');
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
I have also included the required file:
require('PHPMailer-master/class.phpmailer.php');
Ideas?
From the PHPMailer Docs:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
https://github.com/PHPMailer/PHPMailer
Currently I am working on a PHP email script using PHPMailer` library. I am sending a mass mail using BCC for all the email addresses.
I want each email to contain the current recipient's email address in the message body.
Below is my sample code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('noreply#example.com');
$arrMail [] = array('bcc1#example.com','bcc2#example.com');
for($i=0;$i<count( $arrMail);$i++)
{
$mail->addBCC($arrMail[$i]);
$htmlversion = 'Hello '.$arrMail[$i].' !'.
}
// $htmlversion = 'Hello <email_id needed here> !'.
$mail->Body = $htmlversion;
$mail->AltBody = $textVersion;
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Mail sent';
}
Problem: If bcc1#example.com receives the email, its message body should contain their email address. Currently I am getting the first email address in the message body for every recipient.
Note: I dont want to send mail one-by-one using To like mentioned in other pages.
Also is it possible by using some session or database logic?
I am using php 5.5.9.
Your code is reusing the same email address because you didn't put the creation of the mail body in the loop. If you use a loop then you also don't need BCC.
$arrMail [] = array('bcc1#example.com', 'bcc2#example.com');
$total = count($arrMail);
for($i = 0; $i < $total; $i++) {
$email = $arrMail[$i];
$htmlversion = "Hello $email !";
$mail->Body = $htmlversion;
$mail->AltBody = $textVersion;
$mail->AddAddress($email);
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Mail sent';
}
}
I dont want to send mail one-by-one using To like mentioned in other pages.
Unfortunately, BCC by its very nature sends the same email to multiple recipients. If you want to customise each email for each person, you have to send them individual emails.
I am trying to set up PHP Mailer to allow me to send attachments, mainly just images, from a form. I have the following code working except I am not receiving the attachments, they are not being uploaded. Can anybody help me or point me in the right direction as to what I am missing?
PHP
<?php
require 'class.phpmailer.php';
date_default_timezone_set('Europe/London');
$mail = new PHPMailer;
$current=date('l F dS, Y, H:i a');
$mail->From = 'me#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('karina#live.co.uk', 'Karina McG');
$mail->addAddress('me#live.com');
$mail->addReplyTo('noreply#tempur.com');
$mail->addCC('');
$mail->addBCC('');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$name=$_POST['name'];
$subject=$_POST['subject'];
$system=$_POST['system'];
$country=$_POST['country'];
$description=$_POST['description'];
$mail->Subject = "TEMPUR Web Support Request - $system";
$mail->Body = "Dear $name,<br>
<br>
We have received your TEMPUR request form! - $current<br>
<br>
We will be in contact shortly regarding your issue/s.<br>
<br>
Name : $name <br>
Country : $country <br>
Subject : $subject <br>
System : $system <br>
Description : $description";
$mail->AltBody = 'Hehehe';
//$to="$email";//Will change to Digital Users email!
//Send email
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
HTML
Upload File:<input type="file" name="upload" id="upload"><br>
add enctype="multipart/form-data" to the form as attribute if it is not their.
also try $mail->addAttachment($_FILES["upload"]["tmp_name"]);
It will work.