laravel 5.4 embed image in mail

心不动则不痛 提交于 2019-11-27 23:18:57

问题


I have just upgraded my 5.2 install of laravel to 5.3 and then to 5.4 following the official upgrading methods.

I am now trying to use one of the new features, to create a markdown formated email.

According to the documentation found at: https://laravel.com/docs/5.4/mail#view-data

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:

However, this:

<img src="{{ $message->embed(public_path().'/img/official_logo.png') }}">

will produce the following error:

Undefined variable: message

Am I missing something? Or is there something undocumented in the upgrading guides?

Later edit:

I am calling the email function with:

\Mail::to($user)->send(new WelcomeCandidate($user, $request->input('password')));

And WelcomeCandidate looks like:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Models\User;

class WelcomeCandidate extends Mailable
{

    use Queueable, SerializesModels;

    public $user;
    public $password;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user, $password)
    {
        //
        $this->user = $user;
        $this->password = $password;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->subject('Welcome!');
        return $this->markdown('emails.welcome-candidate');
    }
}

回答1:


It seems that the older $message->embed doesn't work nicely with Markdown emails. Like you mentioned in the comments it seems broken since 5.4

But you could just try it like this inside your markdown email:

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

[logo]: {{asset('/img/official_logo.png')}} "Logo"

Like shown here: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images

Asset function reference: https://laravel.com/docs/5.4/helpers#method-asset




回答2:


Try this:

<img src="data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}}" alt="">

or

![](data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}})



回答3:


You can also use this useful package

https://github.com/eduardokum/laravel-mail-auto-embed

Taken from the readme

Its use is very simple, you write your markdown normally:

@component('mail::message')
# Order Shipped

Your order has been shipped!

@component('mail::button', ['url' => $url])
View Order
@endcomponent

Purchased product:

![product](https://example.com/products/product-1.png)

Thanks,<br>
{{ config('app.name') }}
@endcomponent

When sending, it will replace the link that would normally be generated:

<img src="https://example.com/products/product-1.png">

by an embedded inline attachment of the image:

<img src="cid:3991f143cf1a86257f8671883736613c@Swift.generated">



回答4:


Solved my issue!

  <img src="{{ $message->embed(base_path() . '/img/logo.png') }}" />
Controller:


\Mail::send(['html' =>'mail'],
        array(
            'name' => $r->name,
            'email' => $r->email,
            'user_message' => $r->message,
           // 'telephone'=>$r->telephone,
           // 'subject'=>$r->subject
        ), function($message) use ($r)  {

            $message->to('abc@gmail.com')->subject('Contact Form || abc.com');
            $message->from($r->email);
            // ->setBody($r->user_message); // assuming text/plain

        });

If you are in localhost , you can use public_path instead of base_path function




回答5:


You could try the following:

class WelcomeCandidate extends Mailable
{
    use Queueable, SerializesModels;

    public $message;
    public function __construct(User $user)
    {
        $this->user = $user;
        $this->message = (object) array('image' => '/path/to/file');
    }
}


来源:https://stackoverflow.com/questions/42503168/laravel-5-4-embed-image-in-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!