laravel 5.4 在邮件中嵌入图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42503168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.4 embed image in mail
提问by Angelin Calu
I have just upgraded my 5.2
install of laravel to 5.3
and then to 5.4
following the official upgrading methods.
我刚刚将我5.2
的 Laravel 安装升级到5.3
并5.4
遵循官方升级方法。
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
根据在以下位置找到的文档: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:
要嵌入内嵌图像,
$message
请对电子邮件模板中的变量使用 embed 方法。Laravel 会自动使$message
变量可用于所有电子邮件模板,因此您无需担心手动传递它:
However, this:
然而,这:
<img src="{{ $message->embed(public_path().'/img/official_logo.png') }}">
will produce the following error:
将产生以下错误:
Undefined variable:
message
未定义的变量:
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')));
\Mail::to($user)->send(new WelcomeCandidate($user, $request->input('password')));
And WelcomeCandidate looks like:
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');
}
}
回答by Christophvh
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
似乎旧的 $message->embed 不能很好地处理 Markdown 电子邮件。就像你在评论中提到的,它似乎从 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
如此处所示:https: //github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images
Asset function reference: https://laravel.com/docs/5.4/helpers#method-asset
回答by Mikael
Try this:
尝试这个:
<img src="data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}}" alt="">
or
或者
))}})
回答by John
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('[email protected]')->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
如果你在 localhost ,你可以使用 public_path 而不是 base_path 函数
回答by cyberfly
You can also use this useful package
你也可以使用这个有用的包
https://github.com/eduardokum/laravel-mail-auto-embed
https://github.com/eduardokum/laravel-mail-auto-embed
Taken from the readme
取自自述文件
Its use is very simple, you write your markdown normally:
它的使用非常简单,你正常写下你的markdown:
@component('mail::message')
# Order Shipped
Your order has been shipped!
@component('mail::button', ['url' => $url])
View Order
@endcomponent
Purchased product:

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:[email protected]">
回答by robrok
I just encountered the same issue and found a solution.
我刚刚遇到了同样的问题并找到了解决方案。
Before rendering images you have to create a symbolic link from "public/storage" to "storage/app/public" using this command:
在渲染图像之前,您必须使用以下命令创建从“public/storage”到“storage/app/public”的符号链接:
php artisan storage:link
In "storage/app/public" you should have a folder "images" Now you can render this code in your markdown.blade.php:
在“storage/app/public”你应该有一个文件夹“images”现在你可以在你的markdown.blade.php中渲染这个代码:
}})
Second option is similar:
第二个选项类似:
}})
Both work fine
两者都工作正常
回答by Tony Chung
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');
}
}