Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include an image in a HTML email in Laravel

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.

like image 994
cchapman Avatar asked Apr 01 '15 16:04

cchapman


People also ask

How do I insert an image into an HTML email?

To attach an image, you need to have the encoding scheme of the image you want to attach. This is the base64 string of the picture. You can get this by right-clicking on the image you want to attach, copy the image address, and paste it into the HTML text. The recipient will have a preview of when they open the email.

Can I use PNG in HTML email?

While most of these formats are used for specific purposes and applications, the common 3 formats that are predominantly used in images in HTML emails are JPEG, GIF and PNG.


2 Answers

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."

like image 115
ojb Avatar answered Oct 06 '22 17:10

ojb


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.

like image 43
Limon Monte Avatar answered Oct 06 '22 19:10

Limon Monte