laravel 5.4 在邮件中嵌入图片

laravel 5.4 embed image in mail

我刚刚按照官方升级方法将 laravel 的 5.2 安装升级到 5.3,然后升级到 5.4

我现在正在尝试使用其中一项新功能来创建降价格式的电子邮件。

根据在以下位置找到的文档: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:

然而,这:

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

会产生以下错误:

Undefined variable: message

我错过了什么吗?或者升级指南中是否有未记录的内容?

稍后编辑:

我正在调用电子邮件函数:

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

WelcomeCandidate 看起来像:

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

似乎旧的 $message->embed 不能很好地处理 Markdown 电子邮件。 Like you mentioned in the comments it seems broken since 5.4

但你可以在你的 markdown 电子邮件中这样尝试:

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

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

如图所示: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images

资产功能参考:https://laravel.com/docs/5.4/helpers#method-asset

您可以尝试以下方法:

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

试试这个:

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

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

解决了我的问题!

  <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

        });

如果你在 localhost ,你可以使用 public_path 而不是 base_path function

你也可以使用这个有用的包

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

Taken from the readme

它的使用很简单,你正常写你的markdown:

@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

发送时,会替换正常生成的link:

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

通过图像的嵌入式内联附件:

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

我刚遇到同样的问题并找到了解决方案。

在渲染图像之前,您必须使用以下命令创建从 "public/storage" 到 "storage/app/public" 的符号 link:

php artisan storage:link

在 "storage/app/public" 中你应该有一个文件夹 "images" 现在您可以在 markdown.blade.php:

中呈现此代码
!['alt_tag']({{Storage::url('/images/your_image.png')}})

第二个选项类似:

!['alt_text']({{Storage::url($comment->user->image->path)}}) 

两者都很好

在后端,我创建了用于显示图像的端点。并将我需要的图像放在 resource/img 中。 Laravel 代码如下:

 public function getImage($name)
 {
        return response()->file(base_path() . '/resources/img/' . $name . '.png');
 }

然后在我的 html 电子邮件模板中,我创建了 div 背景图像。

<div style='background: url("https://mysite1.com/api/v1/get_image/logo")'></div>

它对我有用。