Skip to content

Core\Mail\Message::attachments

$message->attachments(): array<Core\Mail\Attachment>

Returns the files attached to a received message, and the images shown inside its HTML body.

Each file is a Core\Mail\Attachment, in the order of the message. name(), type() and bytes() read its file name, its media type (such as image/png) and its content. A message with no files gives an empty array.

Every file name and byte is tainted: it comes from outside your program. Novis does not allow a tainted name in a file path, so your program chooses the name when it saves a file. A name such as ../../etc/passwd is shortened to passwd. When nothing is left, name() returns null.

Outlook sometimes packs the files of a mail into one file, winmail.dat. The array has the files inside it instead. A mail inside a .msg file is a .msg attachment, and parse can read it. A file written as uuencode (an old way to write a file as text) is an attachment too.

Good to know: you can pass a received attachment to Core\Mail::send. An attachment with no name throws an error there.

The examples below show how to list the files, save a file, and forward a file.

Return value

Returns array<Core\Mail\Attachment>. One attachment for each file part, in order. A part is one piece of a message, such as the text, the HTML or a file. The array is empty when there is none.

Changelog

VersionChange
0.0.6Core\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.

nvs.toml
# The examples send mail through this block. `capture = true` keeps each message
# and sends nothing, so no mail leaves the computer.
[mail.shop]
capture = true
from = "shop@example.com"
from_name = "Example Shop"
# The program may send mail through the `shop` block.
[capabilities.mail]
send = ["shop"]

List the files of a message

<?nvs
// Shows how to list the files that are attached to a received message.
use Core\Mail\Attachment;
use Core\Mail\Message;
Message $mail = Message::parse(
"From: Ada <ada@example.org>\n"
. "Subject: My documents\n"
. "MIME-Version: 1.0\n"
. "Content-Type: multipart/mixed; boundary=\"b1\"\n"
. "\n"
. "--b1\n"
. "Content-Type: text/plain\n"
. "\n"
. "Here are the two files.\n"
. "--b1\n"
. "Content-Type: text/csv; name=\"orders.csv\"\n"
. "Content-Disposition: attachment; filename=\"orders.csv\"\n"
. "\n"
. "id,total\n41,12.50\n42,8.00\n"
. "--b1\n"
. "Content-Type: application/pdf\n"
. "Content-Disposition: attachment; filename=\"invoice.pdf\"\n"
. "Content-Transfer-Encoding: base64\n"
. "\n"
. "JVBERi0xLjQKJcOkw7zDtsOfCg==\n"
. "--b1--\n",
);
// `attachments()` returns one attachment for each file, in order.
// The text part is the body of the message, so it is not in the array.
foreach ($mail->attachments() as Attachment $file) {
echo $file->name() ?? 'no name', ', ', $file->type(), ', ', Core\Bytes::length($file->bytes()), " bytes\n";
}
Output
orders.csv, text/csv, 25 bytes
invoice.pdf, application/pdf, 19 bytes

Save an attachment

<?nvs
// Shows how to save the attached file of a received message.
use Core\IO;
use Core\Mail\Attachment;
use Core\Mail\Message;
Message $mail = Message::parse(
"Subject: The photo\n"
. "MIME-Version: 1.0\n"
. "Content-Type: multipart/mixed; boundary=\"b1\"\n"
. "\n"
. "--b1\n"
. "Content-Type: text/plain\n"
. "\n"
. "The photo of the broken mug.\n"
. "--b1\n"
. "Content-Type: image/png\n"
. "Content-Disposition: attachment; filename=\"mug.png\"\n"
. "Content-Transfer-Encoding: base64\n"
. "\n"
. "iVBORw0KGgo=\n"
. "--b1--\n",
);
// `name()` is a tainted string (text from outside your program), because the sender chose it.
// A tainted string cannot be part of a file path, so the program chooses its own file name.
string $folder = IO::createTemporaryDirectory();
int $number = 1;
foreach ($mail->attachments() as Attachment $file) {
string $path = Core\Path::join($folder, 'upload-' . $number . '.bin');
IO::write($path, $file->bytes());
echo 'saved ', $file->name() ?? 'a file with no name', ' as upload-', $number, '.bin, ', IO::size($path), " bytes\n";
$number = $number + 1;
// This example deletes the file again, so it leaves nothing behind.
IO::remove($path);
}
IO::removeDirectory($folder);
Output
saved mug.png as upload-1.bin, 8 bytes

Forward an attachment

<?nvs
// Shows how to send the attached file of a received message to somebody else.
use Core\Mail;
use Core\Mail\Message;
Message $mail = Message::parse(
"From: Ada <ada@example.org>\n"
. "Subject: Invoice from the supplier\n"
. "MIME-Version: 1.0\n"
. "Content-Type: multipart/mixed; boundary=\"b1\"\n"
. "\n"
. "--b1\n"
. "Content-Type: text/plain\n"
. "\n"
. "The invoice is attached.\n"
. "--b1\n"
. "Content-Type: application/pdf\n"
. "Content-Disposition: attachment; filename=\"invoice-77.pdf\"\n"
. "Content-Transfer-Encoding: base64\n"
. "\n"
. "JVBERi0xLjQKJcOkw7zDtsOfCg==\n"
. "--b1--\n",
);
// A received attachment is a `Core\Mail\Attachment`, so `Mail::send` can send it again.
// `send` throws an error for an attachment that has no name.
Mail::send('shop', ['accounts@example.com'], 'Please pay this invoice', 'The invoice is attached.', {
attachments: $mail->attachments(),
});
echo 'forwarded ', Core\Arr::count($mail->attachments()), " file\n";
Output
forwarded 1 file