Php 如何呈现邮件的HTML?

Php 如何呈现邮件的HTML?,php,laravel,Php,Laravel,我有一个Mailable,它接受两个参数,当发送给用户时,在生成的视图中使用这两个参数。我想在Mailable发送后发出一个事件,但是,应该向该事件传递一个参数,该事件是Mailable生成的视图的字符串版本,并替换变量: namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use App\Events\Com

我有一个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
        ]);
    }
}
我试过:

  • 添加
    事件(新的CommunicationEvent($this->render())
    在build函数中调用
    $this->from([…])[…]之前和之后
  • 我尝试调用
    (new DummyMail($this->title,$this->name))->render()如建议。事件没有被触发,我的AJAX请求的响应也没有成功,
    storage/logs/laravel.log
    和apache2日志都没有帮助
  • 我尝试调用
    $this->buildMarkdownView()
    ,该键应该有一个名为
    html
    的键,该键应该为我提供模板,但是会抛出一个错误,显示“View[]not found.”

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

因此,我已经成功地呈现了模板并将其传递给事件,这可能不是最“有说服力”的解决方案,但是,它似乎完成了任务

  • 在您的邮件中,公开定义将使用的视图:

    public $view = 'mail.dummy-tpl';
    
  • build
    函数中,在
    $this->from>之后dummy@test.com“)[…]
    我们需要以字符串形式获取渲染模板:

    $tpl = view($this->view, [
        'title' => $this->title,
        'name' => $this->name,             
    ])->render();
    
    我注意到,您不需要使用
    render
    函数将渲染模板作为字符串返回,但是无论我在哪里查看,都建议使用
    render
    ,因此最好是显式的

  • 最后,使用助手调用您选择的事件:

  • 进行上述更改后,生成的可邮寄类现在应为:

    <?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));        
        }
    }
    
    Gotchas

    确保
    $view
    属性为public,否则将出现以下错误:

    对App\Mail\DummyMail::$view的访问级别必须是公共的(与类\Mail\Mailable相同)


    你想干什么?为什么在活动中需要邮件的正文?那是什么活动 / 它的听众会怎么做?@MartinBean我已经解决了这个问题,我的回答如下所示。基本上,我想记录通过事件发送的所有电子邮件-是的,我知道邮件事件,是的,我尝试过它们,但我面临的问题是,呈现的HTML模板从未出现在传递给默认
    MessageSending
    MessageSent
    事件的属性中。因此,您无法实际创建发送给用户的确切内容的记录。如果查看事件的源,第一个参数是
    Swift\u Message
    实例。你可以得到它,也就是说,
    $event->message->toString()
    @MartinBean我会试一试,看看它会输出什么,等一下。@MartinBean它会给你电子邮件输出,不确定它是否可解析,或者我会怎么做。
    <?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));        
        }
    }
    
    $this->from('dummy@test.com')->subject('Howdy!')->view('mail.dummy-tpl')->with([
        'title' => $this->title,
        'name' => $this->name
    ]);`