Core\Mail\Attachment::inlineBytes
Core\Mail\Attachment::inlineBytes(
string $name,
string|bytes $data,
{type?: string},
): Core\Mail\AttachmentReturns an inline attachment with the file name and the image you give. An inline image is an image shown inside the HTML of the mail, not as a download.
Use it for an image that is not in a file, such as a photo from a database. 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 type is a media type, such as image/png. It says what kind of file it is. inlineBytes detects it from the first bytes. With the type option, you set it yourself. A file name with a control character, such as a line break, throws a RuntimeError.
send checks that the HTML and the inline images match. An inline image that the HTML does not use throws a RuntimeError, and so does a cid: URL with no inline image for it.
The examples below show a photo from a database, one image for each product, and the error for a cid: URL with no image.
Parameters
$namestringThe file name of the image, such aslogo.png.$datastring|bytesThe image, as astringorbytes.
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 file name is empty, is longer than 255 bytes, or contains a control character. This error is also thrown whentypeis not a media type such asimage/png.
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"]A product photo from the database
<?nvs// Shows how to show an image inside a mail when the program has the image's bytes and no file.
use Core\Encoding;use Core\Mail;use Core\Mail\Attachment;
// A shop often keeps product photos in its database. Here, a tiny PNG image is written in hex.bytes $photo = Encoding::fromHex( '89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489' . '0000000a49444154789c63000100000500010d0a2db40000000049454e44ae426082',);
// The type is detected from the first bytes. This prints `image/png`.Attachment $image = Attachment::inlineBytes('mug.png', $photo);echo $image->type(), "\n";
Mail::send( 'shop', ['anna@example.com'], 'Your mug is on its way', html`<p><img src="{$image->src()}" alt="Blue mug"></p><p>Your order has shipped.</p>`, { attachments: [$image] },);echo "Sent the mail with the photo inside it.\n";image/pngSent the mail with the photo inside it.One image for each product
<?nvs// Shows how to send an order summary with one inline image for each product.
use Core\Encoding;use Core\Html;use Core\Html\Markup;use Core\Mail;use Core\Mail\Attachment;
bytes $photo = Encoding::fromHex( '89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489' . '0000000a49444154789c63000100000500010d0a2db40000000049454e44ae426082',);array<string> $products = ['Blue mug', 'Red mug', 'Tea towel'];
// This loop makes one inline image (an image shown inside the mail) and one line of HTML for each product.// Each image gets its own `src()`, so each line shows the right image.array<Attachment> $images = [];Markup $rows = '' as Markup;foreach ($products as string $product) { Attachment $image = Attachment::inlineBytes($product . '.png', $photo); $images[] = $image; $rows = $rows + html`<p><img src="{$image->src()}" alt="{$product}"> {$product}</p>`;}
Mail::send('shop', ['anna@example.com'], 'Your order', $rows, { attachments: $images });
// Two images never have the same `src()`. This prints `false`.echo $images[0]->src() == $images[1]->src() ? "true\n" : "false\n";echo 'Sent ', Core\Arr::count($images), " inline images.\n";falseSent 3 inline images.The html names an image that is not attached
<?nvs// Shows the error `Core\Mail::send` throws when the HTML uses a `cid:` URL with no inline image.
use Core\Mail;
// A `cid:` URL points at an image inside the same mail. This HTML has one, but no image is attached.// A mail program would show a broken image, so `send` throws a `RuntimeError` and sends nothing.try { Mail::send( 'shop', ['anna@example.com'], 'Our winter sale', html`<p><img src="cid:banner@example.com" alt="Winter sale"></p><p>Everything is 20% off.</p>`, ); echo "sent\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Mail::send: the HTML uses `cid:banner@example.com`, and no inline attachment of this message has that `src()`. Pass the attachment in `attachments`.