Core\Mail\Attachment::inlineFile
Core\Mail\Attachment::inlineFile(
string $path,
{type?: string},
): Core\Mail\AttachmentReads an image from the disk and returns it as an inline attachment. An inline image is an image shown inside the HTML of the mail, not as a download.
Write the attachment's src() in the src of an <img> tag. src() returns a cid: URL, which points at an image inside the same mail. Pass the attachment in the attachments option of Core\Mail::send. The image is sent with the mail, so the mail program does not load it from the internet.
The file is read when inlineFile runs. A missing file throws an IOError on that line. A file that the fs.read capability does not cover throws a RuntimeError. A relative path starts at the folder of the program file that contains it.
send checks that the HTML and the inline images match. An inline image that the HTML does not use throws a RuntimeError, and nothing is sent. So does a cid: URL with no inline image for it.
The examples below send a welcome mail with a logo, show the error for an image the HTML does not use, and test the logo.
Parameters
$pathstringThe image file. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.
Options
typestringoptionalThe media type, such asimage/svg+xml. A media type says what kind of file it is. By default, the type is detected from the first bytes. Give it for an SVG image, because the type of an SVG cannot be detected.
Return value
Returns Core\Mail\Attachment. An inline attachment. Pass it in the attachments option of Core\Mail::send, together with an HTML body that uses its src().
Errors
RuntimeErrorThe configuration does not grantfs.readfor this path. This error is also thrown whentypeis not a media type.IOErrorThe file does not exist or cannot be read.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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.
# 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"]
# The program may read files in the `files` folder beside this file, and no other file.[capabilities.fs]read = ["files"]A logo in a welcome mail
<?nvs// Shows how to send a welcome mail with the shop's logo shown inside the text.
use Core\Mail;use Core\Mail\Attachment;
// `Attachment::inlineFile` reads the image now. The path starts at the folder of this program.Attachment $logo = Attachment::inlineFile('files/logo.png');
// `src()` returns the URL of the image inside this mail. It goes in the `src` of the `<img>` tag.Mail::send( 'shop', ['anna@example.com'], 'Welcome to Example Shop', html`<p><img src="{$logo->src()}" alt="Example Shop"></p><p>Welcome, Anna!</p>`, { attachments: [$logo] },);
echo 'Sent the welcome mail with ', $logo->name(), " inside it.\n";Sent the welcome mail with logo.png inside it.An image the html does not use
<?nvs// Shows the error `Core\Mail::send` throws when the HTML does not show an inline image.
use Core\Mail;use Core\Mail\Attachment;
// `inlineFile` makes an inline image, which is an image shown inside the mail.Attachment $logo = Attachment::inlineFile('files/logo.png');Attachment $banner = Attachment::inlineFile('files/banner.png');
// The HTML shows the logo, but not the banner. Every inline image must appear in the HTML,// so `send` throws a `RuntimeError` and sends nothing.try { Mail::send( 'shop', ['anna@example.com'], 'Our winter sale', html`<p><img src="{$logo->src()}" alt="Example Shop"></p><p>Everything is 20% off.</p>`, { attachments: [$logo, $banner] }, ); echo "sent\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Mail::send: the inline attachment "banner.png" is not used. Write its `src()` in the HTML, or attach it with `fromFile` or `fromBytes`.A test checks the logo
<?nvs// Shows how to test that a mail shows its logo inside the HTML.// proof: test
use Core\Mail;use Core\Mail\Attachment;use Core\Str;use Core\Test;
class Welcome{ // Sends a welcome mail with the logo inside the HTML. public static function send(string $email): void { Attachment $logo = Attachment::inlineFile('files/logo.png'); Mail::send( 'shop', [$email], 'Welcome to Example Shop', html`<p><img src="{$logo->src()}" alt="Example Shop"></p><p>Welcome!</p>`, { attachments: [$logo] }, ); }}
class WelcomeTest{ #[Test] public function theLogoIsInsideTheMail(): void { Welcome::send('anna@example.com');
// The mail has one attachment, and it is the inline logo. Attachment $logo = Test::sentMail()[0]->attachments()[0]; Test::assertTrue($logo->isInline()); Test::assertSame($logo->type(), 'image/png');
// The HTML that was sent uses the logo's `src()`. string $html = Test::sentMail()[0]->html() ?? ''; Test::assertTrue(Str::contains($html, '<img src="' . $logo->src() . '"')); }} WelcomeTest ✓ theLogoIsInsideTheMail
0 failed, 1 passed, 0 skipped, 0 flaky