【问题标题】:How can I display image in email layout on the laravel?如何在 laravel 的电子邮件布局中显示图像?
【发布时间】:2018-02-12 12:24:57
【问题描述】:

我这样发送电子邮件的代码:

public function toMail($notifiable)
{
    return (new MailMessage)
                ->subject('Test')
                ->markdown('vendor.mail.markdown.message', ['data' => $this->data]);
}

我的 message.blade 视图如下:

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            {{ config('app.name') }}
        @endcomponent
    @endslot

    {{-- Body --}}
    This is your logo 
    ![Some option text][logo]
    [logo]: {{asset('img/my-logo.png')}} "Logo"  

    {{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset

    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
        @endcomponent
    @endslot
@endcomponent

发送邮件成功,但邮件内容如下:

This is your logo 
![Some option text][logo]

[logo]: http://myshop.dev/img/my-logo.png "Logo"

图片不显示

我该如何解决这个问题?

我关注这个参考:https://medium.com/@adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c

【问题讨论】:

  • Laravel 文档显示它没有用于图像的电子邮件组件。您很可能必须自己制作。
  • @Stuart Murphy,你是什么意思?尽量详细回答
  • 您是从本地发送电子邮件吗?
  • @Ashish Patel,是的。我从本地发送电子邮件
  • 好的,然后让您的应用上线。然后按照@sletheren 的步骤进行操作。

标签: laravel email notifications laravel-5.4


【解决方案1】:

这对我有用!

<img src="{{$message->embed(asset('images/image_name.png'))}}">

对于那些有未定义变量:消息的人: 您需要将变量 $message 发送到视图,而 $messageMailable 实例。
示例:

$this->view('view_name_here')
     ->with(['message' => $this])
     ->subject("Hello..");

【讨论】:

  • +1 获取关于 $message 的注释。文档说“请记住,Laravel 会自动使 $message 变量可用于您的所有电子邮件视图”,至少在使用 NotificationMailable 时并非如此。
  • 小心这种方法,在我的例子中 PHP (7.2) 返回“致命错误:允许的内存大小耗尽错误”。
【解决方案2】:

这是一个老问题,但这可能对其他人有所帮助。试试这个...(假设您想用您的徽标替换应用名称)

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            <img src="{{ asset('img/my-logo.png') }}" alt="{{ config('app.name') }} Logo">
        @endcomponent
    @endslot

    {{-- Body --}}
    {{ $slot }}

    {{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset

    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
        @endcomponent
    @endslot
@endcomponent

当您扩展 'vendor.mail.markdown.message' 时,您明确表示呈现的邮件将仅使用此视图,即 markdown,您完全忽略了将要发送的 HTML 电子邮件。

Markdown 电子邮件具有 HTML 版本和原始文本版本,您看不到对 Markdown 文件所做的更改,因为您正在查看电子邮件的 HTML 版本,因此请查看您必须更新的那些更改@ 987654323@不仅是markdown一个。

【讨论】:

    【解决方案3】:
    @component('mail::layout')
        {{-- Header --}}
        @slot('header')
            @component('mail::header', ['url' => config('app.url')])
                {{ config('app.name') }}
            @endcomponent
        @endslot
    
        {{-- Body --}}
        This is your logo 
        ![Some option text][logo]
        [logo]: <img src="{{url('/img/my-logo.png')}}" style="height: 100px;"/>  "Logo"  
    
        {{-- Subcopy --}}
        @isset($subcopy)
            @slot('subcopy')
                @component('mail::subcopy')
                    {{ $subcopy }}
                @endcomponent
            @endslot
        @endisset
    
        {{-- Footer --}}
        @slot('footer')
            @component('mail::footer')
                &copy; {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
            @endcomponent
        @endslot
    @endcomponent
    

    请参阅我已在您的图像应位于的位置添加了一个标签。 url('/img/my-logo.png') 转换为您存储图像的完整站点地址。

    【讨论】:

      【解决方案4】:

      尝试在&lt;img&gt;标签内输出图片:

      <img src="{{ $message->embed(asset('img/my-logo.png')) }}" alt="alt here">
      

      【讨论】:

      • 是一样的。结果如下:&lt;img src="http://myshop.dev/img/my-logo.png" alt="alt here"&gt; 。我使用:laravel.com/docs/5.4/mail。好像不一样
      • 使用 laravel 的 embed 方法怎么样?检查编辑的答案
      • 应该通过:Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, you should avoid passing a message variable in your view payload
      • 我没有传递消息变量
      • 如果使用 laravel embed,那么在 toMail 方法中像什么?
      猜你喜欢
      • 2018-02-13
      • 2011-04-03
      • 1970-01-01
      • 2011-09-06
      • 2020-08-20
      • 2012-06-23
      • 2011-10-17
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多