是否可以在 Laravel 的 HTML 电子邮件中包含图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29395740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:16:33  来源:igfitidea点击:

Is it possible to include an image in a HTML email in Laravel

phphtmlemaillaravelblade

提问by cchapman

I'm currently trying to send an HTML email via Laravel's Mail::send(). The view in which I'm using is a Blade template, but I'm not quite sure on how I would go about including an image in the body of the email.

我目前正在尝试通过 Laravel 的Mail::send(). 我使用的视图是 Blade 模板,但我不太确定如何在电子邮件正文中包含图像。

In my template, I have

在我的模板中,我有

<h4>You have successfully updated your Session!</h4>

@if ( $old_date != $date)
    <p>You have changed from {{ $old_date }} to {{ $date }}</p>
@endif

<p>If you have any questions or concerns, please contact the Office of Admissions</p>

{{ HTML::image('/images/full-logo.png', 'logo') }}

However, my email comes out with just a broken link icon in the body of the email. Is it possible to pass an image as a parameter to the view? In my Controller which sends the email, I have

但是,我的电子邮件在电子邮件正文中只有一个断开的链接图标。是否可以将图像作为参数传递给视图?在我发送电子邮件的控制器中,我有

$data = array (
                        'first_name'    => $student->first_name,
                        'last_name'     => $student->last_name,
                        'date'          => $day,
                        'old_date'      => $old_day,
                );


Mail::send ( '/emails/create_student', $data, function ($message) {
    ...
} );

So I'm not sure if it might be possible to include an image via the $dataarray.

所以我不确定是否可以通过$data数组包含图像。

采纳答案by Limon Monte

HTML::image()generates absolute path to image, like this:

HTML::image()生成图像的绝对路径,如下所示:

http://yoursite.dev/image.png

which works perfectly in your local environment.

这在您的本地环境中完美运行。

Mail client, as Gmail, will override image path with something like this:

邮件客户端,如 Gmail,将使用以下内容覆盖图像路径:

http://images.gmail.com/?src=http://yoursite.dev/image.png

Obviosly 3rd party service can't access your local domain and so you see broken image.

显然,第 3 方服务无法访问您的本地域,因此您会看到损坏的图像。

As a solution, you can upload images to 3rd party service like Amazon AWS and pass link to uploaded image to email template.

作为一种解决方案,您可以将图像上传到亚马逊 AWS 等第三方服务,并将上传图像的链接传递到电子邮件模板。

回答by ojb

This one worked for me: The image is declared in the email view

这个对我有用:图像在电子邮件视图中声明

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

from Laravel doc"A $message variable is always passed to e-mail views, and allows the inline embedding of attachments."

来自Laravel 文档“$message 变量始终传递给电子邮件视图,并允许内联嵌入附件。”