我想用很少的模板系统发送多封电子邮件。我试着自己,但并不是一切顺利。我有这个模板
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>new email template</title>
</head>
<body>
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<div align="center">
Hi,
<p> I would like to send at your email {email}</p>
<p> a little hash sum {hash}</p>
</div>
</div>
</body>
</html>
我的PHPmailer片段(up仅配置为我的邮件帐户)
$mail->Subject = "Mail sending template";
$recipients = array(
'mail1@mail.com' => 'gfsggsfg5t653rwtwrwtwrt',
'mail2@mail.com' => '6536536536356363636536356',
);
foreach($recipients as $email => $hash) {
$mail->addAddress($email);
$vars = array('{email}','{hash}');
$values = array($email,$hash);
$body = str_replace($vars,$values,$body);
$mail->msgHTML($body);
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $email) . ') ' . $mail->ErrorInfo . '<br />';
break;
} else {
echo "Send ok for: :" .$email . ' (' . str_replace("@", "@", $email) . ')<br />';
}
$mail->clearAddresses();
$mail->clearAttachments();
}
当我尝试发送这些电子邮件时,它发送确定。我可以收到电子邮件,但都有相同的数据:从第一个&#34;行&#34; $ recipments(mail1@mail.com)。来自mail2@mail.com我可以收到一封电子邮件,但是来自mail1@mail.com的{email}和{hash}。
出了什么问题?
感谢您的帮助:)
答案 0 :(得分:0)
简单的错误:你在循环之前的某个地方将你的原始模板加载到$body
,但是在循环内你用替换完成覆盖$body
,所以下次循环时,你的占位符不再存在。这应该有效:
$mail->Subject = "Mail sending template";
$recipients = array(
'mail1@mail.com' => 'gfsggsfg5t653rwtwrwtwrt',
'mail2@mail.com' => '6536536536356363636536356',
);
foreach($recipients as $email => $hash) {
$mail->addAddress($email);
$vars = array('{email}','{hash}');
$values = array($email,$hash);
$body2 = str_replace($vars,$values,$body);
$mail->msgHTML($body2);
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $email) . ') ' . $mail->ErrorInfo . '<br />';
break;
} else {
echo "Send ok for: :" .$email . ' (' . str_replace("@", "@", $email) . ')<br />';
}
$mail->clearAddresses();
$mail->clearAttachments();
}