Core\Mail\Message::html
$message->html(): ?stringReturns the HTML body of a received message, or null when the message has no HTML part.
The sender wrote this HTML, so html() returns a tainted string. Tainted means that the value comes from outside your program. A tainted string cannot be put on a page as HTML. Pass it to Core\Html::sanitize first. It keeps simple formatting, such as paragraphs, bold text and links, and it removes scripts.
The HTML is decoded. Mail often encodes a body for sending, for example as quoted-printable. html() returns the HTML as normal text.
Many messages have a plain text body as well. text() returns it.
For an Outlook .msg file, the result is its HTML body. Outlook sometimes saves the HTML inside a rich text (RTF) body instead. Then the result is that HTML. When the file has neither, it is null.
The examples below show an HTML reply made safe for a page, a message with no HTML, and a decoded HTML body.
Return value
Returns ?string. The HTML, or null when the message has no HTML part. For an Outlook file, it is the HTML body. Outlook sometimes saves the HTML inside a rich text (RTF) body instead, and then it is that HTML.
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
Show a reply on a page
<?nvs// Shows how to put the HTML body of a received message on a page.
use Core\Mail\Message;
// A customer sent this reply. It has a plain text body and an HTML body.Message $reply = Message::parse( "From: Ada <ada@example.org>\n" . "Subject: Re: Your order\n" . "MIME-Version: 1.0\n" . "Content-Type: multipart/alternative; boundary=\"b1\"\n" . "\n" . "--b1\n" . "Content-Type: text/plain\n" . "\n" . "Thank you, the mug arrived.\n" . "--b1\n" . "Content-Type: text/html\n" . "\n" . "<p>Thank you, the <b>mug</b> arrived.</p><script>alert(1)</script>\n" . "--b1--\n",);
// `html()` returns a tainted string (text from outside your program). It cannot be put on a page as HTML.// `Core\Html::sanitize` keeps the `p` and `b` tags and removes the script.echo Core\Html::sanitize($reply->html() ?? '');<p>Thank you, the <b>mug</b> arrived.</p>A message with only text
<?nvs// Shows what `html()` returns for a message that has no HTML body.
use Core\Mail\Message;
Message $note = Message::parse( "From: Bo <bo@example.org>\n" . "Subject: Opening hours\n" . "\n" . "Are you open on Sunday?\n",);
// The message has a plain text body only, so `html()` returns `null`.echo $note->html() ?? 'no HTML', "\n";
// `text()` returns the plain text body. `Core\Html::escape` makes it safe for a page.// The body already ends with a line break, so this `echo` does not add `"\n"`.echo Core\Html::escape($note->text() ?? '');no HTMLAre you open on Sunday?The html is decoded
<?nvs// Shows that `html()` returns the HTML decoded, as the mail program of the sender wrote it.
use Core\Mail\Message;
// Many mail programs send HTML in the quoted-printable encoding. In it, `=3D` is `=`// and a line that ends with `=` continues on the next line.Message $newsletter = Message::parse( "From: News <news@example.com>\n" . "Subject: Spring sale\n" . "MIME-Version: 1.0\n" . "Content-Type: text/html; charset=utf-8\n" . "Content-Transfer-Encoding: quoted-printable\n" . "\n" . "<p>Our <a href=3D\"https://example.com/sale\">spring sale</a> starts to=\n" . "day.</p>\n",);
// `html()` returns the decoded HTML. This prints the link with `=` and the word `today`.echo Core\Html::sanitize($newsletter->html() ?? '');<p>Our <a href="https://example.com/sale">spring sale</a> starts today.</p>