Skip to content

Core\Mail\Attachment::fromBytes

Returns an attachment with the file name and the content you give. Use it for a file that your program builds, such as a CSV export.

Pass the result in the attachments option of Core\Mail::send. The content can be a string or bytes. Nothing is written to the disk.

The type is a media type, such as image/png. It says what kind of file it is. fromBytes detects it from the first bytes of the content. Plain text has no such start, so a CSV file or a text file gets application/octet-stream. Give the type option for those files, such as text/csv.

The file name is checked when fromBytes runs. An empty name, a name over 255 bytes and a name with a control character, such as a line break, throw a RuntimeError. A name with letters that are not ASCII is allowed. The mail encodes it so that every mail program shows it correctly.

The examples below send a CSV export, show which type each file gets, and show a file name with a line break.

Parameters

  • $namestringThe file name that the recipient sees, such as invoice.pdf. A name with letters that are not ASCII is encoded with RFC 2231.
  • $datastring|bytesThe content, as a string or bytes.

Options

  • 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 file name is empty, is longer than 255 bytes, or contains a control character. This error is also thrown when type is not a media type such as image/png.

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

A csv export

<?nvs
// Shows how to send a CSV file that the program builds, with no file on the disk.
use Core\Mail;
use Core\Mail\Attachment;
// Each key is an order number, and each value is the total of that order.
array<string> $totals = ['1041' => '19.90', '1042' => '59.90', '1043' => '7.50'];
// This loop adds one line for each order.
string $csv = "order,total\n";
foreach ($totals as string $order => string $total) {
$csv = $csv . $order . ',' . $total . "\n";
}
// A CSV file is plain text, so its type cannot be detected. The `type` option sets it.
Attachment $export = Attachment::fromBytes('orders-november.csv', $csv, { type: 'text/csv' });
Mail::send('shop', ['accounts@example.com'], 'Orders in November', 'The orders are attached.', {
attachments: [$export],
});
echo 'Sent ', $export->name(), " with these lines:\n", $csv;
Output
Sent orders-november.csv with these lines:
order,total
1041,19.90
1042,59.90
1043,7.50

The type is detected

<?nvs
// Shows which type `Attachment::fromBytes` gives each file when no `type` option is given.
use Core\Encoding;
use Core\Mail\Attachment;
// A PNG image starts with fixed bytes, so its type is detected. This prints `image/png`.
bytes $photo = Encoding::fromHex(
'89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489'
. '0000000a49444154789c63000100000500010d0a2db40000000049454e44ae426082',
);
echo Attachment::fromBytes('photo.png', $photo)->type(), "\n";
// A PDF file starts with `%PDF-`. This prints `application/pdf`.
echo Attachment::fromBytes('report.pdf', "%PDF-1.4\n%%EOF\n")->type(), "\n";
// Plain text has no such start, so the type is `application/octet-stream`.
echo Attachment::fromBytes('notes.txt', "Call Anna on Monday.\n")->type(), "\n";
// With the `type` option, you set the type yourself.
echo Attachment::fromBytes('notes.txt', "Call Anna on Monday.\n", { type: 'text/plain' })->type(), "\n";
Output
image/png
application/pdf
application/octet-stream
text/plain

A file name from a form

<?nvs
// Shows what happens when a file name that a user typed contains a line break.
use Core\Mail\Attachment;
// A name with letters that are not ASCII is allowed. The mail encodes it for every mail program.
echo Attachment::fromBytes('Rechnung März.txt', "Total: 59.90 EUR\n")->name(), "\n";
// This name hides a second line. In a mail header, that line would add a `Bcc:` recipient.
string $typed = "report.txt\r\nBcc: everyone@example.com";
// A line break is a control character, so `fromBytes` throws a `RuntimeError`.
try {
Attachment::fromBytes($typed, "Total: 59.90 EUR\n");
echo "The report is attached.\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Rechnung März.txt
Core\Mail\Attachment::fromBytes: the file name "report.txt\r\nBcc: everyone@example.com" contains a control character