Laravel 5.3 introduces a new service called notifications, allowing the construct of emails (among other notifications) via a simple fluent syntax:
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');
What is an eloquent approach to adding images to the email notifications? I have already published the vendor files to modify the base template.
My thoughts currently stand at:
- Extend
Illuminate\Notifications\Messages\SimpleMessage
as a new local class, along the lines ofSimpleMediaMessage
with a few additional methods (example:->image(src, url, alt)
) - Modify the base template (or create one specific to
SimpleMediaMessage
that loops over the media array built up via->image
- Finally, register a custom channel to allow notifications to deliver notifications with images.
This seems quite heavy handed for something as simple as images in email. Am I missing something? Is there a better approach?
Edited for clarity
When I refer to images, I mean banner and trail images that are visible in the message itself (not as a seperate attachment).
The attached image shows a) in red what can be currently achieved, and b) in purple what I am looking for.
Second edit
Re-reading the answers posted so far, especially @Erics, I see that with a very simple modification to the template, you can in fact do the following:
->line("<img src='foo.example/bar.jpg' />")
The template needs to be modified to allow unsafe markup:
// Note this is in two spots - intro + outro
{{ $line }} --> becomes --> {!! $line !!}
Disadvantages to this method:
- Possibly opening up a security issue, the whole reason of using {{}} over {!!!!}
The image can't take advantage of the inline styles, unless you do it outside the email template, for example:
->line(" < img style='max-width:570px;/* all the other junk to make images look ok in email */' src='foo.example.bar.jpg' /> ")