Skip to content

Core\Mail\Attachment::src

$attachment->src(): string

Returns the URL of an inline image, such as cid:3f2a…@inline. An inline image is an image shown inside the HTML of the mail. A cid: URL points at an image inside the same mail. Write it in the src of an <img> tag, and the mail program shows the image at that place.

Only an attachment made with inlineFile or inlineBytes has a src(). On an attachment made with fromFile or fromBytes, the call throws a RuntimeError.

The URL is random, and it is made once, when the attachment is made. Every call returns the same URL, and two images never have the same one. So you can build the HTML before you send the mail.

Core\Mail::send checks each cid: URL in the HTML. The URL must be the src() of an inline attachment of the same mail. A URL from another mail, or one you typed, throws a RuntimeError, and nothing is sent.

The examples below show what a URL looks like, the error for a file to download, and a test that checks the HTML uses each image.

Return value

Returns string. The cid: URL of this inline attachment.

Errors

  • RuntimeErrorThe attachment is not inline. Only inlineFile and inlineBytes make an attachment with a src().

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

What a src looks like

<?nvs
// Shows the URL that `src()` returns for an inline image, which is an image shown inside the mail.
use Core\Mail\Attachment;
use Core\Str;
Attachment $logo = Attachment::inlineBytes('logo.svg', '<svg xmlns="http://www.w3.org/2000/svg"/>', {
type: 'image/svg+xml',
});
// A `cid:` URL points at an image inside the same mail.
// The URL starts with `cid:` and ends with `@inline`. The part between is random.
string $src = $logo->src();
echo Str::startsWith($src, 'cid:') ? "starts with cid:\n" : "other start\n";
echo Str::endsWith($src, '@inline') ? "ends with @inline\n" : "other end\n";
// The URL is made once, when the attachment is made. So every call returns the same URL.
echo $src == $logo->src() ? "the same URL each time\n" : "a new URL each time\n";
// A second image gets another random URL.
Attachment $banner = Attachment::inlineBytes('banner.svg', '<svg xmlns="http://www.w3.org/2000/svg"/>', {
type: 'image/svg+xml',
});
echo $src == $banner->src() ? "the same URL as the logo\n" : "another URL than the logo\n";
Output
starts with cid:
ends with @inline
the same URL each time
another URL than the logo

A file to download has no src

<?nvs
// Shows the error `src()` throws for an attachment that is not inline.
use Core\Mail\Attachment;
// `fromBytes` makes a file to download. A mail program does not show it inside the HTML,
// so it has no `src()`. The call throws a `RuntimeError`.
Attachment $receipt = Attachment::fromBytes('receipt.txt', "Total: 59.90 EUR\n", { type: 'text/plain' });
try {
echo $receipt->src(), "\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Core\Mail\Attachment::src: this attachment is not inline, so it has no `src()`. Build it with `inlineFile` or `inlineBytes` to show it inside the HTML.

A newsletter template

<?nvs
// Shows how to test that a newsletter writes each image's `src()` into its HTML.
// proof: test
use Core\Mail;
use Core\Mail\Attachment;
use Core\Str;
use Core\Test;
class Newsletter
{
// Sends the newsletter with a header image and a footer image inside the HTML.
public static function send(string $email): void
{
string $svg = '<svg xmlns="http://www.w3.org/2000/svg"/>';
Attachment $header = Attachment::inlineBytes('header.svg', $svg, { type: 'image/svg+xml' });
Attachment $footer = Attachment::inlineBytes('footer.svg', $svg, { type: 'image/svg+xml' });
Mail::send(
'shop',
[$email],
'News from Example Shop',
html`<img src="{$header->src()}" alt="News"><p>New mugs are here.</p><img src="{$footer->src()}" alt="">`,
{ attachments: [$header, $footer] },
);
}
}
class NewsletterTest
{
#[Test]
public function eachImageIsInTheHtml(): void
{
Newsletter::send('anna@example.com');
// Each attachment that was sent has its `src()`, and the HTML uses each one.
string $html = Test::sentMail()[0]->html() ?? '';
foreach (Test::sentMail()[0]->attachments() as Attachment $image) {
Test::assertTrue(Str::contains($html, 'src="' . $image->src() . '"'));
}
}
}
Output
NewsletterTest
✓ eachImageIsInTheHtml
0 failed, 1 passed, 0 skipped, 0 flaky