Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add images to emails generated by Laravel 5.3's new notification service?

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 of SimpleMediaMessage 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.

enter image description here

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:

  1. Possibly opening up a security issue, the whole reason of using {{}} over {!!!!}
  2. 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' /> ")

like image 728
Chris Avatar asked Oct 19 '16 10:10

Chris


People also ask

How do I get email notifications in Laravel?

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'); });

What is notify () in Laravel?

Laravel Notify is a package that lets you add custom notifications to your project.


1 Answers

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.

like image 93
Eric Avatar answered Sep 21 '22 02:09

Eric