Core\Mail\ReportRecipient::address
$reportRecipient->address(): stringReturns the address that the mail server tried to deliver to, such as ada@example.org.
The address comes from the Final-Recipient line of the delivery report (the address the server tried). A delivery report is the standard part of a bounce that lists each recipient and what happened. The type in front of the address, such as rfc822;, and any angle brackets around it are removed. So you can compare the result with the addresses you sent mail to.
Good to know: the result is a tainted string, 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. Use the address to find the row in your own data, and change that row. A recipient whose address is empty or has a control character is not in the report's list.
The examples below print the address of each recipient, show the brackets and the type being removed, and find the customers whose address failed.
Return value
Returns string. The address, such as user@example.com. 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 address that failed
<?nvs// Shows the address of each recipient in a bounce.
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" . "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.1.1\n" . "--b--\n",);
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo 'Could not deliver to ', $recipient->address(), "\n";}Could not deliver to ada@example.orgCould not deliver to bo@example.orgBrackets and the type are removed
<?nvs// Shows that `address()` returns the bare address, whichever way the server wrote it.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
// Three servers write the same kind of line in three ways.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\n\n" . "Final-Recipient: rfc822; <bo@example.org>\nAction: failed\n\n" . "Final-Recipient: cy@example.org\nAction: failed\n" . "--b--\n",);
// Each line prints the address alone. The type `rfc822;` and the brackets are removed.foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { echo '[', $recipient->address(), "]\n";}[ada@example.org][bo@example.org][cy@example.org]Mark the customers to contact
<?nvs// A shop finds each customer whose address failed, so somebody can ask them for a new one.
use Core\Mail\Message;use Core\Mail\ReportRecipient;
class Customers{ // This function returns the name of each customer of the shop, by email address. public static function names(): array<string> { return [ 'ada@example.org' => 'Ada Lovelace', 'bo@example.org' => 'Bo Berg', ]; }}
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; BO@example.org\nAction: failed\nStatus: 5.1.1\n\n" . "Final-Recipient: rfc822; zed@example.org\nAction: failed\nStatus: 5.1.1\n" . "--b--\n",);
foreach ($bounce->report()?->recipients() ?? [] as ReportRecipient $recipient) { // Servers may change the case of the address, so the program compares in lower case. tainted string $address = Core\Str::lower($recipient->address()); ?string $name = Customers::names()[$address] ?? null; echo $address, ': ', $name ?? 'not a customer', "\n";}bo@example.org: Bo Bergzed@example.org: not a customer