Skip to content

Core\Mail\Message::from

$message->from(): ?Core\Mail\Address

Returns the sender of a received message: the first address of its From header.

The result is a Core\Mail\Address. Its address() is the email address, and its name() is the name before the address, such as Ada Lovelace. A name in another alphabet is sent as an encoded word (the way a header writes non-ASCII text), and from() decodes it. A message without a From address returns null.

Good to know: the sender writes the From header, and nothing checks it. Anybody can write From: ceo@example.com. So do not trust the address to decide who may do something.

Both the address and the name are tainted. 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.

The examples below read the sender and the name, read a name in another alphabet, and show a sender on a web page.

Return value

Returns ?Core\Mail\Address. The address and its name, or null when the message has no From address.

Changelog

VersionChange
0.0.6Core\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

Who sent the message

<?nvs
// Shows how to read the sender of a received message.
use Core\Mail\Address;
use Core\Mail\Message;
Message $mail = Message::parse(
"From: Ada Lovelace <ada@example.org>\r\n"
. "Subject: Table for two\r\n"
. "\r\n"
. 'Do you have a table for two on Friday?',
);
// `from` returns the address and the name before the address.
// This prints `Ada Lovelace` and `ada@example.org`.
?Address $sender = $mail->from();
echo 'Name: ', $sender?->name() ?? '', "\n";
echo 'Address: ', $sender?->address() ?? '', "\n";
// A message without a `From` header gives `null`. This prints `unknown sender`.
echo Message::parse("Subject: Hi\r\n\r\nHello")->from()?->address() ?? 'unknown sender', "\n";
Output
Name: Ada Lovelace
Address: ada@example.org
unknown sender

A name in another alphabet

<?nvs
// Shows that a sender's name in another alphabet is decoded.
use Core\Mail\Message;
// A mail program writes a name with letters outside ASCII as an encoded word, such as `=?UTF-8?B?...?=`.
// An encoded word is the way a header writes non-ASCII text.
Message $mail = Message::parse(
"From: =?UTF-8?B?0JDQvdC90LAg0J/QsNCy0LvQvtCy0LA=?= <anna@example.org>\r\n"
. "Subject: Order 1042\r\n"
. "\r\n"
. 'Hello.',
);
// `name()` returns the decoded name. This prints `Анна Павлова`.
echo $mail->from()?->name() ?? '', "\n";
// The address is not encoded. This prints `anna@example.org`.
echo $mail->from()?->address() ?? '', "\n";
Output
Анна Павлова
anna@example.org

The sender on a web page

<?nvs
// Shows how a help desk page lists the sender of each new message.
use Core\Html\Markup;
use Core\Mail\Message;
// The sender chose this name, and it has HTML in it.
Message $mail = Message::parse(
"From: \"<b>Bo</b>\" <bo@example.org>\r\n"
. "Subject: Refund\r\n"
. "\r\n"
. 'I would like a refund.',
);
// The name and the address are tainted. Tainted means that the value comes from outside your program.
// An html`...` template escapes every value in it, so the name shows as text and not as tags.
tainted string $name = $mail->from()?->name() ?? '';
tainted string $address = $mail->from()?->address() ?? '';
Markup $row = html`<li>{$name} &lt;{$address}&gt;</li>`;
// This prints `<li>&lt;b&gt;Bo&lt;/b&gt; &lt;bo@example.org&gt;</li>`.
echo $row, "\n";
Output
<li>&lt;b&gt;Bo&lt;/b&gt; &lt;bo@example.org&gt;</li>