首页 > 解决方案 > 从 MailMessage 对象获取内容

问题描述

我正在尝试创建一个系统,该系统将根据他们的帐户自动记录我的网站发送给用户的任何电子邮件通知的内容。

我通过监听NotificationSent事件来做到这一点,这使我可以轻松访问Notifiable(我想要存储我的日志条目Notification的对象)和(定义已发送消息的对象)。

使用这些我可以得到MailMessage对象,但我不知道如何渲染它。我希望它有一个渲染方法,但我找不到。大概还有其他一些对象接受MailMessage并进行渲染。有什么线索吗?

理想情况下,我想要电子邮件的纯文本版本(降价)

谢谢你的帮助

标签: laravelmarkdown

解决方案


我有一个正在做我想做的事情的解决方案。我蚕食了我为其他东西编写的一些代码,这让我走了很长一段路。有些步骤我不完全理解,所以这里可能会有一些不必要的膨胀:

class LogNotification
{
    public function handle(NotificationSent $event)
    {
        //getting the MailMessage object
        $mailMsg = $event->notification->toMail($event->notifiable);

        //I think this is getting the additional variables from the 
        //MailMessage that are needed to render the view
        $msgData = array_merge($mailMsg->toArray(),$mailMsg->viewData);

        //I don't fully understand this. I get that the Markdown object is 
        //going to do the rendering, but I don't know why it needs to be 
        //instantiated with an empty View object
        $markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));

        //Pass the renderText method the path to the markdown, and the message data
        $msgContent = $markdown->renderText($mailMsg->markdown, $msgData);
    }
}

希望这对某人有所帮助(如果有人可以对实例化 Markdown 对象时发生的事情提供适当的解释,我将不胜感激)。


推荐阅读