Is it possible to include an image in a HTML email in Laravel
Solution 1
HTML::image()
generates absolute path to image, like this:
http://yoursite.dev/image.png
which works perfectly in your local environment.
Mail client, as Gmail, will override image path with something like this:
http://images.gmail.com/?src=http://yoursite.dev/image.png
Obviosly 3rd party service can't access your local domain and so you see broken image.
As a solution, you can upload images to 3rd party service like Amazon AWS and pass link to uploaded image to email template.
Solution 2
This one worked for me: The image is declared in the email view
<img src="{{ $message->embed(public_path() . '/resources/icon/icon.png') }}" alt="" />
from Laravel doc "A $message variable is always passed to e-mail views, and allows the inline embedding of attachments."
cchapman
Technical Manager/Lead Developer with a master's degree in Instructional Technology and Media from Columbia University. Developing in Node, React and AWS.
Updated on June 20, 2022Comments
-
cchapman almost 2 years
I'm currently trying to send an HTML email via Laravel's
Mail::send()
. The view in which I'm using is a Blade template, but I'm not quite sure on how I would go about including an image in the body of the email.In my template, I have
<h4>You have successfully updated your Session!</h4> @if ( $old_date != $date) <p>You have changed from {{ $old_date }} to {{ $date }}</p> @endif <p>If you have any questions or concerns, please contact the Office of Admissions</p> {{ HTML::image('/images/full-logo.png', 'logo') }}
However, my email comes out with just a broken link icon in the body of the email. Is it possible to pass an image as a parameter to the view? In my Controller which sends the email, I have
$data = array ( 'first_name' => $student->first_name, 'last_name' => $student->last_name, 'date' => $day, 'old_date' => $old_day, ); Mail::send ( '/emails/create_student', $data, function ($message) { ... } );
So I'm not sure if it might be possible to include an image via the
$data
array.