【发布时间】: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 模板从未在传递给的属性中默认
MessageSending或MessageSent事件。因此,您实际上无法创建确切发送给用户的记录。 -
如果您查看MessageSent 事件的来源,第一个参数是
Swift_Message实例。你可以得到as a string,即$event->message->toString()。 -
@MartinBean 我会试一试,一会儿看看它的输出。
-
@MartinBean 它为您提供raw 电子邮件输出,不确定它是否可解析或者我什至会如何处理。