如何在Laravel中排队发送电子邮件 [英] How Send emails in queue in Laravel

查看:115
本文介绍了如何在Laravel中排队发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在队列中发送电子邮件,但是没有用.

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

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

推荐答案

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

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

  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. 火灾排队:php artisan queue:work database --tries=1(在所有未完成的尝试之后,它将注册在失败的作业表中)
  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)

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

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:

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.

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

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:

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

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

如果您有要始终排队的可邮递类,则可以在该类上实现ShouldQueue契约.现在,即使您在邮寄时调用send方法,由于可实现的协定,可邮寄邮件仍将排队:

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:

use Illuminate\Contracts\Queue\ShouldQueue;

class OrderShipped extends Mailable implements ShouldQueue
{
    //
}

这篇关于如何在Laravel中排队发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆