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.
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.
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.
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."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With