【问题标题】:Is there any event after sent email in Laravel在 Laravel 中发送电子邮件后是否有任何事件
【发布时间】:2020-08-03 16:29:49
【问题描述】:

我的应用流程如下所示:

  1. 我创建了一张发票,发票状态=created
  2. 我将其发送给用户,我使用 Queueable,因此发送电子邮件的过程与主应用程序分开。
  3. 发送电子邮件完成后,发票状态应为sent

问题是,对于 app/Mail 中的特定 Mailable 类,我找不到像 AfterEmailSent 这样的任何事件。

【问题讨论】:

    标签: laravel email


    【解决方案1】:

    您需要创建自己的事件,首先创建一个:

    php artisan make:event InvoiceEmailSentEvent

    Events\InvoiceEmailSentEvent.php 内添加以下内容:

     public $invoice;
    
     public function __construct($invoice)
        {
            $this->invoice = $invoice;
        }
    

    为事件创建一个监听器:

    php artisan make:listener InvoiceEmailSentListener

    对于Listeners\InvoiceEmailSentListener.php内的handle()函数,添加

    //import your Mailable up here
    
    public function handle(InvoiceEmailSentEvent $event)
        {
    
            // You don't have to sent email here, but I just added it if $invoice contains an email field
            $email = $event->invoice->email;
            Mail::to($email)->send(new Your_Mailable_Class_Goes_Here($event->invoice));
           // UPDATE INVOICE STATUS HERE
        }
    

    App\Providers\EventServiceProvider.php$listen 数组中注册您的事件和侦听器

      'App\Events\InvoiceEmailSentEvent' => [
                'App\Listeners\InvoiceEmailSentListener',
            ],
    

    最后,在您的控制器中,您可以调用事件,传递您要更新的发票,例如

     $invoice = Invoice::findOrFail($invoice_id)->first();
     event(new InvoiceEmailSentEvent($invoice));
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 2017-09-22
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多