Laravel 如何在队列中发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49276803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:30:43  来源:igfitidea点击:

How Send emails in queue in Laravel

laravelemail

提问by nitinsridar

I tried to send email in a queue, but not working.

我试图在队列中发送电子邮件,但没有成功。

Mail::queue('emails.mailsubscribe', ['email'=>$email], 
    function($message) use($email)
    {
       $message->to('[email protected]')->subject('Subscribe: XXXXX');
    });

回答by Adam Kozlowski

In order to make Laravel 5/6full queuing you need make below steps:

为了进行Laravel 5/6完全排队,您需要执行以下步骤:

  1. php artisan queue:table(for jobs)
  2. php artisan queue:failed-table(for failed jobs)
  3. php artisan migrate
  4. Set in .env QUEUE_DRIVER=database
  5. Fire: php artisan config:cache
  6. Fire queuing: php artisan queue:work database --tries=1(after all uncompleted tries it will be registered in failed jobs table)
  1. php artisan queue:table(工作)
  2. php artisan queue:failed-table(对于失败的工作)
  3. php artisan migrate
  4. 在 .env 中设置 QUEUE_DRIVER=database
  5. 火: php artisan config:cache
  6. Fire queuing:(php artisan queue:work database --tries=1在所有未完成的尝试之后,它将被注册到失败的作业表中)

Since sending email messages can drastically lengthen the response time of your application, many developers choose to queue email messages for background sending. Laravel makes this easy using its built-in unified queue API. To queue a mail message, use the queue method on the Mail facade after specifying the message's recipients:

由于发送电子邮件消息会大大延长应用程序的响应时间,因此许多开发人员选择将电子邮件消息排队以进行后台发送。Laravel 使用其内置的统一队列 API 使这变得容易。要对邮件消息进行排队,请在指定邮件的收件人后使用 Mail 外观上的 queue 方法:

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->queue(new OrderShipped($order));

This method will automatically take care of pushing a job onto the queue so the message is sent in the background. Of course, you will need to configure your queues before using this feature.

此方法将自动将作业推送到队列中,以便在后台发送消息。当然,您需要在使用此功能之前配置您的队列。

If you wish to delay the delivery of a queued email message, you may use the later method. As its first argument, the later method accepts a DateTime instance indicating when the message should be sent:

如果您希望延迟发送排队的电子邮件,您可以使用后一种方法。作为它的第一个参数,后面的方法接受一个 DateTime 实例,指示何时应该发送消息:

$when = now()->addMinutes(10);

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->later($when, new OrderShipped($order));

If you have mailable classes that you want to always be queued, you may implement the ShouldQueue contract on the class. Now, even if you call the send method when mailing, the mailable will still be queued since it implements the contract:

如果您有希望始终排队的可邮寄类,则可以在该类上实现 ShouldQueue 协定。现在,即使您在邮寄时调用 send 方法,由于它实现了合同,可邮寄物品仍将排队:

use Illuminate\Contracts\Queue\ShouldQueue;

class OrderShipped extends Mailable implements ShouldQueue
{
    //
}