【问题标题】:How to render the HTML of a Mailable?如何呈现可邮寄的 HTML?
【发布时间】:2019-04-30 16:12:56
【问题描述】:

我有一个 Mailable,它接受发送给用户时在生成的视图中使用的两个参数。我想在 Mailable 发送后发出一个事件,但是,应该将一个参数传递给该事件,这是由 Mailable 生成的视图的字符串版本,并替换了变量:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Events\CommunicationEvent;
use Illuminate\Contracts\Queue\ShouldQueue;

class DummyMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
    * Create a new message instance.
    *
    * @return void
    */
    public function __construct($title, $name)
    {
        $this->title = $title;
        $this->name = $name;
    }

    /**
    * Build the message.
    *
    * @return $this
    */
    public function build()
    {
        $this->from('dummy@test.com')->subject('Howdy!')->view('mail.dummy-tpl')->with([
            'title' => $this->title,
            'name' => $this->name
        ]);
    }
}

我试过了:

  • 在构建函数中的$this->from([...])[...]调用前后添加event(new CommunicationEvent($this->render()));
  • 我已尝试按照建议的here 致电(new DummyMail($this->title, $this->name))->render();。该事件没有被触发,我的 AJAX 请求的响应也没有成功,storage/logs/laravel.log 和 apache2 日志都没有帮助。
  • 我尝试调用 $this->buildMarkdownView(),它应该有一个名为 html 的键,它应该给我模板,但是会引发错误,提示“View [] not found.”。李>

那么,我怎样才能简单地将为电子邮件生成的视图作为字符串返回,以便可以将其传递给我计划发出的事件?

【问题讨论】:

  • 你想做什么?为什么在活动中需要邮件正文?该事件 / 它的监听器做了什么?
  • @MartinBean 我已经解决了这个问题,如下面的回答所示。本质上,我想记录所有通过事件发送的电子邮件 - 是的,我知道邮件事件,是的,我已经尝试过了,但我面临的问题是渲染的 HTML 模板从未在传递给的属性中默认MessageSendingMessageSent 事件。因此,您实际上无法创建确切发送给用户的记录。
  • 如果您查看MessageSent 事件的来源,第一个参数是Swift_Message 实例。你可以得到as a string,即$event->message->toString()
  • @MartinBean 我会试一试,一会儿看看它的输出。
  • @MartinBean 它为您提供raw 电子邮件输出,不确定它是否可解析或者我什至会如何处理。

标签: php laravel


【解决方案1】:

所以,我已经设法渲染模板并将其传递给事件,它可能不是最“雄辩”的解决方案,但它似乎完成了这项工作。

  1. 在您的 Mailable 中公开定义将使用的视图:

    public $view = 'mail.dummy-tpl';
    
  2. build函数中,在$this->from('dummy@test.com')[...]之后我们需要将渲染后的模板作为字符串获取:

    $tpl = view($this->view, [
        'title' => $this->title,
        'name' => $this->name,             
    ])->render();
    

    我注意到您不需要使用render 函数将呈现的模板作为字符串返回,但是在我看到的任何地方都建议使用render,所以最好是明确的。

  3. 最后,使用 event 助手调用您选择的事件:

    event(new CommunicationEvent($tpl, $this));
    

进行上述更改后,生成的 Mailable 类现在应该是:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Events\CommunicationEvent;
use Illuminate\Contracts\Queue\ShouldQueue;

class DummyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $view = 'mail.dummy-tpl';

    /**
    * Create a new message instance.
    *
    * @return void
    */
    public function __construct($title, $name)
    {
        $this->title = $title;
        $this->name = $name;
    }

    /**
    * Build the message.
    *
    * @return $this
    */
    public function build()
    {
        $this->from('dummy@test.com')->subject('Howdy!')->with([
            'title' => $this->title,
            'name' => $this->name
        ]);

        $tpl = view($this->view, [
            'title' => $this->title,
            'name' => $this->name,             
        ])->render();

        event(new CommunicationEvent($tpl, $this));        
    }
}

注意:由于我们已经公开定义了属性$view,因此我们不再需要以下代码中的view 链:

$this->from('dummy@test.com')->subject('Howdy!')->view('mail.dummy-tpl')->with([
    'title' => $this->title,
    'name' => $this->name
]);`  

陷阱

确保$view 属性是公开的,否则会出现以下错误:

App\Mail\DummyMail::$view 的访问级别必须是公共的(如 Illuminate\Mail\Mailable 类)

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 2021-06-03
    • 2012-07-31
    • 2011-08-02
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多