Skip to content

Core\Test\SentMail::replyTo

$sentMail->replyTo(): ?string

replyTo() returns the address that gets the answer when somebody replies to a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and replyTo() is one of its methods.

The result is the replyTo address that your program gave to Core\Mail::send. When the program gave no replyTo address, the result is null. A reply then goes to the from address of the mail block.

Use replyTo() to check where answers go. A mail from an address that nobody reads needs a replyTo address, or the answers are lost.

The examples below show a notice whose replies go to the support desk, an alert with no replyTo address, and a message between two users that a request sends.

Return value

Returns ?string. The replyTo address, or null when none was given.

Changelog

VersionChange
0.0.5Core\Test::sentMail returns the messages your program sent in the current test. Each one is a Core\Test\SentMail, and its methods return the values given to Core\Mail::send.

Examples

The examples below run with this configuration file.

nvs.toml
# The mail server for the examples. Nothing listens on port 1, so a real send
# would fail. Under `nvs test`, no message is sent and no connection is opened.
[mail.site]
host = "127.0.0.1"
port = 1
from = "hello@example.com"
timeout = "1s"
# The program may send mail through the `site` block.
[capabilities.mail]
send = ["site"]

Replies go to the support desk

<?nvs
// Shows how to test that the replies to a notice go to the support desk.
// proof: test
use Core\Mail;
use Core\Test;
class Maintenance
{
// Tells a customer about planned work. A customer with a question replies to the support desk.
public static function announce(string $customer, string $day): void
{
Mail::send('site', [$customer], 'Planned work on ' . $day, 'The site is closed for two hours.', {
replyTo: 'support@example.com',
});
}
}
class MaintenanceTest
{
#[Test]
public function repliesGoToTheSupportDesk(): void
{
Maintenance::announce('ada@example.com', 'Sunday');
Test::assertSame(Test::sentMail()[0]->replyTo(), 'support@example.com');
}
}
Output
MaintenanceTest
✓ repliesGoToTheSupportDesk
0 failed, 1 passed, 0 skipped, 0 flaky

An alert has no reply address

<?nvs
// Shows how to test a mail that has no reply address.
// proof: test
use Core\Mail;
use Core\Test;
class DiskAlert
{
// Tells the operators that a disk is almost full.
public static function send(int $percentUsed): void
{
Mail::send('site', ['ops@example.com'], 'Disk ' . $percentUsed . '% full', 'Please free some space.');
}
}
class DiskAlertTest
{
#[Test]
public function theAlertHasNoReplyAddress(): void
{
DiskAlert::send(93);
// The program gave no `replyTo` address, so the result is `null`.
Test::assertNull(Test::sentMail()[0]->replyTo());
}
}
Output
DiskAlertTest
✓ theAlertHasNoReplyAddress
0 failed, 1 passed, 0 skipped, 0 flaky

A buyer writes to a seller

<?nvs
// Shows how to test that a seller's reply goes to the buyer who wrote the message.
// proof: test
use Core\Http\Method;
use Core\Test;
class MarketMessageTest
{
// The form has a second address in it. The reply must still go to the buyer's saved address.
#[Test]
public function theReplyGoesToTheBuyer(): void
{
Test::request(Method::Post, '/messages', {
headers: ['content-type' => 'application/x-www-form-urlencoded'],
body: 'buyer=ada&seller=bo&replyTo=someone-else@example.com',
});
Test::assertCount(Test::sentMail(), 1);
var $mail = Test::sentMail()[0];
Test::assertEquals($mail->to(), ['bo@example.com']);
Test::assertSame($mail->replyTo(), 'ada@example.com');
}
}
// `Core\Test::request` runs this file again for the request. This part finds the
// saved addresses of both users. It does not read the `replyTo` field of the form.
if (Core\Env::context() == Core\Env::Context::Request) {
array<string> $saved = ['ada' => 'ada@example.com', 'bo' => 'bo@example.com'];
?string $buyer = $saved[Core\Request::formField('buyer') ?? ''] ?? null;
?string $seller = $saved[Core\Request::formField('seller') ?? ''] ?? null;
if ($buyer != null && $seller != null) {
Core\Mail::send('site', [$seller], 'A question about your item', 'Is it still for sale?', {
replyTo: $buyer,
});
}
Core\Response::setStatus(202);
}
Output
MarketMessageTest
✓ theReplyGoesToTheBuyer
0 failed, 1 passed, 0 skipped, 0 flaky