Core\Mail\Address::of
Core\Mail\Address::of(string $address, string $name): Core\Mail\AddressCore\Mail\Address::of( string $address, string $name, ): Core\Mail\Address
Core\Mail\Address::of returns an email address with a name, such as "Ada Lovelace" <ada@example.com>. A mail program shows the name in place of the address.
Pass the result to Core\Mail::send as a recipient, a cc, a bcc or the replyTo address.
The first argument is the address only. of checks it at once, with the same checks as send. An empty address, or an address with a space, a line break or a second @, throws a RuntimeError. A name inside the address string, such as Ada <ada@example.com>, also throws. Pass the name as the second argument.
The name can be any text, including text a user typed. It is encoded before it is sent, so a line break or a comma in it is part of the name. It cannot add a header or a recipient. An empty name sends the address alone.
A domain with letters that are not ASCII, such as bücher.example, is sent in its xn-- form.
The examples below show a recipient with a name, a name inside the address, and a test of a name from a form.
Parameters
$addressstringThe email address, such asada@example.com. A domain with letters that are not ASCII is sent in itsxn--form.$namestringThe name to show, such asAda Lovelace. It can be any text, including text a user typed. An empty name sends the address alone.
Return value
Returns Core\Mail\Address. The address and its name, to pass to Core\Mail::send.
Errors
RuntimeError$addressis not an email address. A string such asAda <ada@example.com>is not an address either: pass the name as$name.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Address::of is an email address with a display name, such as "Ada Lovelace" <ada@example.com>. A plain string is always an address alone. |
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 recipient with a name
<?nvs// Shows how to send a mail to an address with a name.
use Core\Mail\Address;
// The mail program of the recipient shows `Ada Lovelace` in place of the address.Address $ada = Address::of('ada@example.com', 'Ada Lovelace');
// A named address can be a recipient, a `cc`, a `bcc` or the `replyTo` address.Core\Mail::send('shop', [$ada], 'Welcome', 'Your account is ready.', { replyTo: Address::of('help@example.com', 'Example Shop Help'),});echo "The welcome mail is sent.\n";The welcome mail is sent.A name inside the address
<?nvs// Shows what happens when the name is written inside the address string.
// `Address::of` reads its first argument as an address only. `<` is not allowed in an address,// so the call throws a `RuntimeError`. Pass the name as the second argument.try { Core\Mail\Address::of('Ada Lovelace <ada@example.com>', 'Ada Lovelace'); echo "created\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Mail\Address::of: `$address` is not an address — it contains a character an address may not contain: a control character, a space, or one of `< > , ; : ( ) " \ %`A name from a form
<?nvs// Shows how to test that a name a user typed is shown as a name, and never as a second address.// proof: test
use Core\Mail\Address;use Core\Test;
class SignupTest{ #[Test] public function theNameIsOnlyAName(): void { // The name has a comma and a second address. It can be any text. string $typed = 'Anna, bo@example.com'; Core\Mail::send('shop', [Address::of('anna@example.com', $typed)], 'Welcome', 'Hello.');
// The mail has one recipient. The comma and the second address are part of the name. Test::assertEquals(Test::sentMail()[0]->to(), ['anna@example.com']); }} SignupTest ✓ theNameIsOnlyAName
0 failed, 1 passed, 0 skipped, 0 flaky