Skip to content

Core\Mail\Attachment::fromFile

Reads a file from the disk and returns it as an attachment for Core\Mail::send.

Pass the result in the attachments option of Core\Mail::send. The file is read when fromFile runs. So a missing file throws an IOError on the line that names it. A file that the fs.read capability does not cover throws a RuntimeError on the same line.

A relative path written in the code, such as 'files/invoice.pdf', starts at the folder of the program file that contains it.

The recipient sees the last part of the path as the file name. The name option gives another name. The type is a media type, such as application/pdf. It says what kind of file it is. fromFile detects it from the first bytes of the file. With the type option, you set it yourself.

Good to know: the whole file is kept in memory until the mail is sent. For a large file, send a download link instead.

The examples below send an invoice as a PDF, give a file a clearer name, and show the two errors.

Parameters

  • $pathstringThe file to attach. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Options

  • namestringoptionalThe file name that the recipient sees. By default, it is the last part of the path.
  • typestringoptionalThe media type, such as application/pdf. A media type says what kind of file it is. By default, the type is detected from the first bytes. If it cannot be detected, it is application/octet-stream.

Return value

Returns Core\Mail\Attachment. An attachment. Pass it in the attachments option of Core\Mail::send.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path. This error is also thrown when the name or the type is not allowed, as for fromBytes.
  • IOErrorThe file does not exist or cannot be read.

Changelog

VersionChange
0.0.6Core\Mail\Attachment adds a file to a mail, from a path or from bytes. An inline image's src() is the cid: URL to write in an <img> element.

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"]
# The program may read files in the `files` folder beside this file, and no other file.
[capabilities.fs]
read = ["files"]

An invoice with a pdf

<?nvs
// Shows how to send an invoice with a PDF file attached.
use Core\Mail;
use Core\Mail\Attachment;
// `Attachment::fromFile` reads the file now. The path starts at the folder of this program.
Attachment $invoice = Attachment::fromFile('files/invoice-1042.pdf');
Mail::send(
'shop',
['anna@example.com'],
'Your invoice for order 1042',
'Thank you for your order. Your invoice is attached.',
{ attachments: [$invoice] },
);
// The type is detected from the first bytes of the file. This prints `application/pdf`.
echo 'Sent ', $invoice->name(), ' as ', $invoice->type(), "\n";
Output
Sent invoice-1042.pdf as application/pdf

A name the customer sees

<?nvs
// Shows how to give an attached file a clearer name than the file on the disk has.
use Core\Mail;
use Core\Mail\Attachment;
// The `name` option is the file name in the mail. Without it, the name is the last part of the path.
array<Attachment> $files = [
Attachment::fromFile('files/invoice-1042.pdf', { name: 'Example Shop invoice 1042.pdf' }),
Attachment::fromFile('files/terms.pdf'),
];
Mail::send(
'shop',
['anna@example.com'],
'Your invoice for order 1042',
'Your invoice and our terms are attached.',
{ attachments: $files },
);
foreach ($files as Attachment $file) {
echo $file->name(), "\n";
}
Output
Example Shop invoice 1042.pdf
terms.pdf

A file that cannot be read

<?nvs
// Shows the errors `Attachment::fromFile` throws when it cannot read a file.
use Core\Mail\Attachment;
// The invoice file does not exist, so `fromFile` throws an `IOError` on this line.
try {
Attachment::fromFile('files/invoice-9999.pdf');
echo "The invoice is attached.\n";
} catch (IOError $missing) {
echo "The invoice file is missing, so no mail is sent.\n";
}
// `nvs.toml` allows reading only the `files` folder. This file is outside it,
// so `fromFile` throws a `RuntimeError`.
try {
Attachment::fromFile('nvs.toml');
echo "The settings file is attached.\n";
} catch (RuntimeError $notAllowed) {
echo "This program may not read nvs.toml.\n";
}
Output
The invoice file is missing, so no mail is sent.
This program may not read nvs.toml.