Core\Test\SentMail::fromName
$sentMail->fromName(): ?stringfromName() returns the sender name of a message. Mail programs show this name in the list of messages. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and fromName() is one of its methods.
The result is the fromName option that your program gave to Core\Mail::send. Without that option, the result is from_name of the [mail.<name>] block. When neither is set, the result is null. The sender address is always from of the block. fromName changes only the name.
Use fromName() to check that a mail shows the right sender. For example, a reply from a support agent can show the agent's name.
The examples below show the name from the settings, a name that the program gives, and a block with no name.
Return value
Returns ?string. The fromName option, or else the from_name of the [mail.<name>] block. null when neither is set.
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 these blocks. `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"
# This block has no `from_name`.[mail.alerts]capture = truefrom = "alerts@example.com"
# The program may send mail through both blocks.[capabilities.mail]send = ["shop", "alerts"]The name comes from the settings
<?nvs// Shows how to test the sender name when the program gives none.// proof: test
use Core\Mail;use Core\Test;
class Welcome{ // Sends a welcome mail with no `fromName` option. public static function send(string $email): void { Mail::send('shop', [$email], 'Welcome', 'Thank you for signing up.'); }}
class WelcomeTest{ #[Test] public function theNameIsTheShopName(): void { Welcome::send('anna@example.com');
// The name is `from_name` of the `[mail.shop]` block in `nvs.toml`. Test::assertSame(Test::sentMail()[0]->fromName(), 'Example Shop'); }} WelcomeTest ✓ theNameIsTheShopName
0 failed, 1 passed, 0 skipped, 0 flakyA person signs the mail
<?nvs// Shows how to test a mail that shows the name of the person who wrote it.// proof: test
use Core\Mail;use Core\Test;
class Support{ // Sends a reply from a support agent. `fromName` replaces the name of the block. // The address is still `from` of the block. public static function answer(string $email, string $agent, string $text): void { Mail::send('shop', [$email], 'About your order', $text, { fromName: $agent . ' at Example Shop', }); }}
class SupportTest{ #[Test] public function theAgentNameIsShown(): void { Support::answer('anna@example.com', 'Carla', 'Your parcel left today.');
Test::assertSame(Test::sentMail()[0]->fromName(), 'Carla at Example Shop'); }} SupportTest ✓ theAgentNameIsShown
0 failed, 1 passed, 0 skipped, 0 flakyA block with no name
<?nvs// Shows how to test that an alert has only a sender address and no sender name.// proof: test
use Core\Mail;use Core\Test;
class DiskAlert{ // Sends an alert through the `alerts` block. That block has no `from_name`. public static function send(int $percent): void { Mail::send('alerts', ['ops@example.com'], 'Disk almost full', 'The disk is ' . $percent . '% full.'); }}
class DiskAlertTest{ #[Test] public function theAlertHasNoName(): void { DiskAlert::send(93);
// Neither the call nor the block sets a name, so the result is `null`. Test::assertNull(Test::sentMail()[0]->fromName()); Test::assertSame(Test::sentMail()[0]->endpoint(), 'alerts'); }} DiskAlertTest ✓ theAlertHasNoName
0 failed, 1 passed, 0 skipped, 0 flaky