Core\Mail\ReportRecipient::isPermanent
$reportRecipient->isPermanent(): boolReturns true when the delivery to this recipient failed permanently, and false when the problem may be temporary.
A bounce is a message from a mail server that says your mail could not be delivered. A delivery report is the standard part of a bounce that lists each recipient and what happened. This method reads the status code of one recipient in that report.
The result is true when the status code starts with 5, such as 5.1.1 for an address that does not exist. The server will not deliver to that address if you send the mail again. A code that starts with 4 is temporary. 4.2.2 is an example: it means that the mailbox is full. For such a code, the result is false. A recipient with no status code also gives false.
In plain words: a permanent failure means "stop sending to this address". A temporary one means "try again later".
Good to know: use this to decide when to stop mailing an address. Many mail providers block a sender that keeps sending to addresses that do not exist.
Return value
Returns bool. true for a permanent failure.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Message::report reads the delivery report in a bounce or a delay. For each recipient, it gives the address, what happened, the status code such as 5.1.1, and whether the failure is permanent. |
Examples
Stop or try again
<?nvs// Shows which recipients of a delivery report failed permanently and which may still get the mail.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
Message $bounce = Message::parse( "From: mailer-daemon@mx.example.net\n" . "Subject: Undelivered Mail\n" . "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n" . "--b\nContent-Type: message/delivery-status\n\n" . "Reporting-MTA: dns; mx.example.net\n\n" . "Final-Recipient: rfc822; ada@example.org\nAction: failed\nStatus: 5.1.1\n\n" . "Final-Recipient: rfc822; bo@example.org\nAction: delayed\nStatus: 4.2.2\n" . "--b--\n",);
// `5.1.1` starts with `5`, so the result is `true`. `4.2.2` starts with `4`, so the result is `false`.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { string $next = $recipient->isPermanent() ? 'stop sending' : 'try again later'; echo $recipient->address(), ' ', $recipient->status(), ': ', $next, "\n";}ada@example.org 5.1.1: stop sendingbo@example.org 4.2.2: try again laterA recipient without a status
<?nvs// Shows that a recipient without a status code is not a permanent failure.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
// The report says the delivery failed, but it gives no `Status` field.Message $bounce = Message::parse( "From: mailer-daemon@mx.example.net\n" . "Subject: Undelivered Mail\n" . "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n" . "--b\nContent-Type: message/delivery-status\n\n" . "Reporting-MTA: dns; mx.example.net\n\n" . "Final-Recipient: rfc822; ada@example.org\nAction: failed\n" . "--b--\n",);
// The status is an empty string, so the result is `false`. This prints `false`.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo $recipient->address(), ' [', $recipient->status(), '] ', $recipient->isPermanent() ? 'true' : 'false', "\n";}ada@example.org [] falseRemove dead addresses from a newsletter
<?nvs// Removes each address that failed permanently from a newsletter list, and keeps the others.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
array<string> $subscribers = ['ada@example.org', 'bo@example.org', 'cy@example.org', 'dee@example.org'];
// These bounces arrived after the last newsletter.array<string> $bounces = [ "From: mailer-daemon@mx.example.net\n" . "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n" . "--b\nContent-Type: message/delivery-status\n\n" . "Reporting-MTA: dns; mx.example.net\n\n" . "Final-Recipient: rfc822; ada@example.org\nAction: failed\nStatus: 5.1.1\n\n" . "Final-Recipient: rfc822; cy@example.org\nAction: delayed\nStatus: 4.4.1\n" . "--b--\n", "From: postmaster@mx.example.com\n" . "Content-Type: multipart/report; report-type=delivery-status; boundary=\"c\"\n\n" . "--c\nContent-Type: message/delivery-status\n\n" . "Reporting-MTA: dns; mx.example.com\n\n" . "Final-Recipient: rfc822; dee@example.org\nAction: failed\nStatus: 5.2.1\n" . "--c--\n",];
// This loop collects the addresses that failed permanently.array<tainted string> $dead = [];foreach ($bounces as string $raw) { foreach (Message::parse($raw)->report()?->recipients() ?? [] as ReportRecipient $recipient) { if ($recipient->isPermanent()) { $dead[] = $recipient->address(); } }}
// A temporary problem keeps the address on the list.foreach ($subscribers as string $address) { echo $address, Core\Arr::contains($dead, $address) ? ' removed' : ' kept', "\n";}ada@example.org removedbo@example.org keptcy@example.org keptdee@example.org removed