Core\Mail\ReportRecipient::diagnostic
$reportRecipient->diagnostic(): ?stringReturns the message that the receiving mail server gave for one recipient of a delivery report, such as 550 5.1.1 User unknown.
The text comes from the Diagnostic-Code line of the report (the server's own message), without the smtp; type in front of it. Each server writes its own words, so the text is meant for a person to read. When the report has no Diagnostic-Code line, the result is null.
Good to know: a program should decide what to do with status() and isPermanent(), and keep this text for a log or a support page. The server that sent the report wrote it, so the result is a tainted string. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. Escape it before you show it on a page.
Return value
Returns ?string. The message, or null when the report gives none. It is tainted, because the server that sent the report wrote it.
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
The server message for each recipient
<?nvs// Shows the message that the receiving server gave for each recipient of a bounce.
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" . "Diagnostic-Code: smtp; 550 5.1.1 User unknown\n\n" . "Final-Recipient: rfc822; bo@example.org\nAction: failed\nStatus: 5.2.2\n" . "Diagnostic-Code: smtp; 552 5.2.2 Mailbox full\n" . "--b--\n",);
// The `smtp;` in front of the text is not part of the result.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo $recipient->address(), ': ', $recipient->diagnostic() ?? 'no message', "\n";}ada@example.org: 550 5.1.1 User unknownbo@example.org: 552 5.2.2 Mailbox fullA report without a message
<?nvs// Shows that `diagnostic()` returns `null` when the report has no `Diagnostic-Code` line.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
// The first recipient has no `Diagnostic-Code` line.// The second recipient has a long message that continues on the next line.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: failed\nStatus: 5.7.1\n" . "Diagnostic-Code: smtp; 550 5.7.1 Message rejected\n because of the sender policy\n" . "--b--\n",);
// The first recipient has no message, so the result is `null`.// For the second recipient, the two lines are joined with a space.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { ?tainted string $text = $recipient->diagnostic(); echo $recipient->address(), ': ', $text == null ? 'null' : $text, "\n";}ada@example.org: nullbo@example.org: 550 5.7.1 Message rejected because of the sender policyA bounce log for the support team
<?nvs// Writes one log line for each failed recipient, with the server message for the support team.
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" . "Diagnostic-Code: smtp; 550 5.1.1 <ada@example.org>: Recipient address rejected\n\n" . "Final-Recipient: rfc822; bo@example.org\nAction: delayed\nStatus: 4.4.1\n" . "Diagnostic-Code: smtp; 421 4.4.1 Connection timed out\n" . "--b--\n",);
// The program decides with `isPermanent()`. The message is only kept for a person to read.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { string $level = $recipient->isPermanent() ? 'error' : 'warning'; echo $level, ' ', $recipient->address(), ' "', $recipient->diagnostic() ?? '', "\"\n";}
// On a web page, the message is escaped. The `<` and `>` around the address become `<` and `>`.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo Core\Html::escape($recipient->diagnostic() ?? ''), "\n";}error ada@example.org "550 5.1.1 <ada@example.org>: Recipient address rejected"warning bo@example.org "421 4.4.1 Connection timed out"550 5.1.1 <ada@example.org>: Recipient address rejected421 4.4.1 Connection timed out