Core\Test\SentMail::bounceTag
$sentMail->bounceTag(): ?stringbounceTag() returns the bounce tag of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and bounceTag() is one of its methods.
A bounce is the error message that a mail server sends back when it cannot deliver a message. It goes to return_path of the [mail.<name>] block. The bounce tag is added to that address after a +. With return_path = "bounces@example.com" and the tag order-42, the bounce goes to bounces+order-42@example.com. So your program can read which message failed.
The result is the bounceTag option that your program gave to Core\Mail::send. When the program gave no bounceTag option, the result is null. A tag has 1 to 64 letters, digits, dots, underscores or hyphens. Core\Mail::send throws an error for any other tag.
Use bounceTag() to check that each message has a tag that names its delivery.
The examples below show a tag with an order number, a mail with no tag, and a request that gives each reader of a newsletter their own tag.
Return value
Returns ?string. The bounceTag option, or null when none was given.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Test\SentMail reads every part of a sent message: its Message-ID, the sender's name, the attachments, the thread headers, the unsubscribe link and the bounce tag. |
Examples
The examples below run with this configuration file.
# The examples send mail through this block. `capture = true` keeps each message# and sends nothing, so no mail leaves the computer. A bounce goes to `return_path`.[mail.shop]capture = truefrom = "shop@example.com"from_name = "Example Shop"return_path = "bounces@example.com"
# The program may send mail through the `shop` block.[capabilities.mail]send = ["shop"]The tag names the order
<?nvs// Shows how to test that an order mail has a bounce tag with the order number.// proof: test
use Core\Mail;use Core\Test;
class Order{ // Sends the confirmation with the bounce tag `order-1042`. If the address does not exist, // the bounce goes to `bounces+order-1042@example.com`, and the program knows which order failed. public static function confirm(string $email, int $number): void { Mail::send('shop', [$email], 'Order ' . $number . ' is confirmed', 'Thank you for your order.', { bounceTag: 'order-' . $number, }); }}
class OrderTest{ #[Test] public function theTagNamesTheOrder(): void { Order::confirm('anna@example.com', 1042);
Test::assertSame(Test::sentMail()[0]->bounceTag(), 'order-1042'); }} OrderTest ✓ theTagNamesTheOrder
0 failed, 1 passed, 0 skipped, 0 flakyA mail with no tag
<?nvs// Shows how to test that a mail without a bounce tag has none.// proof: test
use Core\Mail;use Core\Test;
class Contact{ // Sends a copy of a contact form to the shop team. No bounce tag is needed here. public static function forward(string $text): void { Mail::send('shop', ['team@example.com'], 'New contact form', $text); }}
class ContactTest{ #[Test] public function theCopyHasNoTag(): void { Contact::forward('Do you ship to Norway?');
// No `bounceTag` option was given, so the result is `null`. // A bounce then goes to `return_path` of the block, with no tag. Test::assertNull(Test::sentMail()[0]->bounceTag()); }} ContactTest ✓ theCopyHasNoTag
0 failed, 1 passed, 0 skipped, 0 flakyOne tag for each delivery
<?nvs// Shows how to test that a request gives each delivery of a newsletter its own bounce tag.// proof: test
use Core\Http\Method;use Core\Test;
class DeliveryTest{ #[Test] public function eachDeliveryHasItsOwnTag(): void { Test::request(Method::Post, '/newsletters/10/send');
// A bounce for `bo@example.com` goes to `bounces+news-10.reader-2@example.com`. // The program reads the tag and stops the newsletter for that reader. Test::assertSame(Test::sentMail()[0]->bounceTag(), 'news-10.reader-1'); Test::assertSame(Test::sentMail()[1]->bounceTag(), 'news-10.reader-2'); }}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.// It sends one message for each reader. The tag has the newsletter number and the reader number.if (Core\Env::context() == Core\Env::Context::Request) { array<string> $readers = ['1' => 'anna@example.com', '2' => 'bo@example.com']; foreach ($readers as string $number => string $email) { Core\Mail::send('shop', [$email], 'Newsletter 10', 'Ten new books are in the shop.', { bounceTag: 'news-10.reader-' . $number, }); } Core\Response::setStatus(202);} DeliveryTest ✓ eachDeliveryHasItsOwnTag
0 failed, 1 passed, 0 skipped, 0 flaky