Skip to content

Core\Mail\Attachment::name

$attachment->name(): ?string

Returns the file name of an attachment, such as invoice.pdf. This is the name the recipient sees in the mail program.

An attachment made with fromFile or inlineFile has the last part of the path as its name. fromFile also has a name option that gives another name. For fromBytes and inlineBytes, it is the name you gave. A name with letters that are not ASCII is returned as you wrote it.

name() 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.

An attachment of a received message can have no file name, and then the result is null. If you pass that attachment to Core\Mail::send, it throws a RuntimeError. Make a copy with fromBytes, which gives it a name.

In a test, Core\Test::sentMail()[0]->attachments() returns the attachments of the first mail that was sent.

The examples below show the name from a path, a list of names in the text of a mail, and a test that checks the name.

Return value

Returns ?string. The file name the recipient sees. name() returns null for a received file with no name.

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"]

The name comes from the path

<?nvs
// Shows the file name an attachment gets from its path, and how to give it another name.
use Core\Mail\Attachment;
// Without the `name` option, the name is the last part of the path. This prints `invoice-1042.pdf`.
echo Attachment::fromFile('files/invoice-1042.pdf')->name(), "\n";
// With the `name` option, the recipient sees that name instead.
echo Attachment::fromFile('files/invoice-1042.pdf', { name: 'Invoice 1042.pdf' })->name(), "\n";
Output
invoice-1042.pdf
Invoice 1042.pdf

A list of the attached files

<?nvs
// Shows how to write the names of the attached files into the text of the mail.
use Core\Mail;
use Core\Mail\Attachment;
array<Attachment> $files = [
Attachment::fromFile('files/invoice-1042.pdf'),
Attachment::fromBytes('Lieferschein März.txt', "1 x Blue mug\n", { type: 'text/plain' }),
];
// `name()` returns a `tainted string`. Tainted means that the value can come from a user.
// The text that contains the names is then a `tainted string` too.
tainted string $text = "Please find these files attached:\n";
foreach ($files as Attachment $file) {
$text = $text . '- ' . $file->name() . "\n";
}
// A `tainted string` body is sent as plain text, never as HTML.
Mail::send('shop', ['anna@example.com'], 'Your documents for order 1042', $text, {
attachments: $files,
});
echo $text;
Output
Please find these files attached:
- invoice-1042.pdf
- Lieferschein März.txt

A test reads the names

<?nvs
// Shows how to test the file names of the attachments that a mail was sent with.
// proof: test
use Core\Mail;
use Core\Mail\Attachment;
use Core\Test;
class Invoice
{
// Sends the invoice of one order as a PDF file named after the order.
public static function send(string $email, int $order): void
{
Attachment $pdf = Attachment::fromFile('files/invoice-1042.pdf', {
name: 'Invoice ' . $order . '.pdf',
});
Mail::send('shop', [$email], 'Your invoice', 'Your invoice is attached.', {
attachments: [$pdf],
});
}
}
class InvoiceTest
{
#[Test]
public function theFileIsNamedAfterTheOrder(): void
{
Invoice::send('anna@example.com', 1042);
// `attachments()` returns the attachments in the order they were given.
array<Attachment> $files = Test::sentMail()[0]->attachments();
Test::assertCount($files, 1);
Test::assertSame($files[0]->name(), 'Invoice 1042.pdf');
}
}
Output
InvoiceTest
✓ theFileIsNamedAfterTheOrder
0 failed, 1 passed, 0 skipped, 0 flaky