PHPMailer - 使用模板系统发送多封电子邮件
PHPMailer - sending multiple emails with template system
我想用很少的模板系统发送多封电子邮件。我尝试自己,但并非一切顺利。我有这个模板
<!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 片段(上面只是为我的邮件帐户配置的)
$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();
}
当我尝试发送这些电子邮件时,它发送正常。我可以接收电子邮件,但都具有这些相同的数据:来自 $recipments (mail1@mail.com) 的第一个 "row"。来自 mail2@mail.com 我可以收到一封电子邮件,但是带有来自 mail1@mail.com 的 {email} 和 {hash}。
怎么了?
感谢您的帮助:)
简单错误:您在循环之前的某处将原始模板加载到 $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();
}
我想用很少的模板系统发送多封电子邮件。我尝试自己,但并非一切顺利。我有这个模板
<!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 片段(上面只是为我的邮件帐户配置的)
$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();
}
当我尝试发送这些电子邮件时,它发送正常。我可以接收电子邮件,但都具有这些相同的数据:来自 $recipments (mail1@mail.com) 的第一个 "row"。来自 mail2@mail.com 我可以收到一封电子邮件,但是带有来自 mail1@mail.com 的 {email} 和 {hash}。
怎么了?
感谢您的帮助:)
简单错误:您在循环之前的某处将原始模板加载到 $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();
}