使用 PHPMailer 向收件人发送单独的电子邮件 当我将 $mail->SingleTo = TRUE;
添加到我的代码时,我在 To: 字段中什么也没有得到。
当我删除 $mail->SingleTo = TRUE;
时,我收到的电子邮件在 To: 字段中的电子邮件地址是正确的。
这是我得到的:
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
(其中 xxxxxxx 代表我的电子邮件地址。)
这是我的代码:
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!";
}
}
最佳答案
您正在使用 SMTP 发送电子邮件。使用 Smtp 发送时,phpmailer 类没有使用 SingleTo 参数。
此外,如果您在 SingleTo == true 时看到 CreateHeader 函数,则配方仅添加到 $this->SingleToArray 而不是 header 本身,所以基本上它是 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();
}
关于个人电子邮件的 PHPMailer 设置发送空到 : fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380193/