Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not receiving the $message variable in view from a Laravel HTML Mailable (NON Markdown)

I've read several similar questions related to this problem but all refer to Markdown mailables.

I'm trying to send inline images in the mailables but I haven't found a way to do it properly (Laravel 5.5).

The documentation says this:

Inline Attachments

Embedding inline images into your emails is typically cumbersome; however, Laravel provides a convenient way to attach images to your emails and retrieving the appropriate CID. To embed an inline image, use the embed method on the $message variable within your email template. Laravel automatically makes the $message variable available to all of your email templates, so you don't need to worry about passing it in manually:

<body>
    Here is an image:

    <img src="{{ $message->embed($pathToFile) }}">
</body>

But, when doing that I receive this error:

Undefined variable: message (View: /path/to/project/resources/views/mails/new_user_welcome.blade.php)

I know that this has a limitation when using a Markdown message but I'm not using one.


This are the related files:

Mail/NewUserWelcomeEmail.php

class NewUserWelcomeEmail extends Mailable
{
    use SerializesModels;

    public function build()
    {
        return $this->view('mails.new_user_welcome');
    }
}

Resources/views/mails/new_user_welcome.blade.php

@extends('layouts.mail')

@section('content')

    <img src="{{ $message->embed(url("storage/images/inline_image.png")) }}" 
    alt="An inline image" />

@endsection

App/Http/Controllers/UserController.php

public function register(NewUserRequest $request)
{
    // some code

    Mail::to($user)->send(new NewUserWelcomeEmail($user));

    return 'done';
}
like image 617
Kenny Horna Avatar asked May 09 '18 08:05

Kenny Horna


2 Answers

Well, to be honest, I haven't found a way to make this work properly. I mean, as it stands, this should work. Maybe is my Laravel installation (?)..

Anyway, I did make it work with a workaround.

1) Using Eduardokum' Laravel Mail Auto Embed package, this basically generate a CID for each of your media assets.

But after adding this package this didn't work as expected.. so I:

2) change the way I was referencing my assets, from this:

   <img src="{{ url("storage/inline_image.png") }}" />

To this:

   <img src="{{ asset("storage/inline_image.png") }}" />

Now it works.

like image 189
Kenny Horna Avatar answered Sep 20 '22 16:09

Kenny Horna


In my case (Larvel 5.5), I've managed, to modify header logo, in both html and markdown.

Laravel documentation, although really great, could be better in this regard.

Anyway, follow these steps, and you should be fine...

1 - Publish mail templates via:

php artisan vendor:publish --tag=laravel-mail

so you can easily modify your mail source files.

2 - Modify message.blade.php in resources/views/vendor/mail/html with this:

@slot('header')
    @component('mail::header', ['url' => config('app.url')])
        <img src="{{asset('assets/img/pathToYourImage...')}}">
    @endcomponent
@endslot

3 - All your emails should receive logo via CID from now.

Note:

In this example Laravel, automatically converts assets to CIDs, so you don't need to call $message->embed(... at all...

Please test extensively, with these html/markdown directories and blade directives going on. It is kinda tricky, but it definitely, does its magic...

like image 33
Bart Avatar answered Sep 21 '22 16:09

Bart