Skip to content

Core\Mail\Address::address

$address->address(): string

Returns the email address of a Core\Mail\Address, such as ada@example.org, without the name.

You meet a Core\Mail\Address in two places. Core\Mail\Address::of makes one for a mail that your program sends. For a received Core\Mail\Message, the methods from(), to(), cc() and replyTo() return addresses. name() returns the name that is shown for the address.

address() 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 received message writes its addresses, so an address does not prove who sent the message. Upper and lower case are kept as the sender wrote them.

The examples below show the addresses of a received message, an address your program made, and how to find a customer by the address of a message.

Return value

Returns string. The address, without the name. For a received message, the address is tainted, because the sender wrote it.

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

The address of the sender

<?nvs
// Shows how to read the email address of the person who sent a message.
use Core\Mail\Address;
use Core\Mail\Message;
Message $reply = Message::parse(
"From: Ada Lovelace <ada@example.org>\n"
. "To: Shop <shop@example.com>, orders@example.com\n"
. "Subject: Re: Your order\n"
. "\n"
. "Thank you!\n",
);
// `from()` returns `null` when the message has no `From` address. This prints `ada@example.org`.
?Address $sender = $reply->from();
echo $sender?->address() ?? 'no sender', "\n";
// `address()` returns the address alone, without the name.
foreach ($reply->to() as Address $recipient) {
echo $recipient->address(), "\n";
}
Output
ada@example.org
shop@example.com
orders@example.com

An address you made

<?nvs
// Shows what `address()` returns for an address that your program made with `Address::of`.
use Core\Mail\Address;
Address $ada = Address::of('ada@example.org', 'Ada Lovelace');
// `address()` returns the first argument of `of`. This prints `ada@example.org`.
echo $ada->address(), "\n";
// The name is not part of it. `name()` returns the name.
echo $ada->name(), "\n";
Output
ada@example.org
Ada Lovelace

Find the customer of a message

<?nvs
// Shows how to find the customer who sent a message, by the sender's address.
use Core\Mail\Message;
// This array has the customer number for each email address of the shop. The addresses are in lower case.
array<int> $customers = ['ada@example.org' => 1042, 'bo@example.org' => 1043];
Message $mail = Message::parse("From: Ada <Ada@Example.org>\nSubject: My order\n\nWhere is it?");
// The sender may write the address in any case, so the program changes it to lower case first.
// The address is tainted (text from outside your program). Using it as an array key is allowed.
tainted string $key = Core\Str::lower($mail->from()?->address() ?? '');
echo 'customer ', $customers[$key] ?? 0, "\n";
Output
customer 1042