Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

php - Swift Mailer Delivery Status

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?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

//Send the message
$numSent = $mailer->send($message);

printf("Sent %d messages
", $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
";
}
else
{
  echo "Failed
";
}

2) Use the failures-by-reference feature to know if specific address(es) were rejected or couldn't complete:

//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 receipts as 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.

$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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...