【问题标题】:Laravel - send mail to queue with different connectionLaravel - 将邮件发送到具有不同连接的队列
【发布时间】:2020-12-25 18:05:20
【问题描述】:

我使用此代码发送发票

        Mail::send($email_template, $view_vars, static function ($m) use ($customer, $company, $email_subject, $users, $invoice, $invoice_name) {
        $m->from($company->send_email, $company->name);
        $m->to($users, $customer->getFullName())->subject($email_subject);
        $m->attachData($invoice, $invoice_name);
    });

使用 atache 发送电子邮件需要一些时间的问题。 决定 - 使用 Queue 并将 Mail::send() 更改为 Mail::queue()

但我使用 iron.io 作为 QUEUE 驱动程序。默认情况下,带有附件的电子邮件会发送到 iron.io - 所以它也需要一些时间。

我想在这种情况下使用数据库连接。

我可以使用代码吗:

Mail::queue(.......)->onConnection('database');

?

Jobs 已经存在。 在此之后,我需要一些额外的代码,电子邮件是真正在后台发送的,它会自动完成吗?

【问题讨论】:

    标签: php laravel email queue


    【解决方案1】:

    最好的方法是对所有队列使用本地连接方法(数据库)。

    【讨论】:

      【解决方案2】:

      根据 Laravel 文档,您可以将 onConnection 与队列一起使用。 https://laravel.com/docs/8.x/queues#chain-connection-queue

      要在邮件发送后做额外的处理,你需要监听 Laravel 的 MailSent 事件。 https://laravel.com/api/5.5/Illuminate/Mail/Events/MessageSent.html

      1. 在 EventServiceProvider 中定义监听器
      'Illuminate\Mail\Events\MessageSent' => [
         'App\Listeners\OnMessageSent',
      ]
      
      1. 在监听器中定义句柄函数。
      use Illuminate\Mail\Events\MessageSent;
      
      class OnMessageSent {
      
          // listen for MailSent event
          public function handle(MessageSent $event)
          {
              if ($event) {
                  // do something here
                  dd($event);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-13
        • 2022-01-24
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        • 2019-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多