Core\Mail\Message::raw
$message->raw(): bytesReturns a received message exactly as Core\Mail\Message::parse read it: every header and the whole body, as bytes.
Nothing is decoded and nothing is changed. An encoded subject stays encoded, and every line ending stays as it was. Use raw() to keep a copy of a message, for example as an .eml file that every mail program can open, or to forward the whole message as an attached file. For an Outlook .msg file, raw() returns the bytes of that file.
The sender wrote every byte, so raw() returns tainted bytes. Tainted means that the value comes from outside your program, so you must check it before you use it.
To read one part of the message, use the other methods, such as subject(), text() or attachments().
The examples below show how to save a message as a file, that the bytes are not changed, and how to forward a whole message.
Return value
Returns bytes. The raw bytes, as tainted bytes, because the sender wrote them.
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
The examples below run with this configuration file.
# The examples send mail through this block. `capture = true` keeps each message# and sends nothing, so no mail leaves the computer.[mail.shop]capture = truefrom = "shop@example.com"from_name = "Example Shop"
# The program may send mail through the `shop` block.[capabilities.mail]send = ["shop"]Keep a copy of a message
<?nvs// Shows how to save a received message as an `.eml` file, which every mail program can open.
use Core\IO;use Core\Mail\Message;
Message $complaint = Message::parse( "From: Ada <ada@example.org>\r\n" . "Subject: The mug arrived broken\r\n" . "\r\n" . "Please send a new one.\r\n",);
// `raw()` returns the whole message as tainted bytes: the headers and the body.// Tainted means that the value comes from outside your program.string $folder = IO::createTemporaryDirectory();string $path = Core\Path::join($folder, 'complaint-1.eml');IO::write($path, $complaint->raw());
// The file has the same 88 bytes that were parsed.echo 'saved ', IO::size($path), " bytes\n";
// This example deletes the file again, so it leaves nothing behind.IO::remove($path);IO::removeDirectory($folder);saved 88 bytesThe bytes are not changed
<?nvs// Shows that `raw()` returns the bytes exactly as `parse` read them.
use Core\Mail\Message;
// The subject is encoded, and the lines end with `\r\n`.string $received = "Subject: =?UTF-8?B?R3LDvMOfZQ==?=\r\n\r\nHello\r\n";Message $message = Message::parse($received);
// `subject()` returns the decoded text. This prints `Grüße`.echo $message->subject(), "\n";
// `raw()` still has the encoded subject and every `\r\n`. This prints `same bytes`.echo $message->raw() == $received as bytes ? 'same bytes' : 'changed', "\n";Grüßesame bytesForward a whole message
<?nvs// Shows how to forward a whole received message, as an attached file, to the person who checks complaints.
use Core\Mail;use Core\Mail\Attachment;use Core\Mail\Message;
Message $complaint = Message::parse( "From: Ada <ada@example.org>\n" . "Subject: The mug arrived broken\n" . "\n" . "Please send a new one.\n",);
// `message/rfc822` is the media type of a whole mail message. The mail program of// the recipient opens the attached file as a mail.Attachment $copy = Attachment::fromBytes('complaint.eml', $complaint->raw(), { type: 'message/rfc822' });Mail::send('shop', ['support@example.com'], 'A complaint to check', 'The message is attached.', { attachments: [$copy],});echo 'forwarded ', $copy->name() ?? '', ', ', Core\Bytes::length($copy->bytes()), " bytes\n";forwarded complaint.eml, 84 bytes