Core\Mail\ReportRecipient::action
$reportRecipient->action(): Core\Mail\ReportRecipient::ActionReturns what the mail server did with the message for one recipient of a delivery report.
A delivery report is the standard part of a bounce that lists each recipient and what happened. A bounce is a message from a mail server that says your mail could not be delivered. The Action line of the report says what the server did for one recipient.
The result is a Core\Mail\ReportRecipient::Action case. Failed means the message was not delivered. Delayed means the message is late and the server still tries. Delivered means the server put the message in the mailbox. Relayed means the message was passed on to a server that sends no report. Expanded means the message was sent on to the members of a mailing list.
One report may give a different action for each recipient. A program that handles bounces checks the action of each recipient before it changes anything. Only Failed means the address did not get the message.
Good to know: some recipients have no Action line, or have a word that the delivery report standard (RFC 3464) does not define. Such a recipient is not in recipients(), so action() never returns a value for it.
Return value
Returns Core\Mail\ReportRecipient::Action. A Core\Mail\ReportRecipient::Action case.
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
What the server did
<?nvs// Shows the action of each recipient in one delivery report.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
class Said{ public static function of(ReportRecipient::Action $action): string { return match ($action) { ReportRecipient::Action::Failed => 'failed', ReportRecipient::Action::Delayed => 'is late', ReportRecipient::Action::Delivered => 'was delivered', ReportRecipient::Action::Relayed => 'was passed on', ReportRecipient::Action::Expanded => 'went to a mailing list', }; }}
Message $report = 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; bo@example.org\nAction: delayed\nStatus: 4.4.1\n\n" . "Final-Recipient: rfc822; cy@example.org\nAction: delivered\nStatus: 2.0.0\n" . "--b--\n",);
foreach ($report->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo 'The mail to ', $recipient->address(), ' ', Said::of($recipient->action()), "\n";}The mail to ada@example.org failedThe mail to bo@example.org is lateThe mail to cy@example.org was deliveredA delivery notice is not a bounce
<?nvs// Shows a delivery report where every recipient got the message. It is not a bounce.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
// Some servers send a report when the message arrives, if the sender asked for one.Message $notice = Message::parse( "From: mailer-daemon@mx.example.net\n" . "Subject: Delivery Status Notification (Success)\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",);
// `kind()` returns the kind of message. `Other` means it is not a bounce, a delay, a complaint, a reply or an automatic reply.// No recipient failed or is late, so the kind is `Other`. This prints `bounce: no`.echo 'bounce: ', $notice->kind() == Message::Kind::Bounce ? 'yes' : 'no', "\n";
// The report is still there, and the action of the recipient is `Delivered`.foreach ($notice->report()?->recipients() ?? [] as ReportRecipient $recipient) { bool $delivered = $recipient->action() == ReportRecipient::Action::Delivered; echo $recipient->address(), ' delivered: ', $delivered ? 'yes' : 'no', "\n";}bounce: noada@example.org delivered: yesGive up or wait
<?nvs// A bounce handler stops mailing an address that failed, and waits for one that is late.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
class Handler{ public static function decide(ReportRecipient $recipient): string { return match ($recipient->action()) { ReportRecipient::Action::Failed => 'stop sending to this address', ReportRecipient::Action::Delayed => 'wait, the server tries again', default => 'nothing to do', }; }}
array<string> $reports = [ "Final-Recipient: rfc822; ada@example.org\nAction: failed\nStatus: 5.1.1\n", "Final-Recipient: rfc822; bo@example.org\nAction: delayed\nStatus: 4.4.7\n", "Final-Recipient: rfc822; list@example.org\nAction: expanded\nStatus: 2.0.0\n",];
foreach ($reports as string $group) { Message $message = 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" . $group . "--b--\n", ); foreach ($message->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo $recipient->address(), ': ', Handler::decide($recipient), "\n"; }}ada@example.org: stop sending to this addressbo@example.org: wait, the server tries againlist@example.org: nothing to do