laravel 5.4在邮件中嵌入图像 [英] laravel 5.4 embed image in mail

查看:210
本文介绍了laravel 5.4在邮件中嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

根据位于以下位置的文档: https://laravel.com/docs/5.4 /mail#view-data

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

要嵌入嵌入式图像,请在$message上使用embed方法 电子邮件模板中的变量. Laravel自动使 $message变量可用于所有电子邮件模板,因此您 不必担心手动传递它:

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

将产生以下错误:

未定义变量:message

我错过了什么吗?还是升级指南中没有记载的内容?

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

以后

我通过以下方式调用电子邮件功能:

I am calling the email function with:

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

WelcomeCandidate看起来像:

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

推荐答案

看来,较旧的$ message-> embed与Markdown电子邮件不能很好地配合使用. 就像您在评论中提到的,自5.4起,它似乎已失效

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"

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

资产函数参考: https://laravel.com/docs/5.4/helpers#方法资产

这篇关于laravel 5.4在邮件中嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆