Core\Mail\Message::text
$message->text(): ?stringReturns the plain text body of a received message, or null when the message has no plain text part.
The text is decoded. Mail often encodes a body for sending, for example as base64 or quoted-printable. text() returns normal text. When the message has several plain text parts, text() returns the first one.
The sender wrote this text, so text() 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. You can print it, search it and split it. Before you put it on a page, escape it with Core\Html::escape.
Some messages have only an HTML body. Then text() returns null, and html() returns the body.
uuencode is an old way to write a file as text, from a begin line to an end line. Such a block is removed from the text, and attachments() returns it as a file. For an Outlook .msg file, the text is the plain text body that Outlook saved.
The examples below show the text of a reply, a message with HTML only, and a reply without its quoted lines.
Return value
Returns ?string. The text, or null when the message has no text/plain part. For an Outlook file, it is the plain text body that Outlook saved, or null.
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 the text of a reply
<?nvs// Shows how to read the plain text body of a message that also has an HTML body.
use Core\Mail\Message;
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>\n" . "--b1--\n",);
// `text()` returns the `text/plain` part. This prints `Thank you, the mug arrived.`// `echo` adds no line break, and the text already ends with one.echo $reply->text() ?? 'no text';Thank you, the mug arrived.A message with only html
<?nvs// Shows what `text()` returns for a message that has an HTML body only.
use Core\Mail\Message;
Message $newsletter = Message::parse( "From: News <news@example.com>\n" . "Subject: Spring sale\n" . "Content-Type: text/html\n" . "\n" . "<p>Our spring sale starts today.</p>\n",);
// The message has no `text/plain` part, so `text()` returns `null`.echo $newsletter->text() ?? 'no text', "\n";
// The HTML body is still there. `html()` returns it.echo $newsletter->html() == null ? 'no HTML' : 'has HTML', "\n";no texthas HTMLThe new lines of a reply
<?nvs// Shows how to keep only the new lines of a reply, without the quoted message below them.
use Core\Mail\Message;
Message $reply = Message::parse( "From: Ada <ada@example.org>\n" . "Subject: Re: Ticket 7\n" . "\n" . "The printer works again.\n" . "Thank you!\n" . "\n" . "> Please restart the printer.\n" . "> Does it work now?\n",);
// A mail program puts `>` in front of each line it quotes. This loop skips those lines.// Each line is tainted: it comes from outside your program, so you must check or escape it.foreach (Core\Str::lines($reply->text() ?? '') as var $line) { if (!Core\Str::startsWith($line, '>') && $line != '') { echo $line, "\n"; }}The printer works again.Thank you!