laravel 5.4 在邮件中嵌入图像 [英] laravel 5.4 embed image in mail
问题描述
我刚刚按照官方升级方法将 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 AppMail;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateContractsQueueShouldQueue;
use AppModelsUser;
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屋!