Skip to content

Core\Test\SentMail::references

$sentMail->references(): array<string>

references() returns the message ids of the earlier messages in a thread. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and references() is one of its methods.

The result is the references option that your program gave to Core\Mail::send, in the same order. The oldest message comes first. When the program gave no references option, the list is empty. Each id has no angle brackets, in the same form that Core\Mail::send returns.

Mail programs use this list to put a message in the right thread, even when a message in the middle is missing. A reply usually sets inReplyTo to the last id of the list. Core\Mail::send throws an error when an item of the list is not a message id.

Use references() to check that a new message lists the whole thread.

The examples below show a thread of three messages, a password reset that is not in a thread, and a comment that a request adds to a thread.

Return value

Returns array<string>. The references option, in the order given. The list is empty when none were 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 thread of three messages

<?nvs
// Shows how to test that each new message of a thread lists every earlier message, oldest first.
// proof: test
use Core\Mail;
use Core\Test;
class Thread
{
// The message ids of the thread, oldest first.
public array<string> $ids = [];
// Sends the first message of the thread, and keeps its id.
public function start(string $email, string $text): void
{
$this->ids[] = Mail::send('shop', [$email], 'Your return', $text);
}
// Sends the next message. `references` lists every earlier message,
// and `inReplyTo` names the last one. Then the thread keeps the new id.
public function add(string $email, string $text): void
{
string $last = $this->ids[Core\Arr::count($this->ids) - 1];
$this->ids[] = Mail::send('shop', [$email], 'Re: Your return', $text, {
inReplyTo: $last,
references: $this->ids,
});
}
}
class ThreadTest
{
#[Test]
public function eachMessageListsTheEarlierOnes(): void
{
Thread $thread = new Thread();
$thread->start('anna@example.com', 'We got your return.');
$thread->add('anna@example.com', 'We checked the item.');
$thread->add('anna@example.com', 'We paid back 25.00 EUR.');
// The first message has no earlier message, so the list is empty.
Test::assertEquals(Test::sentMail()[0]->references(), []);
// The third message lists the first two, oldest first.
Test::assertEquals(Test::sentMail()[2]->references(), [$thread->ids[0], $thread->ids[1]]);
}
}
Output
ThreadTest
✓ eachMessageListsTheEarlierOnes
0 failed, 1 passed, 0 skipped, 0 flaky

A password reset is not in a thread

<?nvs
// Shows how to test that a password reset mail is not part of a thread.
// proof: test
use Core\Mail;
use Core\Test;
class PasswordReset
{
// Sends the reset link. Each reset is a new message, with no earlier messages.
public static function send(string $email, string $token): void
{
Mail::send('shop', [$email], 'Reset your password', 'Open https://example.com/reset/' . $token);
}
}
class PasswordResetTest
{
#[Test]
public function theResetMailHasNoReferences(): void
{
PasswordReset::send('bo@example.com', 'k3v9');
// No `references` option was given, so the list is empty.
Test::assertEquals(Test::sentMail()[0]->references(), []);
}
}
Output
PasswordResetTest
✓ theResetMailHasNoReferences
0 failed, 1 passed, 0 skipped, 0 flaky

A request adds a comment to a thread

<?nvs
// Shows how to test that a comment a request sends lists the earlier messages of its thread.
// proof: test
use Core\Http\Method;
use Core\Test;
class CommentTest
{
#[Test]
public function theCommentListsTheThread(): void
{
Test::request(Method::Post, '/issues/12/comments');
Core\Test\SentMail $mail = Test::sentMail()[0];
Test::assertEquals($mail->references(), ['issue-12@example.com', 'comment-1@example.com']);
// The last id of the list is also the message that this comment replies to.
Test::assertSame($mail->inReplyTo(), 'comment-1@example.com');
}
}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.
// The program saved the ids of two earlier messages. The new comment passes them to `references`.
if (Core\Env::context() == Core\Env::Context::Request) {
array<string> $earlier = ['issue-12@example.com', 'comment-1@example.com'];
Core\Mail::send('shop', ['team@example.com'], 'Re: Issue 12: the search is slow', 'It is fast again.', {
inReplyTo: $earlier[1],
references: $earlier,
});
Core\Response::setStatus(202);
}
Output
CommentTest
✓ theCommentListsTheThread
0 failed, 1 passed, 0 skipped, 0 flaky