Skip to content

Core\Test\SentMail::unsubscribe

$sentMail->unsubscribe(): ?string

unsubscribe() returns the unsubscribe URL of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and unsubscribe() is one of its methods.

The result is the unsubscribe option that your program gave to Core\Mail::send. When the program gave no unsubscribe option, the result is null.

With this option, mail programs show an unsubscribe button. When the reader clicks it, the mail program sends a POST request to the URL. The reader does not see a page. Your program must handle that request and stop the mail for this reader. Large mail providers expect this link in every newsletter.

The URL must start with https://. Core\Mail::send throws an error for any other URL, and for a URL with a space or a line break.

Use unsubscribe() to check that a newsletter has the link, and that each reader gets their own.

The examples below show a newsletter with the link, an order mail with no link, and a request that sends each reader their own link.

Return value

Returns ?string. The unsubscribe option, or null when none was given.

Changelog

VersionChange
0.0.6Core\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.

nvs.toml
# The examples send mail through this block. `capture = true` keeps each message
# and sends nothing, so no mail leaves the computer.
[mail.shop]
capture = true
from = "shop@example.com"
from_name = "Example Shop"
# The program may send mail through the `shop` block.
[capabilities.mail]
send = ["shop"]

A newsletter has an unsubscribe link

<?nvs
// Shows how to test that a newsletter has a one-click unsubscribe link.
// proof: test
use Core\Mail;
use Core\Test;
class Newsletter
{
// Sends the newsletter. Mail programs show an unsubscribe button for the `unsubscribe` URL.
public static function send(string $email, string $token): void
{
Mail::send('shop', [$email], 'New in October', 'Ten new books are in the shop.', {
unsubscribe: 'https://example.com/unsubscribe/' . $token,
});
}
}
class NewsletterTest
{
#[Test]
public function theNewsletterHasTheLink(): void
{
Newsletter::send('anna@example.com', 'a81f');
Test::assertSame(Test::sentMail()[0]->unsubscribe(), 'https://example.com/unsubscribe/a81f');
}
}
Output
NewsletterTest
✓ theNewsletterHasTheLink
0 failed, 1 passed, 0 skipped, 0 flaky

An order mail has no unsubscribe link

<?nvs
// Shows how to test that an order confirmation has no unsubscribe link.
// proof: test
use Core\Mail;
use Core\Test;
class Order
{
// A customer cannot unsubscribe from the confirmation of their own order.
public static function confirm(string $email): void
{
Mail::send('shop', [$email], 'Order 1042 is confirmed', 'Thank you for your order.');
}
}
class OrderTest
{
#[Test]
public function theConfirmationHasNoLink(): void
{
Order::confirm('anna@example.com');
// No `unsubscribe` option was given, so the result is `null`.
Test::assertNull(Test::sentMail()[0]->unsubscribe());
}
}
Output
OrderTest
✓ theConfirmationHasNoLink
0 failed, 1 passed, 0 skipped, 0 flaky

Each reader gets their own link

<?nvs
// Shows how to test that a request sends each reader a newsletter with their own unsubscribe link.
// proof: test
use Core\Http\Method;
use Core\Test;
class SendNewsletterTest
{
#[Test]
public function eachReaderHasTheirOwnLink(): void
{
Test::request(Method::Post, '/newsletters/10/send');
Test::assertCount(Test::sentMail(), 2);
Test::assertSame(Test::sentMail()[0]->unsubscribe(), 'https://example.com/unsubscribe?reader=1');
Test::assertSame(Test::sentMail()[1]->unsubscribe(), 'https://example.com/unsubscribe?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 link has the number of the reader.
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.', {
unsubscribe: 'https://example.com/unsubscribe?reader=' . $number,
});
}
Core\Response::setStatus(202);
}
Output
SendNewsletterTest
✓ eachReaderHasTheirOwnLink
0 failed, 1 passed, 0 skipped, 0 flaky