Core\Mail\Address::name
$address->name(): stringReturns the name that is shown for an email address, such as Ada Lovelace. An address with no name gives an empty string.
For an address that Core\Mail\Address::of made, the name is its second argument. For an address of a received message, the name is the text before the address in the header, such as Ada Lovelace in Ada Lovelace <ada@example.org>. A name with letters that are not ASCII is often an encoded word (the way a header writes non-ASCII text), such as =?UTF-8?Q?...?=. name() decodes it.
name() returns a tainted string. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. Escape it with Core\Html::escape before you put it on a page. It cannot be part of a file path or an SQL query without a check.
The sender of a message can write any name, so the name does not prove who sent the message.
The examples below show how to greet the sender by name, an address with no name, and an encoded name.
Return value
Returns string. The name, or an empty string when the address has no name.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Message::parse reads a raw email, such as an .eml file or the body of a webhook. It returns the headers, the text and HTML bodies and the attachments, and a broken message does not throw an error. Every value is tainted: it comes from outside your program, so you must check or escape it. |
Examples
Greet the sender by name
<?nvs// Shows how to read the name of the person who sent a message, to greet them in the answer.
use Core\Mail\Address;use Core\Mail\Message;
Message $question = Message::parse( "From: Ada Lovelace <ada@example.org>\n" . "Subject: Opening hours\n" . "\n" . "Are you open on Sunday?\n",);
// `name()` returns the name before the address. This prints `Hello Ada Lovelace,`.?Address $sender = $question->from();echo 'Hello ', $sender?->name() ?? '', ",\n";Hello Ada Lovelace,An address without a name
<?nvs// Shows what `name()` returns for an address that has no name.
use Core\Mail\Address;use Core\Mail\Message;
Message $mail = Message::parse("From: bo@example.org\nSubject: Hi\n\nHello");?Address $sender = $mail->from();
// The `From` header has only an address, so `name()` returns an empty string.// The program shows the address in its place.tainted string $shown = $sender?->name() ?? '';if ($shown == '') { $shown = $sender?->address() ?? 'unknown sender';}echo 'From: ', $shown, "\n";From: bo@example.orgAn encoded name is decoded
<?nvs// Shows that `name()` decodes a name that a mail program encoded.
use Core\Mail\Address;use Core\Mail\Message;
// A name with letters that are not ASCII is sent as an encoded word, such as `=?UTF-8?Q?...?=`.// An encoded word is the way a header writes non-ASCII text.Message $mail = Message::parse( "From: =?UTF-8?Q?J=C3=BCrgen_M=C3=BCller?= <juergen@example.org>\n" . "Subject: Order 42\n" . "\n" . "Hello\n",);
// `name()` returns the decoded name. This prints `Jürgen Müller`.?Address $sender = $mail->from();echo $sender?->name() ?? '', "\n";Jürgen Müller