未收到来自 Laravel HTML 可邮寄(非 Markdown)视图中的 $message 变量

Not receiving the $message variable in view from a Laravel HTML Mailable (NON Markdown)

我读过几个与此问题相关的类似问题,但都涉及 Markdown 邮件。

我正在尝试在邮件中发送内联图像,但我还没有找到正确的方法 (Laravel 5.5)。

documentation 是这样说的:

Inline Attachments

Embedding inline images into your emails is typically cumbersome; however, Laravel provides a convenient way to attach images to your emails and retrieving the appropriate CID. 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:

<body>
    Here is an image:

    <img src="{{ $message->embed($pathToFile) }}">
</body>

但是,这样做时我收到了这个错误:

Undefined variable: message (View: /path/to/project/resources/views/mails/new_user_welcome.blade.php)

我知道这在使用 Markdown 消息时有限制,但 我没有使用


这是相关文件:

Mail/NewUserWelcomeEmail.php

class NewUserWelcomeEmail extends Mailable
{
    use SerializesModels;

    public function build()
    {
        return $this->view('mails.new_user_welcome');
    }
}

Resources/views/mails/new_user_welcome.blade.php

@extends('layouts.mail')

@section('content')

    <img src="{{ $message->embed(url("storage/images/inline_image.png")) }}" 
    alt="An inline image" />

@endsection

App/Http/Controllers/UserController.php

public function register(NewUserRequest $request)
{
    // some code

    Mail::to($user)->send(new NewUserWelcomeEmail($user));

    return 'done';
}

如果你能像这样使用而不是它可以工作你不要在邮件中使用 $message 变量 blade

Mail::send('emails.welcome', $data, function ($message) {
    $message->from('us@example.com', 'Laravel');
    $message->to('foo@example.com')->cc('bar@example.com'); 
});

如果你不想使用这种方法,你可以像这样使用

https://code.tutsplus.com/tutorials/how-to-send-emails-in-laravel--cms-30046

可以这样工作

在我的例子中(Larvel 5.5),我已经设法修改 header 标志,在 html 和降价

Laravel 文档,虽然真的很棒,但在这方面可以做得更好。

无论如何,按照这些步骤,你应该没问题...

1 - 通过以下方式发布邮件模板:

php artisan vendor:publish --tag=laravel-mail

这样您就可以轻松修改您的邮件源文件。

2 - 修改 resources/views/vendor/mail/html 中的 message.blade.php 为:

@slot('header')
    @component('mail::header', ['url' => config('app.url')])
        <img src="{{asset('assets/img/pathToYourImage...')}}">
    @endcomponent
@endslot

3 - 从现在开始,您所有的电子邮件都应该通过 CID 接收徽标。

注:

在此示例中Laravel,自动将资产转换为 CID,因此您根本不需要调用 $message->embed(......

请使用这些 html/markdown 目录和 blade 指令进行广泛测试。这有点棘手,但它确实有它的魔力...

您必须在 Mailable 中将文件路径变量定义为 public 属性 -> 示例 $pathToFile。

如果您有来自可邮寄件之外的文件路径,您可以使用构造函数传入。

class NewUserWelcomeEmail extends Mailable
{
    use SerializesModels;

    // Must be public    
    public $pathToFile;

    /**
    * Create a new message instance.
    */
    public function __construct(string $pathToFile)
    {
        $this->pathToFile= $pathToFile;
    }

    public function build()
    {
        return $this->view('mails.new_user_welcome');
    }
}

然后它在您的视图中按预期工作,如下所示:

@extends('layouts.mail')

@section('content')

    <img src="{{ $message->embed(url($pathToFile)) }}" alt="An inline image" />

@endsection

嗯,老实说,我还没有找到使它正常工作的方法。我的意思是,就目前而言,这应该有效。也许是我的 Laravel 安装 (?)..

无论如何,我确实使用了变通方法。

1) 使用 Eduardokum 的 Laravel Mail Auto Embed 包,这基本上会为您的每个媒体资产生成一个 CID

但是在添加这个包之后,这并没有像预期的那样工作..所以我:

2) 更改我引用我的资产的方式,从此:

   <img src="{{ url("storage/inline_image.png") }}" />

为此:

   <img src="{{ asset("storage/inline_image.png") }}" />

现在可以了。

通过这种方式,您可以在 markdown Laravel 邮件中嵌入图像(此处用于嵌入徽标):

app/Mail/Demo.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Swift_Image;

class Demo extends Mailable
{
    use Queueable, SerializesModels;

    public $image_logo_cid;

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {   
        // Generate a CID 
        $this->image_logo_cid = \Swift_DependencyContainer::getInstance()
            ->lookup('mime.idgenerator')
            ->generateId();

        return $this->withSwiftMessage(function (\Swift_Message $swift) {
            $image = Swift_Image::fromPath(resource_path('img/make-a-wish-logo-rev.gif'));
            $swift->embed($image->setId($this->image_logo_cid));
        })->subject('My awesome markdown mail with an embedded image')
            ->markdown('emails.demo');
    }
}

resources/views/emails/demo.blade.php

@component('mail::message')
    {{-- Just embedding this image in the content here --}}
    <img src="cid:{{ $image_logo_cid }}">
@endcomponent

或者您可以嵌入 mail::layout 组件并将图像放入您的 header:

resources/views/emails/demo.blade.php

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            <img src="cid:{{ $image_logo_cid }}">
        @endcomponent
    @endslot

    {{-- Body --}}
    <!-- Body here -->

    {{-- Subcopy --}}
    @slot('subcopy')
        @component('mail::subcopy')
            <!-- subcopy here -->
        @endcomponent
    @endslot


    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            <!-- footer here -->
        @endcomponent
    @endslot
@endcomponent

或者,如果您总是想要这个 header,只需编辑 resources/views/vendor/mail/html/header.blade.php 文件(在 php artisan vendor:publish --tag=laravel-mail 之后可用)。然后当然你需要创建 CID/Image 每个可邮寄的 app/Mail/Demo.php (或者有一个基本控制器)。