首页 > 解决方案 > Laravel 电子邮件在 Mailtrap 中显示图像正常但在 Gmail 中不可见

问题描述

我在下面使用了一个简单的 Laravel Mailable,并在电子邮件顶部显示了标题图像 - 通过 Mailtrap.io 时显示正常,但是当在实时环境中将相同的电子邮件发送到我的 Gmail 帐户时,它不会显示- 我究竟做错了什么?

class TwoFactorMail extends Mailable
{
    use Queueable, SerializesModels;

    public $code;

    public function __construct($code)
    {
        $this->code = $code;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.twofactor');
    }
}

刀片文件

@component('mail::message')
<html>
<head>
    <title>Two Factor</title>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins|Roboto&display=swap" rel="stylesheet">
</head>
<body>
<img src="data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/header.jpg')))}}" alt="">
        <h1 class="header padded-margin">Authentication code</h1><hr class="divider">
        <p>Your Two-Factor Authentication is below. Add the code into your browser to complete your CrowdControlHQ Sign In.</p>
        <div class="code padded-margin">{{ $code }}</div>
        <p>If you didn’t request this code there is nothing to worry about - you can safely ignore it.</p>
</body>
</html>
@endcomponent

标签: phplaravellaravel-mailmailtrap

解决方案


最好为您的图像标签使用基于标准 url 的 src。

只需确保您在 url 中包含域名,如下所示。

<img src="http://www.example.com/image.jpg" alt="img" />

大多数现代电子邮件客户端不接受 base64 图像。

你可以在这里读更多关于它的内容

https://blog.mailtrap.io/embedding-images-in-html-email-have-the-rules-changed/

搜索页面并阅读这两个主题

    1. 电子邮件中的内联嵌入或 base64 图像
    1. HTML 电子邮件中的链接图像或托管图片

推荐阅读