Core\Test\SentMail::inReplyTo
$sentMail->inReplyTo(): ?stringinReplyTo() returns the message id of the message that this message replies to. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and inReplyTo() is one of its methods.
The result is the inReplyTo option that your program gave to Core\Mail::send. When the program gave no inReplyTo option, the result is null. The id has no angle brackets. It is the same string that Core\Mail::send returned for the first message.
Mail programs use this id to show a message and its reply in one thread. Most programs also read the references option, so a reply usually sets both. Core\Mail::send throws an error when the value is not a message id, such as a value with a space or a line break.
Use inReplyTo() to check that a reply belongs to the right thread.
The examples below show a reply to a support ticket, a new message that starts its own thread, and a request that replies to a question in the same thread.
Return value
Returns ?string. The inReplyTo 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.[mail.shop]capture = truefrom = "shop@example.com"from_name = "Example Shop"
# The program may send mail through the `shop` block.[capabilities.mail]send = ["shop"]A reply in the same thread
<?nvs// Shows how to test that a reply to a support ticket is in the same thread as the ticket mail.// proof: test
use Core\Mail;use Core\Test;
class Ticket{ public string $firstId = '';
// Sends the first mail of the ticket. `Mail::send` returns its message id, and the ticket keeps it. public function open(string $email): void { $this->firstId = Mail::send('shop', [$email], 'Ticket 381: my parcel is late', 'We got your question.'); }
// Sends a reply. `inReplyTo` and `references` name the first mail, // so mail programs show both messages in one thread. public function answer(string $email, string $text): void { Mail::send('shop', [$email], 'Re: Ticket 381: my parcel is late', $text, { inReplyTo: $this->firstId, references: [$this->firstId], }); }}
class TicketTest{ #[Test] public function theAnswerNamesTheFirstMail(): void { Ticket $ticket = new Ticket(); $ticket->open('anna@example.com'); $ticket->answer('anna@example.com', 'Your parcel arrives tomorrow.');
// The reply is the second message. `inReplyTo()` returns the id of the first message. Test::assertSame(Test::sentMail()[1]->inReplyTo(), Test::sentMail()[0]->messageId()); Test::assertEquals(Test::sentMail()[1]->references(), [Test::sentMail()[0]->messageId()]); }} TicketTest ✓ theAnswerNamesTheFirstMail
0 failed, 1 passed, 0 skipped, 0 flakyA new message answers nothing
<?nvs// Shows how to test that a new message does not belong to an earlier thread.// proof: test
use Core\Mail;use Core\Test;
class Signup{ // Sends the first mail to a new customer. It does not reply to an earlier message. public static function welcome(string $email): void { Mail::send('shop', [$email], 'Welcome to Example Shop', 'Thank you for signing up.'); }}
class SignupTest{ #[Test] public function theWelcomeMailStartsANewThread(): void { Signup::welcome('bo@example.com');
// No `inReplyTo` option was given, so the result is `null`. Test::assertNull(Test::sentMail()[0]->inReplyTo()); }} SignupTest ✓ theWelcomeMailStartsANewThread
0 failed, 1 passed, 0 skipped, 0 flakyA request answers a question
<?nvs// Shows how to test that a request replies to a customer's mail in the same thread.// proof: test
use Core\Http\Method;use Core\Test;
class QuestionTest{ #[Test] public function theAnswerIsInTheThread(): void { Test::request(Method::Post, '/questions/55/answer', { headers: ['content-type' => 'application/x-www-form-urlencoded'], body: 'text=Yes%2C+we+ship+to+Norway.', });
Test::assertSame(Test::sentMail()[0]->inReplyTo(), 'q55.7f3a@mail.example.net'); Test::assertSame(Test::sentMail()[0]->text(), 'Yes, we ship to Norway.'); }}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.// The program saved the message id of the customer's question. The reply passes it to `inReplyTo`.if (Core\Env::context() == Core\Env::Context::Request) { array<string> $questionIds = ['55' => 'q55.7f3a@mail.example.net']; string $id = $questionIds['55']; Core\Mail::send('shop', ['anna@example.com'], 'Re: Do you ship to Norway?', Core\Request::formField('text') ?? '', { inReplyTo: $id, }); Core\Response::setStatus(202);} QuestionTest ✓ theAnswerIsInTheThread
0 failed, 1 passed, 0 skipped, 0 flaky