Core\Mail\Message::to
$message->to(): array<Core\Mail\Address>Returns the recipients in the To header of a received message, as an array of Core\Mail\Address.
The addresses are in the order of the header. A message without a To header returns an empty array. A group is a name, a colon, a list of addresses and a ;. A group, such as Team: a@example.com, b@example.com;, gives one address for each member of the group. An entry that is not an address, such as <ada>, is not in the array.
Good to know: the sender writes the To header, and it lists the recipients the sender chose to show. Hidden copies (Bcc) are not in To, so the mail may have reached other people too. Each address and name is tainted. Tainted means that the value comes from outside your program, so you must check or escape it before you use it.
The examples below list every recipient, read the members of a group, and choose a team by the address that the message was sent to.
Return value
Returns array<Core\Mail\Address>. One address for each recipient, in order. The array is empty when there are none.
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
Every recipient
<?nvs// Shows how to list the recipients of a received message.
use Core\Mail\Address;use Core\Mail\Message;
Message $mail = Message::parse( "From: ada@example.org\r\n" . "To: Sales <sales@example.com>, billing@example.com\r\n" . "Subject: A quote for 40 chairs\r\n" . "\r\n" . 'Please send me a quote.',);
// `to` returns one address for each recipient, in the order of the header.// The second address has no name, so its `name()` is an empty string.foreach ($mail->to() as Address $recipient) { echo $recipient->address(), ' [', $recipient->name(), "]\n";}
// A message without a `To` header gives an empty array. This prints `0`.echo Core\Arr::count(Message::parse("Subject: Hi\r\n\r\nHello")->to()), "\n";sales@example.com [Sales]billing@example.com []0The members of a group
<?nvs// Shows that a group in the `To` header gives one address for each member.
use Core\Mail\Address;use Core\Mail\Message;
// A group is a name, a colon, a list of addresses and a `;`. Here the group is `Kitchen`.// The last address comes after the `;` that ends the group, so it is not in the group.Message $mail = Message::parse( "From: manager@example.com\r\n" . "To: Kitchen: ana@example.com, bo@example.com;, cleo@example.com\r\n" . "Subject: Menu for Saturday\r\n" . "\r\n" . 'The new menu is attached.',);
// This prints the three addresses. The name of the group is not part of the result.foreach ($mail->to() as Address $recipient) { echo $recipient->address(), "\n";}ana@example.combo@example.comcleo@example.comChoose a team by the address
<?nvs// Shows how a shop sends each message to the right team, by the address it was sent to.
use Core\Mail\Address;use Core\Mail\Message;
// Each address of the shop belongs to one team.array<string> $teams = ['sales@example.com' => 'Sales', 'support@example.com' => 'Support'];
array<string> $inbox = [ "To: Support <support@example.com>\r\nSubject: Broken lamp\r\n\r\nIt does not turn on.", "To: SALES@example.com\r\nSubject: 40 chairs\r\n\r\nPlease send a quote.", "To: someone@example.org\r\nSubject: Hello\r\n\r\nWrong address?",];
foreach ($inbox as string $raw) { Message $mail = Message::parse($raw); string $team = 'Unsorted'; foreach ($mail->to() as Address $recipient) { // An address is compared in lower case, because `SALES@example.com` is the same mailbox. tainted string $address = Core\Str::lower($recipient->address()); foreach ($teams as string $known => string $name) { if ($address == $known) { $team = $name; } } } // This prints `Support`, `Sales` and `Unsorted`. echo $mail->subject(), ': ', $team, "\n";}Broken lamp: Support40 chairs: SalesHello: Unsorted