Skip to content

Core\Mail\Message::report

$message->report(): ?Core\Mail\Report

Returns the delivery report inside a bounce or a delay, as a Core\Mail\Report.

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. A server also sends one when the delivery is late. For each recipient, the report has what the server did, the status code and the server's own message. recipients() on the result returns that list.

The result is null for every message that is not a delivery report: a reply, an auto-reply, a spam complaint or a normal mail. Read it with ?->, which calls a method only when the value is not null.

Good to know: a report that says every recipient got the mail still has a report. Its kind() is Other, because nothing failed and nothing is late. Only the first 1,000 recipients of a report are read.

The examples below list the recipients of a bounce, show the messages that have no report, and handle a report where one address failed and one is late.

Return value

Returns ?Core\Mail\Report. The report, or null when the message is not a delivery report.

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 recipients of a bounce

<?nvs
// Shows the delivery report of a bounce: one line for each recipient.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
// A bounce is a message from a mail server that says your mail could not be delivered.
// Its delivery report is the standard part that lists each recipient and what happened.
Message $bounce = Message::parse(
"From: mailer-daemon@mx.example.net\nSubject: Undelivered Mail Returned to Sender\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",
);
// `report()` returns a `Core\Mail\Report`, because this message is a delivery report.
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) {
echo $recipient->address(), ': ', $recipient->diagnostic() ?? 'no message', "\n";
}
Output
ada@example.org: 550 5.1.1 User unknown
bo@example.org: 552 5.2.2 Mailbox full

Only a delivery report has one

<?nvs
// Shows that `report()` is `null` for a message that is not a delivery report.
use Core\Mail\Message;
array<string> $messages = [
'a reply' => "From: cy@example.org\nIn-Reply-To: <order-42@example.com>\nSubject: Re: Your order\n\nThanks.\n",
'a bounce written as text' => "From: postmaster@example.org\nSubject: Undeliverable\n\nThe address does not exist.\n",
'a delivery report' => "From: mailer-daemon@mx.example.net\nSubject: Delivered\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: delivered\nStatus: 2.0.0\n"
. "--b--\n",
];
// The first message is a reply. The second message says that the mail was not delivered.
// Neither of them has a report. The third message is a delivery report that says the mail arrived.
foreach ($messages as string $what => string $raw) {
?Core\Mail\Report $report = Message::parse($raw)->report();
echo $what, ': ', $report == null ? 'no report' : 'a report', "\n";
}
Output
a reply: no report
a bounce written as text: no report
a delivery report: a report

Stop or wait for each address

<?nvs
// A mailing list reads a delivery report and decides for each address: stop sending, or wait.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
Message $notice = Message::parse(
"From: mailer-daemon@mx.example.net\nSubject: Delivery Status Notification\n"
. "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n"
. "--b\nContent-Type: text/plain\n\nSome recipients did not get your mail.\n"
. "--b\nContent-Type: message/delivery-status\n\n"
. "Reporting-MTA: dns; mx.example.net\n\n"
. "Final-Recipient: rfc822; old-address@example.org\nAction: failed\nStatus: 5.1.1\n\n"
. "Final-Recipient: rfc822; busy@example.net\nAction: delayed\nStatus: 4.2.2\n\n"
. "Final-Recipient: rfc822; fine@example.com\nAction: delivered\nStatus: 2.0.0\n"
. "--b--\n",
);
// `?->` calls the method only when the value is not `null`.
// `?? []` gives an empty array when the result is `null`.
foreach ($notice->report()?->recipients() ?? [] as ReportRecipient $recipient) {
if ($recipient->isPermanent()) {
echo 'remove ', $recipient->address(), ' from the list', "\n";
} elseif ($recipient->action() == ReportRecipient::Action::Delayed) {
echo 'wait for ', $recipient->address(), ', the server tries again', "\n";
} else {
echo 'nothing to do for ', $recipient->address(), "\n";
}
}
Output
remove old-address@example.org from the list
wait for busy@example.net, the server tries again
nothing to do for fine@example.com