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:
Illuminate\Notifications\Messages\SimpleMessage
as a new local class, along the lines of SimpleMediaMessage
with a few additional methods (example: ->image(src, url, alt)
)SimpleMediaMessage
that loops over the media array built up via ->image
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:
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' /> ")
Send Email Notifications in Laravel php use App\Notifications\Newvisit; Route::get('/', function () { $user = App\User::first(); $user->notify(new Newvisit("A new user has visited on your application.")); return view('welcome'); });
Laravel Notify is a package that lets you add custom notifications to your project.
How are you wanting to attach? In emails you can reference the full path and just add it to the view in typical HTML format:
<img src="http://yoursite.com/path/to/image.png">
If you want to make it a real file attachment there is both an ->attach()
and attachData
method on the MailMessage. These are typically used for things like PDF's or document attachments.
If you want to attach and reference the attachment in the source then I'm guessing you will need to extend the class like you mentioned or fallback to Laravel's Mail::send
method.
Since the notification system was designed to be simple and opinionated I imagine they wanted to cover the common use cases and it's much simpler to use full paths to images versus referencing the cid:
style.
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