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
-
Use the
build()method in your Mailable class
The$messagevariable is available in thebuild()method, not in thecontent()method (which is used in combination with themailablesapproach).
Example:public function build() { $cid = $this->embed(public_path('images/logo.png')); return $this->view('mails.secretsantainvitation') ->with([ 'logoCid' => $cid, ]); } -
Use the
$logoCidvariable 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.