Reputation: 14241
I've read several similar questions related to this problem but all refer to Markdown mailables.
I'm trying to send inline images in the mailables but I haven't found a way to do it properly (Laravel 5.5).
The documentation says this:
Inline Attachments
Embedding inline images into your emails is typically cumbersome; however, Laravel provides a convenient way to attach images to your emails and retrieving the appropriate
CID
. To embed an inline image, use theembed
method on the$message
variable within your email template. Laravel automatically makes the$message
variable available to all of your email templates, so you don't need to worry about passing it in manually:<body> Here is an image: <img src="{{ $message->embed($pathToFile) }}"> </body>
But, when doing that I receive this error:
Undefined variable: message (View: /path/to/project/resources/views/mails/new_user_welcome.blade.php)
I know that this has a limitation when using a Markdown message but I'm not using one.
This are the related files:
class NewUserWelcomeEmail extends Mailable
{
use SerializesModels;
public function build()
{
return $this->view('mails.new_user_welcome');
}
}
@extends('layouts.mail')
@section('content')
<img src="{{ $message->embed(url("storage/images/inline_image.png")) }}"
alt="An inline image" />
@endsection
public function register(NewUserRequest $request)
{
// some code
Mail::to($user)->send(new NewUserWelcomeEmail($user));
return 'done';
}
Upvotes: 8
Views: 6760
Reputation: 14241
Well, to be honest, I haven't found a way to make this work properly. I mean, as it stands, this should work. Maybe is my Laravel installation (?)..
Anyway, I did make it work with a workaround.
1) Using Eduardokum' Laravel Mail Auto Embed package, this basically generate a CID for each of your media assets.
But after adding this package this didn't work as expected.. so I:
2) change the way I was referencing my assets, from this:
<img src="{{ url('storage/inline_image.png') }}" />
To this:
<img src="{{ asset('storage/inline_image.png') }}" />
Now it works.
Upvotes: 9
Reputation: 3289
This way you can embed images in markdown Laravel mails (used here to eg embed a logo):
app/Mail/Demo.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Swift_Image;
class Demo extends Mailable
{
use Queueable, SerializesModels;
public $image_logo_cid;
/**
* Build the message.
*
* @return $this
*/
public function build()
{
// Generate a CID
$this->image_logo_cid = \Swift_DependencyContainer::getInstance()
->lookup('mime.idgenerator')
->generateId();
return $this->withSwiftMessage(function (\Swift_Message $swift) {
$image = Swift_Image::fromPath(resource_path('img/make-a-wish-logo-rev.gif'));
$swift->embed($image->setId($this->image_logo_cid));
})->subject('My awesome markdown mail with an embedded image')
->markdown('emails.demo');
}
}
resources/views/emails/demo.blade.php
@component('mail::message')
{{-- Just embedding this image in the content here --}}
<img src="cid:{{ $image_logo_cid }}">
@endcomponent
Alternatively you could embed the mail::layout component and put the image in your header:
resources/views/emails/demo.blade.php
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
<img src="cid:{{ $image_logo_cid }}">
@endcomponent
@endslot
{{-- Body --}}
<!-- Body here -->
{{-- Subcopy --}}
@slot('subcopy')
@component('mail::subcopy')
<!-- subcopy here -->
@endcomponent
@endslot
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
<!-- footer here -->
@endcomponent
@endslot
@endcomponent
Or if you always want this header just edit the resources/views/vendor/mail/html/header.blade.php
file (available after php artisan vendor:publish --tag=laravel-mail
). Then of course you need to create the CID/Image with every mailable as seen in the app/Mail/Demo.php (or have a base controller for that).
Upvotes: 0
Reputation: 3971
You have to define the File Path Variable in your Mailable as public property -> example $pathToFile.
If you have the file path from outside of the mailable you can pass in with the constructor.
class NewUserWelcomeEmail extends Mailable
{
use SerializesModels;
// Must be public
public $pathToFile;
/**
* Create a new message instance.
*/
public function __construct(string $pathToFile)
{
$this->pathToFile= $pathToFile;
}
public function build()
{
return $this->view('mails.new_user_welcome');
}
}
Then it works as expected in your view like this:
@extends('layouts.mail')
@section('content')
<img src="{{ $message->embed(url($pathToFile)) }}" alt="An inline image" />
@endsection
Upvotes: 0
Reputation: 1957
In my case (Larvel 5.5), I've managed, to modify header logo, in both html and markdown.
Laravel documentation, although really great, could be better in this regard.
Anyway, follow these steps, and you should be fine...
1 - Publish mail templates via:
php artisan vendor:publish --tag=laravel-mail
so you can easily modify your mail source files.
2 - Modify message.blade.php
in resources/views/vendor/mail/html
with this:
@slot('header')
@component('mail::header', ['url' => config('app.url')])
<img src="{{asset('assets/img/pathToYourImage...')}}">
@endcomponent
@endslot
3 - All your emails should receive logo via CID from now.
Note:
In this example Laravel, automatically converts assets to CIDs, so you don't need to call $message->embed(...
at all...
Please test extensively, with these html/markdown directories and blade directives going on. It is kinda tricky, but it definitely, does its magic...
Upvotes: 1
Reputation: 2493
if you can use like this than it can be work other wise you don't use $message variable in mail blade
Mail::send('emails.welcome', $data, function ($message) {
$message->from('[email protected]', 'Laravel');
$message->to('[email protected]')->cc('[email protected]');
});
if you don't want use this method than you can use like this
https://code.tutsplus.com/tutorials/how-to-send-emails-in-laravel--cms-30046
it can be work like this.
Upvotes: 0