Skip to content

Core\Mail\ReportRecipient::status

$reportRecipient->status(): string

Returns the status code that the mail server gave for one recipient of a delivery report, such as 5.1.1.

The code is three numbers joined by dots. The first number is the class: 2 means the mail was delivered, 4 means a temporary problem, and 5 means a permanent failure. The second and third numbers say what went wrong. For example, 5.1.1 means the address does not exist, and 4.2.2 means the mailbox is full for now.

The code comes from the Status line of the report. The result is the first word of that line, so a comment after the code is not part of it. When the report has no such code, the result is an empty string.

Good to know: the server that sent the report wrote this text, 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. Compare it with the codes you know. Escape it before you show it on a page.

Return value

Returns string. The status code from the Status line of the report. The code is three numbers joined by dots, such as 5.1.1. The result is an empty string when the report has no such code. It is tainted, because the server that sent the report wrote it.

Changelog

VersionChange
0.0.6Core\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 status of each recipient

<?nvs
// Shows the status code of each recipient in a delivery report.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
// A mail server sends this report back. The report lists three recipients.
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\n"
. "Final-Recipient: rfc822; cy@example.org\nAction: delivered\nStatus: 2.0.0\n"
. "--b--\n",
);
// `5.1.1` means the address does not exist. `4.2.2` means the mailbox is full for now.
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) {
echo $recipient->address(), ': ', $recipient->status(), "\n";
}
Output
ada@example.org: 5.1.1
bo@example.org: 4.2.2
cy@example.org: 2.0.0

Only the code is returned

<?nvs
// Shows that `status()` returns only the code, and an empty string when there is no `Status` field.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
Message $delay = Message::parse(
"From: mailer-daemon@mx.example.net\n"
. "Subject: Delayed 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: delayed\nStatus: 4.4.1 (connection timed out)\n\n"
. "Final-Recipient: rfc822; bo@example.org\nAction: delayed\n"
. "--b--\n",
);
// The first recipient has a comment after its code. The result is `4.4.1`.
// The second recipient has no `Status` field. The result is an empty string.
foreach ($delay->report()?->recipients() ?? [] as ReportRecipient $recipient) {
echo $recipient->address(), ': [', $recipient->status(), "]\n";
}
Output
ada@example.org: [4.4.1]
bo@example.org: []

A reason the support team can read

<?nvs
// Turns each status code of a bounce into a reason that the support team can read.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
class Reason
{
// This function checks for the codes that this shop sees most often.
// Any other code gets a general reason from its first number.
public static function of(tainted string $status): string
{
if ($status == '5.1.1') {
return 'the address does not exist';
}
if ($status == '5.2.2' || $status == '4.2.2') {
return 'the mailbox is full';
}
if ($status == '5.7.1') {
return 'the receiving server does not accept mail from the shop';
}
if (Core\Str::startsWith($status, '4.')) {
return 'a temporary problem, the server tries again';
}
if (Core\Str::startsWith($status, '5.')) {
return 'a permanent problem';
}
return 'unknown';
}
}
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\n"
. "Final-Recipient: rfc822; cy@example.org\nAction: delayed\nStatus: 4.4.7\n\n"
. "Final-Recipient: rfc822; dee@example.org\nAction: failed\nStatus: 5.3.0\n"
. "--b--\n",
);
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) {
echo $recipient->address(), ' (', $recipient->status(), '): ', Reason::of($recipient->status()), "\n";
}
Output
ada@example.org (5.1.1): the address does not exist
bo@example.org (5.7.1): the receiving server does not accept mail from the shop
cy@example.org (4.4.7): a temporary problem, the server tries again
dee@example.org (5.3.0): a permanent problem