我无法找到特定情况的答案。
我正在开发一个系统,用于将发布从数据库发送到邮件列表。我设法使用普通loop
代码。
但是,我想仅使用循环来使用BCC
添加收件人,如果有人错过了以下内容,我还要保留error handling
:
foreach($array as $user){
$mail->AddBCC( $user['email'], $user['customerName']);
}
try{
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment
$mail->AddReplyTo("noreply@company.com","Company Name");
$mail->SetFrom('noreply@company.com', 'Company Name');
$mail->Subject = "Company| E-Zine";
$mail->MsgHTML($ezineContent);
if(!$mail->Send()) {
//show error msg
} else {
//show successful msg
}
}catch (phpmailerException $e) {
//show error msg
}catch (Exception $e) {
//show error msg
}
$mail->ClearAddresses();
然后我想通过一个电话将电子邮件发送给上面添加的所有收件人。
是否可以进行错误处理并查找是否有人因为地址不正确而未收到错误?
答案 0 :(得分:5)
跟踪邮件操作成功的常见做法是使用电子邮件的“Return-Path”标题。
电子邮件示例及其标题:
Return-Path: server@return.xyz.com
Received: from localhost (mx-1-1 [127.0.0.1])
by mx-1.xyz.com (Postfix) with ESMTP id 3F81556754
for <jbradler@xyz.com>; Wed, 2 May 2012 12:27:18 +0200 (CEST)
To: jbradler@xyz.com
Subject: test mailing
From: Jens <jbradler@xyz.com>
Message-Id: <20120502102717.763ADA88004@xyz.com>
Date: Wed, 2 May 2012 12:27:17 +0200 (CEST)
Hi Folks, ...
因此,“From”标题是您希望读者看到的真实姓名和电子邮件地址的选择,“返回路径”的主要目的是指定消息指示未送达或其他的地址邮件系统故障将被发送([详见RFC 2821] [1])。
所以基本上这个标题是开始提取未送达报告的正确位置。
我将如何做到这一点:
这是一个具有唯一退货路径的示例:
Return-Path: 123-456@return.xyz.com
Received: from localhost (mx-1-1 [127.0.0.1])
by mx-1.xyz.com (Postfix) with ESMTP id 3F81556754
for <jbradler@xyz.com>; Wed, 2 May 2012 12:27:18 +0200 (CEST)
To: jbradler@xyz.com
Subject: test mailing
From: Jens <jbradler@xyz.com>
Message-Id: <20120502102717.763ADA88004@xyz.com>
Date: Wed, 2 May 2012 12:27:17 +0200 (CEST)
Hi Folks, ...
就是这样。
编辑 - 如何通过phpMailer实现:
/* define domain name for non-delivery reports */
define('RETURN_PATH_DOMAIN', 'return.xyz.com');
/* get current campaign id */
$campaignId = 123;
/* loop recipient list and send email */
foreach ($array as $userId => $user) {
try{
$mail = new PHPMailer();
$mail->HeaderLine('Return-Path', $userId . '-' $campaignId . '@' . RETURN_PATH_DOMAIN);
$mail->To($user['email'], $user['customerName']);
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->AddReplyTo("noreply@company.com","Company Name");
$mail->SetFrom('noreply@company.com', 'Company Name');
$mail->Subject = "Company| E-Zine";
$mail->MsgHTML($ezineContent);
if(!$mail->Send()) {
// show log
} else {
//show successful msg
}
} catch (Exception $e) {
// show error
}
}
据我记得,我们使用本地sendmail存在问题。标题Return-Path被本地MTA的一些配置所取代。如果是这种情况,请尝试将SMTP用于可用的中继主机。