PHPMailer setting for individual emails is sending empty To: fields - php

Using PHPMailer to send individual emails to recipients I'm getting nothing in the To: field when I add $mail->SingleTo = TRUE; to my code.
When I remove $mail->SingleTo = TRUE; I receive emails with an email address in the To: field that is correct.
This is what I get:
reply-to xxxxxx <xxxx#xxxx.com>, No Reply <no-reply#no-reply.com>
to
date Mon, Mar 21, 2011 at 5:07 PM
subject Testing
mailed-by gmail.com
signed-by gmail.com
(where xxxxxxx represents my email address.)
Here's my code:
if(isset($_POST['submit']))
{
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer();
$subject = $_POST['subject'];
$body = $_POST['emailbody'];
$to = $_POST['to'];
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
// After adding this line I'm getting an empty To: field
$mail->SingleTo = TRUE;
$mail->AddAddress("address1#xxxxxx.com", 'xyz abc');
$mail->AddAddress("address2#xxxxxx.com", 'abc xyz');
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
$message= "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$message= "Message sent!";
}
}

You are using SMTP to send email. The phpmailer class is not using the SingleTo parameter when senting using Smtp.
More, if you see the CreateHeader function when the SingleTo == true the recipents are only aded to $this->SingleToArray and not to the header itself so basically it PHPmailer bug.
Looks like the only choice, unless you want to patch phpmailer, is to send email one-by-one without using SingleTo property
the solution will be
function & prepare_mailer($subject, $body) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
$mail->MsgHTML($body);
return $mail;
}
foreach( $_POST['to'] as $to ){
$mail = null;
$mail = & prepare_mailer($_POST['subject'],$_POST['body']);
$mail->AddAddress($to['address'], $to['name']);
$mail->Send();
}

Set up all the other parameters, then in a loop set the recipient, send the email, then use the ClearAllRecipients() function. Like so:
{ // loop start
$mail->AddAddress($person_email, $person_name);
$mail->Send();
$mail->ClearAllRecipients();
} // loop end

The problem is in the SmtpSend function (starting at line 701 in class.phpmailer.php). As you can see there, they don't take the singleTo setting into account, and just add all recipients and send the body of the message once.
So you should add some logic there to test if singleTo is true, and if it is modify the code so it sends these mails separately, prefixing the To: header to the body of the message. If singleTo is false you can call the code as-is (line 713 - 758).
Or alternatively, if you don't want to patch things, then you could use a transport method (ie. sendmail) that does seem to support the singleTo parameter.

$mail->AddAddress()
doesn't like CSV's
so if you have:
$Emails="addr1#host.com,addr2#host.net"; #etc;
do a for loop after a split:
$NewList = preg_split("/,/",$Emails);
foreach ($NewList as $k=>$email){
if ($k==0) $mail->AddAddress($email); # Add main to the "To" list.
else $mail->AddCC($email); # The REST to CC or BCC which ever you prefer.
}
-- BF.

SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.
Since you use both SingleTo an SMTP, as shown in your code, the SingleTo in your case is ignored.
The SMTP protocol is designed in a way that you cannot send one message to several different recipients, each having only its own address in the TO: field. To have each recipient have only its name in the TO:, the whole message have to be transmitted again. This explains why SingleTo is incompatible with SMTP.
According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.

Related

PHPMailer how to set bounce email address

i know there are plenty of guides, but i tried so many of them with no result.
I also tried to reshuffle the parameters so sender goes before replyTo and vice versa, etc, but still.
The idea is to appear to the recpient as it came from something#gmail.com, but on reply (human or robot as bounce email) to always reply to noreply#custom.com
No matter what I do, the bounced email always delives to something#gmail.com instead of noreply#custom.com
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'apps/PHPMailer/src/Exception.php';
require 'apps/PHPMailer/src/PHPMailer.php';
require 'apps/PHPMailer/src/SMTP.php';
try {
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8'; // UTF8 Encoding
$mail->Encoding = 'base64'; // Needs to be set with UTF8
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = "smtp.gmail.com"; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "something#gmail.com"; // SMTP username
$mail->Password = "somePassword"; // SMTP password
$mail->SMTPSecure = "tls";
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Sender
$mail->setFrom('something#gmail.com');
$mail->addReplyTo('noreply#custom.com');
$mail->Sender = 'noreply#custom.com';
// Recipient
$mail->addAddress('fndsgfds#fsscd.com');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "test bouncing";
$mail->Body = "teting";
//$mail->AltBody = strip_tags($row_campaign['text']);
$mail->MessageID = $messageId; // removed from example here, but is stated above
$mail->addCustomHeader('In-Reply-To', $messageId);
$mail->send();
}
catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
die();
}
unset($mail);
Any idea where is the problem?
PHPMailer 6.1.6
The bounce email address is the SMTP MAIL FROM address, also known as the envelope sender. This is often the same as the From address (and PHPMailer uses the from address as the sender by default), but it is entirely normal for it to be different (subject to DMARC config). In PHPMailer you set it by setting the Sender property, as you are doing in this line:
$mail->Sender = 'noreply#custom.com';
When a server receives a message, it takes the envelope sender and adds it to the message in a Return-Path header; this is not something that a sending server should ever do.
So to change the bounce address, change that setting; it is mostly independent of from and reply-to addresses. Note however that gmail may ignore this if you have not configured it as an alias for your gmail username.

How do I delay sending mails with PHPMailer for multiple days?

I'm trying to create a mailing system for our library. People receive an e-mail when they requested a book. However, I want them to receive an e-mail one day before the hand-in date as well.
So I'm not looking for a sleep script code, I'm looking for a code that can maybe store the data on the server until the hand-in date arrived.
I'm using Joomla 3.5.1 for our website, this is the code for the PHPMailer:
jimport('joomla.mail.mail');
try{
$mail = new PHPMailer();
$body = $mailmsgadmin;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "example.host.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "example.host.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "email"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->SetFrom('email', 'First Last');
$mail->AddReplyTo($mailuseremail, $mailusername);
$mail->Subject = $mailsubject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("Users e-mail","Users name");
if(!$mail->Send()) {
JError::raiseWarning( 'SOME_ERROR_CODE', $mail->ErrorInfo);
}
} catch (Exception $e) {JError::raiseWarning( 'SOME_ERROR_CODE', $e->getMessage());}
Instead of trying to get PHPMailer delay the sending, add your email details to a database table. And then write a cron job that checks the table for emails that need to be sent.

customizing mail sending form in php using core php

Well i am in need of little help here.
I am doing a php custom mail sending form in which i need to make custom option for user to setup - port, host, smtp email, smtp pass, smtp username, also there is option to enable or disable smtp, Those all are working, but i have a situation here.
i also want to enable SSL Option for user, so if user select SSL is yes then it will send using SSL to prevent email going to SPAM. but i am unable to do this, can anyone help me with this.
here is code that i am using please check and assist anyone-
https://www.dropbox.com/s/m7ltrl8q0dxic4v/smtpmail-dummy.zip?dl=0
Here for this set the SMTPSecure property of PHPMailer class to 'ssl'. Then connect it using SmtpConnect() method.
So basically you can write a code like below. Not exactly, just for reference.
<?php
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("developer#vrstamphead.com", "Developer Rakesh");
//reply-to address
$mail->SetFrom("developer#vrstamphead.com", "Developer Rakesh"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://www.asif18.com/7/php/send-mails-using-smtp-in-php-by-gmail-server-or-own-domain-server/'>Asif18</a></b>"); //Put your body of the message you can place html code here
if ($_SESSION['enable_ssl'] = 1) { // 1 for true and 0 for false
$mail->SMTPSecure = '';
$mail->SmtpConnect();
}
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
?>
Instead of $_SESSION['enable_ssl'], you can use $_POST['enable_ssl'] as well if you're providing it right a way.
Hope this helps...

SMTP Reply At least one Recipient

I have a problem with Replying mails with SMTP through PHPMailer. When I try to send the mail I get
"You must provide at least one recipient email address."
The following PHP Code I use is:
require("smtp/class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = '****#gmail.com';
$mail->Password = '***';
$mail->SetFrom('***#gmail.com', '***#gmail.com');
$mail->Subject = 'RE: Hello World';
$mail->Body = 'Hello World';
$mail->AddReplyTo('****#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
I would like to know what else I'm lacking in the configuration.
You're missing a To address. You can add one like so:
$mail->AddAddress('josh#example.net', 'Josh Adams');
See a full example here: https://github.com/PHPMailer/PHPMailer#a-simple-example
The Reply-To header designates the default/recommended address to use when the recipient clicks "reply."
AddReplyTo is used to add a Reply-To address. Responses to messages you send with a reply-to address are delivered to that address.
Say, you send an email to one of your visitors with the reply-to address set to support#example.com. When they reply to that email, it will be sent to the email you specified as AddReplyTo.
If you're trying to send an email to yourself, you can just use AddAddress instead.
$mail->AddAddress('someone#example.net', 'JohnDoe');
Hope this helps!
Documentation: phpMailer methods, phpMailer examples.
Use $mail->AddAddress() instead of $mail->AddReplyTo().

Php mailer error: SMTP error

I am using php mailer class. I am getting :
SMTP Error: The following recipients failed: s_deshmukh88#hotmail.com
$mail = new phpMailer();
$body = "Hello, this is a test mail.";
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->Port = 25; // set the SMTP server port
$mail->Host = "localhost"; // SMTP server
$mail->Username = "localhost"; // SMTP server username
$mail->Password = "password"; // SMTP server password
//$mail->SMTPSecure = "tls";
//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("name#domain.com","First Last");
$mail->From = "name#domain.com";
$mail->FromName = "First Last";
$to = "s_deshmukh88#hotmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($mail->Send()){
echo 'Message has been sent.';
}
What can be the reason ?
Just try to put SMTPAuth to false and Host to'localhost'.
Thanks !! :)
I had this problem using authenticated SMTP. When I tested my mailing from home using xampp but connected to a (shared) server, emails went to users without problems. When I tried the same thing with the PHP on the host server, emails went through to addresses on my account but NOT to external addresses. (Ie. if my address is fred#bloggs.com and I send an email to fred#bloggs.com, it goes through but if I send it to jim#smith.com, it fails) The solution turned out to be to use the full host address when working on my local pc, ie $mail->Host = "mail.fred.bloggs.com", but $mail->Host = "localhost" on the server. May seem perverse but it works.
I just ran into this issue, and in my case, i believe it was the webhost's mail server that was not allowing a non-matching domain name for the From address. So for example, i was using mail.mydomain.com to send mail, but i wanted the from address to be test#test.com
When i switched the from address to test#mydomain.com, then the email was getting sent.

Categories