Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Inline attachment with a Mailable

Hello,

I'm trying to add an inline attachment.

https://laravel.com/docs/12.x/mail#inline-attachments

But I get this error message.

Undefined variable $message

The documentation says that the $message variable is not available in plain-text messages.

I don't have any text attribute in the content class.

public function content(): Content
{
    return new Content(
        view: 'mails.secretsantainvitation',
    );
}

What am I doing wrong ?

Thanks for your help.

V

0 likes
3 replies
LaryAI's avatar
Level 58

To send an inline attachment with a Laravel mailable, you'll want to use the embed() or embedData() method from the $message object, but this is only accessible within the build() method, not with the new MailMessage object syntax (the content() method). Here’s how to do it:

Correct Way to Handle Inline Attachments

  1. Use the build() method in your Mailable class
    The $message variable is available in the build() method, not in the content() method (which is used in combination with the mailables approach).
    Example:

    public function build()
    {
        $cid = $this->embed(public_path('images/logo.png'));
    
        return $this->view('mails.secretsantainvitation')
                    ->with([
                        'logoCid' => $cid,
                    ]);
    }
    
  2. Use the $logoCid variable in your Blade template

    <img src="cid:{{ $logoCid }}">
    

If You Prefer content(), Use Attachments, Not Inline Embed

The new mailables (content(), attachments()) use a more structured approach, but inline attachments aren't directly supported yet in this syntax. You'll need to use build() for true inline attachments.

Summary of Your Error

  • You get "Undefined variable $message" because you're trying to use inline attachments in a method (content()) that doesn't give you access to $message.
  • For inline images, always switch to the build() approach in your mailable class.

Reference:
Laravel Docs - Inline Attachments


If you want to use the newer syntax and can move the image as a standalone attachment, you can still use the attachments() method, but this is NOT inline.

public function attachments(): array
{
    return [
        Attachment::fromPath(public_path('images/logo.png')),
    ];
}

But, again, that's just an attachment, not inline in the email content.


Bottom line:
Use the classic build() approach for true in-message inline images.

vincent15000's avatar

Lary's answer doesn't work, $this->embed() doesn't exist.

vincent15000's avatar

I have done another way : I have converted the image to avif and in the code I encode it to base64 and I display the base64 image in the email.

But this way the images are blocked by Gmail. Furthermore I just noticed that Gmail doesn't accept the styles.

Please or to participate in this conversation.