Skip to content

Core\Mail\Attachment::isInline

$attachment->isInline(): bool

Checks whether an attachment is shown inside the HTML body of the mail. It returns true for an inline image, which is an image shown inside the mail. It returns false for a file to download.

inlineFile and inlineBytes make inline attachments. fromFile and fromBytes make files to download. Only an inline attachment has a src(). On any other attachment, src() throws a RuntimeError, so check isInline() first when a list has both kinds.

In a test, Core\Test::sentMail()[0]->attachments() returns the attachments of the first mail that was sent, in the order they were given. Use isInline() to test that an image is shown inside the mail and is not a file to download.

The examples below list which attachments are inline, add an <img> tag only for an inline image, and test that a logo is inline.

Return value

Returns bool. true for an attachment made with inlineFile or inlineBytes. false for one made with fromFile or fromBytes.

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

Files and images

<?nvs
// Shows which attachments are shown inside the HTML and which are files to download.
use Core\Mail\Attachment;
array<Attachment> $parts = [
Attachment::inlineFile('files/logo.png'),
Attachment::fromFile('files/invoice-1042.pdf'),
Attachment::fromBytes('orders.csv', "order,total\n", { type: 'text/csv' }),
];
// `inlineFile` and `inlineBytes` make inline attachments, which are shown inside the HTML.
// `fromFile` and `fromBytes` make files to download.
foreach ($parts as Attachment $part) {
echo $part->name(), ': ', $part->isInline() ? 'inside the HTML' : 'a file to download', "\n";
}
Output
logo.png: inside the HTML
invoice-1042.pdf: a file to download
orders.csv: a file to download

Only an inline image has a src

<?nvs
// Shows how to check `isInline()` before calling `src()`, so the call does not throw.
use Core\Html\Markup;
use Core\Mail;
use Core\Mail\Attachment;
array<Attachment> $parts = [
Attachment::inlineFile('files/logo.png'),
Attachment::fromFile('files/invoice-1042.pdf'),
];
// An inline image is an image shown inside the mail.
// This loop adds an `<img>` tag for each inline image. It skips each file to download.
Markup $body = html`<p>Your invoice is attached.</p>`;
foreach ($parts as Attachment $part) {
if ($part->isInline()) {
$body = $body + html`<p><img src="{$part->src()}" alt="Example Shop"></p>`;
echo 'Added an image tag for ', $part->name(), "\n";
} else {
echo 'Skipped ', $part->name(), "\n";
}
}
Mail::send('shop', ['anna@example.com'], 'Your invoice', $body, { attachments: $parts });
echo "Sent.\n";
Output
Added an image tag for logo.png
Skipped invoice-1042.pdf
Sent.

A test checks the logo is not a download

<?nvs
// Shows how to test that a logo is shown inside the mail and is not a file to download.
// proof: test
use Core\Mail;
use Core\Mail\Attachment;
use Core\Test;
class Receipt
{
// Sends a receipt with the logo inside the HTML and the invoice as a PDF file.
public static function send(string $email): void
{
Attachment $logo = Attachment::inlineFile('files/logo.png');
Mail::send(
'shop',
[$email],
'Your receipt',
html`<p><img src="{$logo->src()}" alt="Example Shop"></p><p>Thank you for your order.</p>`,
{ attachments: [$logo, Attachment::fromFile('files/invoice-1042.pdf')] },
);
}
}
class ReceiptTest
{
#[Test]
public function onlyTheLogoIsInline(): void
{
Receipt::send('anna@example.com');
// The attachments are in the order they were given: the logo, then the invoice.
array<Attachment> $parts = Test::sentMail()[0]->attachments();
Test::assertTrue($parts[0]->isInline());
Test::assertTrue(!$parts[1]->isInline());
}
}
Output
ReceiptTest
✓ onlyTheLogoIsInline
0 failed, 1 passed, 0 skipped, 0 flaky