SwiftMailer 支持至少三层检查,将报告几种类型的传递失败。
1) 始终检查 SwiftMailer 的 send() 或 batchSend() 命令的返回码是否有非零结果。来自the documentation:
//Send the message
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
/* Note that often that only the boolean equivalent of the
return value is of concern (zero indicates FALSE)
if ($mailer->send($message))
{
echo "Sent\n";
}
else
{
echo "Failed\n";
}
2) 使用failures-by-reference feature 了解特定地址是否被拒绝或无法完成:
//Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
/*
Failures:
Array (
0 => receiver@bad-domain.org,
1 => other-receiver@bad-domain.org
)
*/
3) 在某些情况下,您可能还需要enable return receipts,这会确认电子邮件阅读器显示了该消息。它们通常被用户或其电子邮件应用程序禁用或忽略,但如果您收到收据,则具有高度确认性。另请注意,这可能会在发送后很多天发生,因此它不是像上面两个那样的实时同步测试。
$message->setReadReceiptTo('your@address.tld');
但是,由于 SMTP 传递涉及到如此多的变量和系统层,因此通常不可能绝对确定邮件是否已传递。您能做的最好的事情是确保您使用上面的前两项检查。如果您将自己的服务器用于 SMTP 服务,那么您还需要像 Marc B 提到的那样监视您的日志和队列。
另一个例子强调需要熟悉您使用的任何底层电子邮件系统。我刚刚开始使用 John Hobbs 为 Amazon Web Services SES 编写的 Swift_AWSTransport。 SES 能够为通过它发送的每条消息返回带有诊断信息的 XML 响应。尽管 SwiftMailer 本身并不了解如何使用该 XML 响应,但我发现它对于解决交付问题非常宝贵。我提到它是因为我发现在某些情况下,上面的检查 #1 和 #2 对 SwiftMailer 来说似乎是成功的,但 SES 不喜欢我的邮件格式。因此,我正在考虑解析该 XML 作为附加检查。