Core\Mail\Message::parse
Core\Mail\Message::parse(string|bytes $raw): Core\Mail\MessageCore\Mail\Message::parse( string|bytes $raw, ): Core\Mail\Message
Reads a raw email message into a Core\Mail\Message.
You pass the whole message as a string or as bytes. It can be an .eml file, the message a mail provider posts to your webhook, or an Outlook .msg or .oft file.
A message has parts. A part is one piece, such as the text, the HTML or a file.
A winmail.dat attachment from Outlook is replaced by the files inside it. A signed message with its content inside a smime.p7m file is opened. The signature is not checked. A file written as uuencode (an old way to write a file as text) becomes an attachment.
A broken message is not an error. A missing value is null, an empty array or an empty string. Novis reads 16 levels of parts inside parts, and the first 1,000 parts.
Good to know: every value is tainted: it comes from outside your program. Check or escape it before you use it.
The examples below read a webhook message, an .eml file, a broken message, a .msg file and a winmail.dat file, and show inline images on a page.
Parameters
$rawstring|bytesThe whole message, headers and body, asbytesor astring. For an Outlook file, pass its bytes.
Return value
Returns Core\Mail\Message. The message. Novis reads the first 16 levels of nesting and the first 1,000 parts, and skips the rest. Only the first 1,000 header lines of a part are read.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Message::parse reads Outlook .msg and .oft files into the same message an .eml file gives. A winmail.dat attachment is replaced by the files and the body inside it. A signed message with its content inside a smime.p7m file is opened, and a uuencoded file in the text is an attachment. |
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
The examples below run with this configuration file.
# The program may read files in the `files` folder beside this file, and no other file.[capabilities.fs]read = ["files"]A message a webhook posts
<?nvs// Shows how to read the raw message that a mail provider posts to your webhook.
use Core\Mail\Message;
// The body of the request is the whole message, headers first.Message $mail = Message::parse(Core\Request::bytes());
// This prints the sender, the subject and the first line of the text.echo 'From: ', $mail->from()?->address() ?? 'unknown', "\n";echo 'Subject: ', $mail->subject(), "\n";echo 'Text: ', Core\Str::trim($mail->text() ?? ''), "\n";From: ada@example.orgSubject: My order has not arrivedText: Hello, I ordered a lamp last week. Where is it?Read an eml file
<?nvs// Shows how to read a message that was saved as an `.eml` file.
use Core\Mail\Attachment;use Core\Mail\Message;
// An `.eml` file is the raw message. The path is relative to the folder of this program.Message $reply = Message::parse(Core\IO::readBytes('files/reply-1042.eml'));
// This prints `Re: Your order 1042` and the id of the message it answers.echo $reply->subject(), "\n";echo 'Answers: ', $reply->inReplyTo() ?? 'nothing', "\n";
// The text and the attached photo are separate parts of the message.echo 'Text: ', Core\Str::trim($reply->text() ?? ''), "\n";foreach ($reply->attachments() as Attachment $file) { echo 'File: ', $file->name() ?? 'no name', ', ', $file->type(), "\n";}Re: Your order 1042Answers: order-1042@example.comText: Thank you. The photo of the parcel is attached.File: parcel.png, image/pngA broken message in a list
<?nvs// Shows that a broken message does not stop a program that reads many messages.
use Core\Mail\Message;
// The second message has no headers, and the third one has a date that is not a date.array<string> $inbox = [ "From: ada@example.org\r\nSubject: Invoice question\r\nDate: 3 Nov 2026 10:00 +0000\r\n\r\nHello.", 'this is not an email', "From: bo@example.org\r\nSubject: Opening hours\r\nDate: someday\r\n\r\nHi.",];
// `parse` does not throw an error. For a missing subject, `subject()` returns an empty string.// For a missing sender or date, `from()` and `date()` return `null`.foreach ($inbox as string $raw) { Message $mail = Message::parse($raw); // The sender wrote the subject, so it is tainted (text from outside your program). tainted string $subject = $mail->subject() == '' ? '(no subject)' : $mail->subject(); echo $mail->from()?->address() ?? '(no sender)', ': ', $subject; echo $mail->date() == null ? ', no date' : ', dated', "\n";}ada@example.org: Invoice question, dated(no sender): (no subject), no datebo@example.org: Opening hours, no dateRead a file dragged from outlook
<?nvs// Shows how to read a message that was saved from Outlook as a `.msg` file.
use Core\Mail\Attachment;use Core\Mail\Message;
// A user may upload such a file, or drag a mail from Outlook onto your page.// `parse` reads a `.msg` file the same way as an `.eml` file.Message $question = Message::parse(Core\IO::readBytes('files/question-about-order-1042.msg'));
// This prints the sender `ada@example.org`, the subject and the time the mail was sent.echo 'From: ', $question->from()?->address() ?? 'unknown', "\n";echo 'Subject: ', $question->subject(), "\n";echo 'Sent: ', $question->date()?->format('yyyy-MM-dd HH:mm') ?? 'no date', "\n";
// The mail has a photo and a PDF attached.foreach ($question->attachments() as Attachment $file) { echo 'File: ', $file->name() ?? 'no name', ', ', $file->type(), "\n";}From: ada@example.orgSubject: Question about order 1042Sent: 2026-11-03 14:05File: parcel.png, image/pngFile: receipt.pdf, application/pdfThe files inside winmail dat
<?nvs// Shows that `parse` replaces a `winmail.dat` attachment with the files inside it.
use Core\Mail\Attachment;use Core\Mail\Message;
// Outlook sometimes packs the files of a mail into one attachment, `winmail.dat`.// Most mail programs cannot open that file.Message $invoice = Message::parse(Core\IO::readBytes('files/invoice-from-outlook.eml'));
echo $invoice->subject(), "\n";echo 'Text: ', Core\Str::trim($invoice->text() ?? ''), "\n";
// `attachments()` returns the two files from inside `winmail.dat`. It does not return `winmail.dat` itself.foreach ($invoice->attachments() as Attachment $file) { echo 'File: ', $file->name() ?? 'no name', ', ', $file->type(), "\n";}Invoice for order 1042Text: Your invoice and the delivery notes are attached.File: invoice-1042.pdf, application/pdfFile: delivery-notes.txt, application/octet-streamShow inline images on a page
<?nvs// Shows how to display the HTML of a received mail with its inline images on your own page.
use Core\Mail\Attachment;use Core\Mail\Message;
Message $news = Message::parse(Core\IO::readBytes('files/shop-news.msg'));tainted string $html = $news->html() ?? '';
// An inline image has a `cid:` URL, and a web browser cannot load it.// So each `cid:` URL is replaced with a URL of your app that returns the file.foreach ($news->attachments() as Attachment $file) { if ($file->isInline()) { $html = Core\Str::replace($html, $file->src(), '/mail/42/files/' . ($file->name() ?? 'image')); }}
// `Core\Html::sanitize` removes scripts and other unsafe HTML. It keeps the image with its new URL.// This prints `<img src="/mail/42/files/logo.png" alt="Shop logo">`.echo Core\Html::sanitize($html), "\n";<p>Our new logo:</p><img src="/mail/42/files/logo.png" alt="Shop logo">