php Swift Mailer 递送状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5768389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:25:38  来源:igfitidea点击:

Swift Mailer Delivery Status

phpsymfony1symfony-1.4swiftmailer

提问by DavidW

Does anyone know if SwiftMailer send function returns delivery status? I would like to be able to know that email was delivered or not delivered.Is this possible?

有谁知道 SwiftMailer 发送函数是否返回传递状态?我想知道电子​​邮件已送达或未送达。这可能吗?

Thanks

谢谢

回答by ybull

There are at least three layers of checks that SwiftMailer supports, that will report several types of delivery failures.

SwiftMailer 支持至少三层检查,它们将报告几种类型的传递失败。

1) Always check the return code from SwiftMailer's send() or batchSend() commands for a non-zero result. From the documentation:

1) 始终检查 SwiftMailer 的 send() 或 batchSend() 命令的返回代码是否为非零结果。从文档

//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) Use the failures-by-reference featureto know if specific address(es) were rejected or couldn't complete:

2) 使用按引用失败的功能来了解特定地址是否被拒绝或无法完成:

//Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
  echo "Failures:";
  print_r($failures);
}

/*
Failures:
Array (
  0 => [email protected],
  1 => [email protected]
)
*/

3) In a few situations you might want to enable return receiptsas well, which confirm that an email reader displayed the message. They often are disabled or ignored by users or their email apps, but if you get a receipt, it is highly confirmatory. Note also that this might occur many days after sending so it's not a real-time synchronous test like the two above.

3) 在某些情况下,您可能还希望启用回执,以确认电子邮件阅读器显示了消息。它们通常被用户或其电子邮件应用程序禁用或忽略,但如果您收到收据,则可以高度确认。另请注意,这可能会在发送后许多天发生,因此它不是像上面两个那样的实时同步测试。

$message->setReadReceiptTo('[email protected]');

However, since there are so many variables and layers of systems involved in SMTP delivery, it is not generally possible to be absolutely sure messages were delivered. The best you can do is make sure you're using the first two checks above. If you're using YOUR own server for the SMTP service, then you ALSO need to be watching your logs and queues as Marc B mentioned.

但是,由于 SMTP 传递涉及许多变量和系统层,因此通常不可能绝对确定邮件是否已传递。你能做的最好的事情就是确保你使用了上面的前两个检查。如果您将自己的服务器用于 SMTP 服务,那么您还需要像 Marc B 提到的那样查看您的日志和队列。

One other example that emphasizes the need to get familiar with whatever underlying email system you're using. I've just started using the Swift_AWSTransport by John Hobbs for Amazon Web Services SES. SES has the ability to return an XML response with diagnostic information for each message sent through it. Though SwiftMailer doesn't inherently understand how to use that XML response, I have found it invaluable for troubleshooting delivery. I mention it because I found that in some cases, checks #1 and #2 above will appear successful to SwiftMailer, yet SES didn't like something about my message formatting. I'm therefore looking into parsing that XML as an additional check.

另一个例子强调需要熟悉您正在使用的任何底层电子邮件系统。我刚刚开始将 John Hobbs 的 Swift_AWSTransport 用于 Amazon Web Services SES。SES 能够为通过它发送的每条消息返回带有诊断信息的 XML 响应。尽管 SwiftMailer 本身并不了解如何使用该 XML 响应,但我发现它对于解决交付问题非常宝贵。我提到它是因为我发现在某些情况下,上面的检查 #1 和 #2 对 SwiftMailer 来说似乎是成功的,但 SES 不喜欢我的消息格式。因此,我正在考虑将 XML 解析为附加检查。

回答by fyrye

Rather old post, but as of Swiftmailer 4+ and aside from getting the resultstatus from the sendmethod call. If you're wanting to debug your mailer and see what is happening during the transport, you can use the Logger Plugins, or even create your own Plugin. https://swiftmailer.symfony.com/docs/plugins.html

相当老的帖子,但从 Swiftmailer 4+ 开始,除了从方法调用中获取result状态send。如果您想调试邮件程序并查看传输过程中发生的情况,您可以使用 Logger 插件,甚至创建您自己的插件。 https://swiftmailer.symfony.com/docs/plugins.html

$mailer = \Swift_Mailer::newInstance(
    \Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
);
$logger = new \Swift_Plugins_Loggers_ArrayLogger;
//$logger = new \Swift_Plugins_Loggers_EchoLogger; //echo messages in real-time
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

foreach ($recipients as $recipient) {
    //...

    $mailer->send(/*...*/);
}

echo $logger->dump(); //not needed if using EchoLogger plugin

Which will output the transport message for each sendcall, that looks like

它将为每个send调用输出传输消息,看起来像

++ Starting Swift_SmtpTransport 
<< 220 smtp.gmail.com ESMTP x12sm4143221vkc.19 - gsmtp 
>> EHLO server.example.com 
<< 250-smtp.gmail.com at your service, [192.168.1.1] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 
>> AUTH LOGIN 
<< 334 AbcDefGhIjKlMnop 
>> AbcDefGhIjKlMnop== 
<< 334 AbcDefGhIjKlMnop 
>> AbcDefGhIjKlMnop== 
<< 235 2.7.0 Accepted 
++ Swift_SmtpTransport started 
>> MAIL FROM: 
<< 250 2.1.0 OK x12sm4143221vkc.19 - gsmtp 
>> RCPT TO:<[email protected]> 
<< 250 2.1.5 OK x12sm4143221vkc.19 - gsmtp 
>> DATA 
<< 354 Go ahead x12sm4143221vkc.19 - gsmtp 
>> . 
<< 250 2.0.0 OK 1468948643 x12sm4143221vkc.19 - gsmtp

回答by Marc B

Swiftmailer has nothing to do with the actual delivery of the mail. It just hands things over to whatever SMTP server you specified, and it's that server that takes care of the delivery. You need to check the SMTP server's logs to see what happened to the mail. It may get stuck in the outgoing queue because the server's swamped. It may get stuck in a queue because the receiving end is unreachable or is using grey-listing, etc... Swiftmailer's job ends once it gets acknowledgement from the SMTP server that the mail's been queued.

Swiftmailer 与邮件的实际递送无关。它只是将东西交给您指定的任何 SMTP 服务器,由该服务器负责传送。您需要检查 SMTP 服务器的日志以查看邮件发生了什么。由于服务器拥挤,它可能会卡在传出队列中。它可能会卡在队列中,因为接收端无法访问或正在使用灰名单等......一旦从 SMTP 服务器获得邮件已排队的确认,Swiftmailer 的工作就会结束。