Core\Mail\Message::subject
$message->subject(): stringReturns the subject of a received message, as a string.
A mail program writes a subject with letters outside ASCII as an encoded word, such as =?UTF-8?B?...?=. An encoded word is the way a header writes text that is not ASCII. subject decodes it, so the result is the text that the sender typed. A long subject can continue on the next line, which starts with a space. subject replaces each line break and the spaces around it with one space. A message without a Subject header returns an empty string.
Good to know: the subject is tainted, because the sender wrote it. Tainted means that the value comes from outside your program, so you must check or escape it before you use it. The subject can contain HTML, so escape it before you put it on a page.
The examples below read an encoded subject, find an order number in a subject, and show a subject on a web page.
Return value
Returns string. The subject, or an empty string when the message has 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
An encoded subject
<?nvs// Shows that `subject` decodes a subject with letters outside ASCII.
use Core\Mail\Message;
// The sender's mail program wrote `Crème brûlée for 4` as an encoded word.// An encoded word is the way a header writes text that is not ASCII.Message $mail = Message::parse( "From: ada@example.org\r\n" . "Subject: =?UTF-8?Q?Cr=C3=A8me_br=C3=BBl=C3=A9e_for_4?=\r\n" . "\r\n" . 'Can I order it for Saturday?',);
// This prints `Crème brûlée for 4`.echo $mail->subject(), "\n";
// A message without a subject gives an empty string. This prints `[]`.echo '[', Message::parse("From: ada@example.org\r\n\r\nHi")->subject(), "]\n";Crème brûlée for 4[]An order number in the subject
<?nvs// Shows how a shop finds the order number in the subject of a customer's reply.
use Core\Mail\Message;
array<string> $subjects = ['Re: Your order #1042', 'RE: Re: Your order #977', 'Question about returns'];
foreach ($subjects as string $line) { Message $mail = Message::parse("From: ada@example.org\r\nSubject: {$line}\r\n\r\nHello");
// `Core\Str::after` returns the text after the first `#`. Without a `#`, the result is `null`. // `as ?int` converts it to a whole number. If that is not possible, the result is `null`. ?int $order = Core\Str::after($mail->subject(), '#') as ?int; echo $mail->subject(), ' -> ', $order ?? 'no order', "\n";}Re: Your order #1042 -> 1042RE: Re: Your order #977 -> 977Question about returns -> no orderThe subject on a web page
<?nvs// Shows how a help desk page lists the subject of a message that has HTML in it.
use Core\Html\Markup;use Core\Mail\Message;
Message $mail = Message::parse( "From: ada@example.org\r\n" . "Subject: <img src=x onerror=alert(1)> Refund\r\n" . "\r\n" . 'I would like a refund.',);
// The subject is tainted, because the sender wrote it. You must escape it before you show it.// An html`...` template escapes it, so the tag shows as text.Markup $row = html`<td>{$mail->subject()}</td>`;
// This prints `<td><img src=x onerror=alert(1)> Refund</td>`.echo $row, "\n";<td><img src=x onerror=alert(1)> Refund</td>