Skip to content

Core\Mail\Report::recipients

$report->recipients(): array<Core\Mail\ReportRecipient>

Returns each recipient of a delivery report, in the order the report lists them.

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. Core\Mail\Message::report() returns the report, and recipients() returns one Core\Mail\ReportRecipient for each address in it.

One report can be about several addresses. Some of them may have failed while others are only late, so check each recipient on its own.

Good to know: at most 1,000 recipients are read from one report. A recipient without a Final-Recipient line (the address the server tried) is not in the list. The Action line says what the server did. RFC 3464, the standard for delivery reports, lists its values. A recipient with another value is not in the list either.

Every text in the list is tainted, because the server that sent the report wrote it. Tainted means that the value comes from outside your program, so you must check or escape it before you use it.

Return value

Returns array<Core\Mail\ReportRecipient>. The recipients. At most 1,000 are read.

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

List the recipients of a bounce

<?nvs
// Shows the recipients of a bounce, one line for each address in the report.
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\n"
. "Subject: 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\n"
. "Final-Recipient: rfc822; bo@example.org\nAction: failed\nStatus: 5.2.2\n\n"
. "Final-Recipient: rfc822; cy@example.org\nAction: delayed\nStatus: 4.4.1\n"
. "--b--\n",
);
// `report()` is `null` for a message that is not a delivery report. Here it is a report.
array<ReportRecipient> $recipients = $bounce->report()?->recipients() ?? [];
echo Core\Arr::count($recipients), " recipients\n";
foreach ($recipients as ReportRecipient $recipient) {
echo $recipient->address(), ' ', $recipient->status(), "\n";
}
Output
3 recipients
ada@example.org 5.1.1
bo@example.org 5.2.2
cy@example.org 4.4.1

A normal message has no report

<?nvs
// Shows that only a delivery report has recipients. Other messages give an empty array.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
class Count
{
public static function recipients(Message $message): uint
{
// `?->` calls the method only when the value is not `null`.
// `?? []` gives an empty array when the result is `null`.
array<ReportRecipient> $recipients = $message->report()?->recipients() ?? [];
return Core\Arr::count($recipients);
}
}
// This message only looks like a bounce. It is not a delivery report.
Message $fake = Message::parse(
"From: mailer-daemon@example.net\nSubject: Undelivered Mail\n\n"
. "Final-Recipient: rfc822; ada@example.org\nAction: failed\n",
);
echo 'has a report: ', $fake->report() == null ? 'no' : 'yes', "\n";
echo 'recipients: ', Count::recipients($fake), "\n";
// A real report, with one recipient.
Message $bounce = Message::parse(
"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"
. "--b--\n",
);
echo 'has a report: ', $bounce->report() == null ? 'no' : 'yes', "\n";
echo 'recipients: ', Count::recipients($bounce), "\n";
Output
has a report: no
recipients: 0
has a report: yes
recipients: 1

Remove dead addresses from a newsletter

<?nvs
// A newsletter removes each address that failed permanently, and keeps the addresses that are only late.
use Core\Mail\Message;
use Core\Mail\ReportRecipient;
// The subscribers of the newsletter.
array<bool> $subscribed = [
'ada@example.org' => true,
'bo@example.org' => true,
'cy@example.org' => true,
];
// One report about the last newsletter.
Message $bounce = Message::parse(
"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.2.2\n"
. "--b--\n",
);
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) {
if ($recipient->action() != ReportRecipient::Action::Failed || !$recipient->isPermanent()) {
echo 'keep ', $recipient->address(), ": the server tries again\n";
continue;
}
// The address comes from the mail server. The program only changes an address it already has.
foreach ($subscribed as string $address => bool $on) {
if ($address == $recipient->address()) {
$subscribed[$address] = false;
echo 'remove ', $address, "\n";
}
}
}
foreach ($subscribed as string $address => bool $on) {
echo $address, ': ', $on ? 'subscribed' : 'removed', "\n";
}
Output
remove ada@example.org
keep cy@example.org: the server tries again
ada@example.org: removed
bo@example.org: subscribed
cy@example.org: subscribed