Core\Test\SentMail::attachments
$sentMail->attachments(): array<Core\Mail\Attachment>attachments() returns the files that were sent with a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and attachments() is one of its methods.
The result is a list of Core\Mail\Attachment values, in the order your program gave them to the attachments option of Core\Mail::send. When the program gave no attachments, the list is empty. The list also contains each inline image. An inline image is shown inside the HTML of the mail. The HTML shows it with a cid: URL. A cid: URL points at an image inside the same mail.
Each Core\Mail\Attachment has these methods:
name()returns the file name.type()returns the media type, such astext/csv.bytes()returns the content of the file.isInline()returnstruefor an inline image.
Use attachments() to check that a mail has the right file, with the right content.
The examples below show an invoice with a CSV file, a newsletter with a logo inside the HTML, and a notice with no files.
Return value
Returns array<Core\Mail\Attachment>. One Core\Mail\Attachment for each attachment, in the order given. The list is empty when there were none.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Test\SentMail reads every part of a sent message: its Message-ID, the sender's name, the attachments, the thread headers, the unsubscribe link and the bounce tag. |
Examples
The examples below run with this configuration file.
# The examples send mail through this block. `capture = true` keeps each message# and sends nothing, so no mail leaves the computer.[mail.shop]capture = truefrom = "shop@example.com"from_name = "Example Shop"
# The program may send mail through the `shop` block.[capabilities.mail]send = ["shop"]An invoice as a csv file
<?nvs// Shows how to test that a monthly invoice mail has a CSV file attached.// proof: test
use Core\Mail;use Core\Mail\Attachment;use Core\Test;
class Invoice{ // Sends the invoice for one month. The rows go into a CSV file that is attached to the mail. public static function send(string $email, string $month, string $rows): void { Attachment $csv = Attachment::fromBytes('invoice-' . $month . '.csv', $rows, { type: 'text/csv' }); Mail::send('shop', [$email], 'Your invoice for ' . $month, 'The invoice is attached.', { attachments: [$csv], }); }}
class InvoiceTest{ #[Test] public function theCsvFileIsAttached(): void { Invoice::send('anna@example.com', '2026-10', "day,total\n2026-10-03,25.00\n");
array<Attachment> $files = Test::sentMail()[0]->attachments(); Test::assertCount($files, 1); Test::assertSame($files[0]->name(), 'invoice-2026-10.csv'); Test::assertSame($files[0]->type(), 'text/csv');
// `bytes()` returns the content of the file, exactly as the program gave it. Test::assertSame($files[0]->bytes(), "day,total\n2026-10-03,25.00\n" as bytes); }} InvoiceTest ✓ theCsvFileIsAttached
0 failed, 1 passed, 0 skipped, 0 flakyA logo inside the html
<?nvs// Shows how to test a mail that shows a logo image inside its HTML body.// proof: test
use Core\Mail;use Core\Mail\Attachment;use Core\Str;use Core\Test;
class Newsletter{ // An inline image is shown inside the HTML of the mail. A `cid:` URL points at an image // inside the same mail. `src()` returns that URL, and the `<img>` tag uses it. public static function send(string $email): void { Attachment $logo = Attachment::inlineBytes('logo.png', 'pixel', { type: 'image/png' }); Mail::send('shop', [$email], 'New in October', html`<p><img src="{$logo->src()}" alt="Example Shop"></p><p>Ten new books.</p>`, { attachments: [$logo], }); }}
class NewsletterTest{ #[Test] public function theLogoIsInline(): void { Newsletter::send('anna@example.com');
Attachment $logo = Test::sentMail()[0]->attachments()[0]; Test::assertSame($logo->name(), 'logo.png');
// `isInline()` returns `true` for an image inside the HTML. Test::assertTrue($logo->isInline());
// The HTML that was sent contains the `cid:` URL of the image. Test::assertTrue(Str::contains(Test::sentMail()[0]->html() ?? '', $logo->src())); }} NewsletterTest ✓ theLogoIsInline
0 failed, 1 passed, 0 skipped, 0 flakyA short notice has no files
<?nvs// Shows how to test that a request sends a notice with no attached files.// proof: test
use Core\Http\Method;use Core\Test;
class ShippingTest{ #[Test] public function theNoticeHasNoFiles(): void { Test::request(Method::Post, '/orders/1042/ship');
// The request gave no `attachments` option, so the list is empty. Test::assertCount(Test::sentMail()[0]->attachments(), 0); }}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.// It sends a short notice.if (Core\Env::context() == Core\Env::Context::Request) { Core\Mail::send('shop', ['anna@example.com'], 'Order 1042 has shipped', 'Your parcel is on its way.'); Core\Response::setStatus(202);} ShippingTest ✓ theNoticeHasNoFiles
0 failed, 1 passed, 0 skipped, 0 flaky