Setting up PHPMailer for individual emails sends empty fields to:

Using PHPMailer to send individual letters to recipients I do not receive anything in the To: field when I add $mail->SingleTo = TRUE;to my code.

When I delete $mail->SingleTo = TRUE;, I get emails with an email address in the To: field , which 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 is 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!";
}       
}
+3
source share
5 answers

You use SMTP to send email. The phpmailer class does not use the SingleTo parameter when sending using Smtp.

, CreateHeader, SingleTo == true, $this- > SingleToArray, , PHPmailer.

, , phpmailer, , SingleTo

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();

}
+3

, , , ClearAllRecipients(). :

{  // loop start
        $mail->AddAddress($person_email, $person_name);
        $mail->Send();
        $mail->ClearAllRecipients();
}  // loop end
+3

SmtpSend ( 701 class.phpmailer.php). , singleTo .

, , , singleTo , , , To: . singleTo , as-is ( 713 - 758).

, , , ( sendmail), , , singleTo.

0

$mail- > AddAddress()   CSV , :

$Emails="addr1@host.com,addr2@host.net"; #etc;

for :

$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.

0

SingleTo . "sendmail" "mail", SMTP. SingleTo SMTP, - , .

SingleTo SMTP, , SingleTo .

SMTP , , TO:. TO:, . , SingleTo 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 explained that it is better to control sending to multiple recipients at a higher level, since PHPMailer is not the sender of the mailing list. They say that using the PHP mail () function should be discouraged because it is extremely difficult to use safely; SMTP is faster, more secure and provides more control and feedback. And since SMTP is not compatible with SingleTo, PHPMailer authors remove SingleTo, not SMTP.

0
source

All Articles