Core\Mail\Message::header
Returns every value of one header of a received message, such as List-Id or Received.
The result is an array with one string for each line of that header, in the order of the message. A header the message does not have gives an empty array. Upper and lower case in the name are the same, so List-Id and list-id return the same values. An encoded word (the way a header writes non-ASCII text), such as =?UTF-8?B?...?=, is decoded.
Only the headers of the message itself are read. The headers of its parts are not read. A part is one piece of a message, such as the text, the HTML or a file. The first 1,000 header lines are read.
The sender wrote every header, so each value is 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.
Most headers also have a method of their own, such as subject() or references().
The examples below show a header with one and with several lines, a missing header, and an encoded value.
Parameters
$namestringThe header name, such asList-Id. Upper and lower case are the same.
Return value
Returns array<string>. One string for each line of that header. The array is empty when there is 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
Read a header by its name
<?nvs// Shows how to read a header that has no method of its own, such as `List-Id`.
use Core\Mail\Message;
Message $post = Message::parse( "From: Forum <forum@example.com>\n" . "Subject: New answer in your topic\n" . "List-Id: Shop forum <forum.example.com>\n" . "Received: from mx1.example.net\n" . "Received: from mx2.example.net\n" . "\n" . "Someone answered your question.\n",);
// `List-Id` names the mailing list that a message came from. `header()` returns one value for each line.// This prints `Shop forum <forum.example.com>`.echo $post->header('List-Id')[0], "\n";
// A `Received` header has one line for each server that the message passed.// This message passed two mail servers, so it has two `Received` lines.foreach ($post->header('Received') as var $server) { echo $server, "\n";}Shop forum <forum.example.com>from mx1.example.netfrom mx2.example.netA header that is missing
<?nvs// Shows how `header()` treats upper and lower case, and a header that the message does not have.
use Core\Mail\Message;
Message $order = Message::parse( "From: Ada <ada@example.org>\n" . "X-Order: 1042\n" . "\n" . "Please send the invoice again.\n",);
// Upper and lower case in the name are the same. This prints `1042` two times.echo $order->header('X-Order')[0], "\n";echo $order->header('x-order')[0], "\n";
// The message has no `X-Priority` header, so the array is empty. This prints `0`.echo Core\Arr::count($order->header('X-Priority')), "\n";104210420An encoded header is decoded
<?nvs// Shows that `header()` decodes a value that a mail program encoded.
use Core\Mail\Message;
// A header with letters that are not ASCII is sent as an encoded word, such as `=?UTF-8?B?...?=`.// An encoded word is the way a header writes non-ASCII text.Message $ticket = Message::parse( "From: shop@example.com\n" . "X-Customer: =?UTF-8?B?SsO8cmdlbiBNw7xsbGVy?=\n" . "\n" . "A new ticket.\n",);
// `header()` returns the decoded text. This prints `Jürgen Müller`.// The value is a tainted string (text from outside your program), because the sender wrote it.echo $ticket->header('X-Customer')[0], "\n";Jürgen Müller