Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display image in email layout on the laravel?

My code to send email like this :

public function toMail($notifiable)
{
    return (new MailMessage)
                ->subject('Test')
                ->markdown('vendor.mail.markdown.message', ['data' => $this->data]);
}

My message.blade view like this :

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            {{ config('app.name') }}
        @endcomponent
    @endslot

    {{-- Body --}}
    This is your logo 
    ![Some option text][logo]
    [logo]: {{asset('img/my-logo.png')}} "Logo"  

    {{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset

    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
        @endcomponent
    @endslot
@endcomponent

It success send email, but the contain of email like this :

This is your logo 
![Some option text][logo]

[logo]: http://myshop.dev/img/my-logo.png "Logo"

The image not display

How can I solve this problem?

I follow this reference : https://medium.com/@adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c

like image 498
moses toh Avatar asked Oct 11 '25 23:10

moses toh


2 Answers

This worked for me !

<img src="{{$message->embed(asset('images/image_name.png'))}}">

for those who have Undefined variable: message : you need to send the variable $message to the view, while $message is the Mailable instance.
example:

$this->view('view_name_here')
     ->with(['message' => $this])
     ->subject("Hello..");
like image 141
Nysso Avatar answered Oct 14 '25 22:10

Nysso


This is an old question but this might help someone else. Try this... (assuming you want to replace the app name with your logo)

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            <img src="{{ asset('img/my-logo.png') }}" alt="{{ config('app.name') }} Logo">
        @endcomponent
    @endslot

    {{-- Body --}}
    {{ $slot }}

    {{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset

    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
        @endcomponent
    @endslot
@endcomponent

When you extend 'vendor.mail.markdown.message' you explicitly say that the rendered mail will use this view only, which is markdown, and you completely ignore the HTML email that'll be sent.

Markdown emails have an HTML version and a raw text version, you don't see the changes you made to the markdown files because you're looking at the HTML version of your email, so see those changes you have to update /resources/views/vendor/mail/html/message.blade.php not only the markdown one.

like image 22
PW_Parsons Avatar answered Oct 14 '25 21:10

PW_Parsons