Skip to content

Core\Test\SentMail::messageId

$sentMail->messageId(): string

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

Core\Mail::send writes a new message id into every message, and it returns that id. messageId() returns the same string. It has no angle brackets. It looks like 3f2a…@example.com: 32 random hex digits, @, and the domain of the from address of the mail block.

A program stores the id when it needs to find the message again. A reply to the message contains this id, and so does a bounce. Pass the id to the inReplyTo option to send a reply in the same thread.

Use messageId() to check that your program stored the id of the message it sent.

The examples below show an order that keeps the id of its confirmation, two messages with different ids, and a receipt that a request sends after its response.

Return value

Returns string. The same string Core\Mail::send returned for this message.

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"]

The order keeps the id of its mail

<?nvs
// Shows how to test that an order keeps the message id of its confirmation mail.
// proof: test
use Core\Mail;
use Core\Test;
class Order
{
public string $confirmationId = '';
// Sends the confirmation. `Mail::send` returns the message id, and the order keeps it.
// A reply from the customer has this id, so the program can find the order again.
public function confirm(string $email): void
{
$this->confirmationId = Mail::send('shop', [$email], 'Order 1042 is confirmed', 'Thank you for your order.');
}
}
class OrderTest
{
#[Test]
public function theOrderKeepsTheId(): void
{
Order $order = new Order();
$order->confirm('anna@example.com');
// `messageId()` returns the same id that `Mail::send` returned.
Test::assertSame(Test::sentMail()[0]->messageId(), $order->confirmationId);
}
}
Output
OrderTest
✓ theOrderKeepsTheId
0 failed, 1 passed, 0 skipped, 0 flaky

Every message gets a new id

<?nvs
// Shows how to test that every message gets its own id, at the domain of the sender address.
// proof: test
use Core\Mail;
use Core\Str;
use Core\Test;
class Reminder
{
// Sends the same reminder to each address, as one message per address.
public static function sendAll(array<string> $emails): void
{
foreach ($emails as string $email) {
Mail::send('shop', [$email], 'Your cart is waiting', 'You left 2 items in your cart.');
}
}
}
class ReminderTest
{
#[Test]
public function eachMessageHasItsOwnId(): void
{
Reminder::sendAll(['anna@example.com', 'bo@example.com']);
string $first = Test::sentMail()[0]->messageId();
string $second = Test::sentMail()[1]->messageId();
// The two messages have different ids.
Test::assertTrue($first != $second);
// The part after `@` is the domain of `from` in the `[mail.shop]` block.
Test::assertTrue(Str::endsWith($first, '@example.com'));
}
}
Output
ReminderTest
✓ eachMessageHasItsOwnId
0 failed, 1 passed, 0 skipped, 0 flaky

A receipt sent after the response

<?nvs
// Shows how to test a receipt that a request sends after its response.
// proof: test
use Core\Http\Method;
use Core\Test;
class ReceiptTest
{
#[Test]
public function theReceiptIdIsInTheLog(): void
{
// `Test::request` returns only when the work that runs after the response is done.
// So the receipt is already in `Test::sentMail()`.
Core\Test\Response $response = Test::request(Method::Post, '/orders/1042/pay');
Test::assertSame($response->body(), "Order 1042 is paid.\n");
Test::assertCount(Test::sentMail(), 1);
// The request wrote the message id to the log. It is the id of the sent receipt.
string $id = Test::sentMail()[0]->messageId();
Test::assertSame(Test::logs()[0]->fields()['messageId'], $id);
}
}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.
// It prints the response. `Core\Task::runAfterResponse` then sends the receipt, so the customer does not wait.
if (Core\Env::context() == Core\Env::Context::Request) {
echo "Order 1042 is paid.\n";
Core\Task::runAfterResponse(fn(): void => {
string $id = Core\Mail::send('shop', ['anna@example.com'], 'Receipt for order 1042', 'You paid 25.00 EUR.');
Core\Log::write(Core\Log::Level::Info, 'Receipt sent', ['messageId' => $id]);
});
}
Output
ReceiptTest
✓ theReceiptIdIsInTheLog
0 failed, 1 passed, 0 skipped, 0 flaky