Skip to content

Core\Mail\Attachment::bytes

$attachment->bytes(): bytes

Returns the content of an attachment, as tainted bytes. For an attachment you send, these are the bytes that the mail contains. For an attachment of a received message, the sender wrote them.

The result is tainted bytes for every attachment, also when it was made from a string. Tainted means that the value comes from outside your program, so you must check it before you use it.

An attachment made from a string stores the string as UTF-8. So a letter such as é is two bytes. Core\Bytes::length gives the size of the content, and Core\Encoding::toHex shows each byte.

In a test, Core\Test::sentMail()[0]->attachments() returns the attachments of the first mail that was sent. Compare bytes() with the content you expect, written as a string with as bytes.

Good to know: the content is in memory for as long as the attachment exists. A mail with a large file uses about 1.4 times the file's size while it is sent.

The examples below check the size of a file before it is sent, show a string returned as bytes, and test the content of a CSV file.

Return value

Returns bytes. The bytes that are sent.

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

Check the size before sending

<?nvs
// Shows how to check the size of an attachment before the mail is sent.
use Core\Bytes;
use Core\Mail;
use Core\Mail\Attachment;
use Core\Str;
// Many mail servers do not accept a mail over 10 MB. This program allows 5 MB, 5242880 bytes, for the file.
uint $limit = 5242880;
// The program builds a CSV export with one line for each of 1000 orders.
string $csv = "order,total\n" . Str::repeat("1042,59.90\n", 1000);
Attachment $export = Attachment::fromBytes('orders.csv', $csv, { type: 'text/csv' });
// `bytes()` returns the content. `Bytes::length` gives its size in bytes. This prints `11012`.
uint $size = Bytes::length($export->bytes());
echo 'The file has ', $size, " bytes.\n";
if ($size <= $limit) {
Mail::send('shop', ['accounts@example.com'], 'Orders', 'The orders are attached.', {
attachments: [$export],
});
echo "The file is small enough, so it is sent.\n";
}
Output
The file has 11012 bytes.
The file is small enough, so it is sent.

A string is returned as bytes

<?nvs
// Shows that `bytes()` returns `bytes`, also when the attachment was made from a `string`.
use Core\Encoding;
use Core\Mail\Attachment;
// The content is the string `Hi!`.
Attachment $note = Attachment::fromBytes('note.txt', 'Hi!', { type: 'text/plain' });
// `bytes()` returns the same three bytes as `bytes`. In hex, this prints `486921`.
echo Encoding::toHex($note->bytes()), "\n";
// A string with letters that are not ASCII is stored as UTF-8. `é` is two bytes, so this prints `6361c3a9`.
echo Encoding::toHex(Attachment::fromBytes('note.txt', 'caé')->bytes()), "\n";
Output
486921
6361c3a9

A test reads the csv

<?nvs
// Shows how to test the content of a CSV file that a mail was sent with.
// proof: test
use Core\Mail;
use Core\Mail\Attachment;
use Core\Test;
class Export
{
// Sends the orders of one day as a CSV file.
public static function send(string $email, array<string> $lines): void
{
string $csv = "order,total\n";
foreach ($lines as string $line) {
$csv = $csv . $line . "\n";
}
Mail::send('shop', [$email], 'Orders of the day', 'The orders are attached.', {
attachments: [Attachment::fromBytes('orders.csv', $csv, { type: 'text/csv' })],
});
}
}
class ExportTest
{
#[Test]
public function theCsvHasEveryOrder(): void
{
Export::send('accounts@example.com', ['1041,19.90', '1042,59.90']);
// `bytes()` returns the content that was sent. `as bytes` turns the expected text into bytes.
Attachment $file = Test::sentMail()[0]->attachments()[0];
Test::assertSame($file->bytes(), "order,total\n1041,19.90\n1042,59.90\n" as bytes);
}
}
Output
ExportTest
✓ theCsvHasEveryOrder
0 failed, 1 passed, 0 skipped, 0 flaky