Skip to content

Core\Mail\Message::bounceTag

$message->bounceTag(): ?string

Returns the tag after + in the address a received message was sent to, such as order-42 for bounces+order-42@example.com.

When Core\Mail::send has a bounceTag option, the mail server sends each bounce to an address with that tag. So the tag tells you which mail bounced. It also works when the bounce has no copy of your mail, and answers() is null.

The tag is read from the first Delivered-To header, then X-Original-To, then To. The first address with a valid tag gives the result. A valid tag has 1 to 64 letters, digits, ., _ or -, the same rule as for sending. The result is null when no address has a valid tag.

Good to know: the result is tainted, because whoever sent the message wrote these headers. Tainted means that the value comes from outside your program, so you must check it before you use it. Find the tag in your own records, such as a list of orders. Then use your own value from there.

The examples below find the order of a bounce, show which headers are read, and remove a newsletter reader whose address bounced.

Return value

Returns ?string. The tag, or null when no address has a valid tag.

Changelog

VersionChange
0.0.6Core\Mail\Message::kind returns what kind of message it is: a bounce, a delay, a complaint, an automatic reply, a reply or other. A bounce is a message from a mail server that says your mail could not be delivered. answers() returns the message id of the mail it is about, so you can find the mail you sent.

Examples

The tag names the order

<?nvs
// Shows how a bounce tag tells the shop which order confirmation bounced.
use Core\Mail\Message;
// The shop sent the confirmation of order 1042 with `bounceTag: 'order-1042'`.
// The server sends the bounce to `bounces+order-1042@example.com`, and writes that in `Delivered-To`.
Message $bounce = Message::parse(
"Delivered-To: bounces+order-1042@example.com\n"
. "From: mailer-daemon@mx.example.net\nTo: bounces@example.com\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; anna@example.org\nAction: failed\nStatus: 5.1.1\n"
. "--b--\n",
);
// `answers()` returns the id of the mail that bounced, from the copy inside the bounce.
// This bounce has no copy, so `answers()` is `null`. The tag still names the order.
echo 'answers: ', $bounce->answers() ?? 'null', "\n";
echo 'tag: ', $bounce->bounceTag() ?? 'null', "\n";
Output
answers: null
tag: order-1042

Which header is read

<?nvs
// Shows which address gives the tag, and when the result is `null`.
use Core\Mail\Message;
array<string> $headers = [
// `Delivered-To` is read first. This prints `Delivered-To: order-7`.
'Delivered-To' => "Delivered-To: bounces+order-7@example.com\nTo: bounces+order-8@example.com\n",
// Without `Delivered-To`, `X-Original-To` is read. This prints `X-Original-To: order-9`.
'X-Original-To' => "X-Original-To: bounces+order-9@example.com\nTo: bounces@example.com\n",
// Without both, the `To` address is read. This prints `To: order-10`.
'To' => "To: Shop Bounces <bounces+order-10@example.com>\n",
// A space and `!` are not allowed in a tag. This prints `invalid tag: null`.
'invalid tag' => "To: \"bounces+drop table!\"@example.com\n",
// An address without `+` has no tag. This prints `no tag: null`.
'no tag' => "To: bounces@example.com\n",
];
foreach ($headers as string $what => string $lines) {
Message $message = Message::parse($lines . "Subject: Test\n\nBody.\n");
echo $what, ': ', $message->bounceTag() ?? 'null', "\n";
}
Output
Delivered-To: order-7
X-Original-To: order-9
To: order-10
invalid tag: null
no tag: null

Remove a reader whose address bounced

<?nvs
// A newsletter sends each reader's mail with the tag `reader-<id>`. A bounce then removes that reader.
use Core\Mail\Message;
// This array has the address of each reader of the newsletter. The key is the tag of the reader.
array<string> $readers = [
'reader-1' => 'ada@example.org',
'reader-2' => 'bo@example.org',
'reader-3' => 'cy@example.org',
];
array<string> $bounces = [
"Delivered-To: news+reader-2@example.com\nFrom: mailer-daemon@mx.example.org\nSubject: Undeliverable\n"
. "Content-Type: multipart/report; report-type=delivery-status; boundary=\"b\"\n\n"
. "--b\nContent-Type: message/delivery-status\n\nReporting-MTA: dns; mx.example.org\n\n"
. "Final-Recipient: rfc822; bo@example.org\nAction: failed\nStatus: 5.1.1\n--b--\n",
// This message has a tag that the newsletter never sent. Somebody wrote this address by hand.
"Delivered-To: news+reader-99@example.com\nFrom: someone@example.net\nSubject: Hello\n\nHi.\n",
];
foreach ($bounces as string $raw) {
Message $message = Message::parse($raw);
?tainted string $tag = $message->bounceTag();
// Only a real bounce, with a tag of a known reader, removes the reader.
if ($message->kind() != Message::Kind::Bounce || $tag == null || !isset($readers[$tag])) {
echo 'ignored: ', $tag ?? 'no tag', "\n";
continue;
}
foreach ($readers as string $id => string $address) {
if ($id == $tag) {
echo 'removed ', $address, "\n";
}
}
}
Output
removed bo@example.org
ignored: reader-99