知道队列中的邮件是否已发送到Laravel 5.4

时间:2017-09-27 08:32:07

标签: php email laravel-5

我有一个使用laravel 5.4构建的webapp。 现在我开发了一个向所有用户发送通信的功能。

所以我创建了一个Mailable类:

class Comunication extends Mailable
{
    use Queueable, SerializesModels;

    private $data;
    private $recipient;
    private $fromR;
    private $subjectR;
    private $template;

    public function __construct($template, $data,$recipient,$from,$subject)
    {
        $this->data = $data;
        $this->recipient = $recipient;
        $this->fromR = $from;
        $this->subjectR = $subject;
        $this->viewData = $data;
        $this->template = $template;
    }
    public function build()
    {
        return $this->from($this->fromR)->to($this->recipient)->subject($this->subjectR)->view( $this->template, $this->viewData);
    }

在我的控制器中,我有一个函数发送,如:

foreach ($users as $user){
   Mail::queue(new Comunication('mail.comunication', array("user"=>"test"), $user->email, 'mytest@test.it', "subject"));
}

它的工作原理并在我的表中将一封邮件放在db上,但是当我运行时,我会知道是否可以检查:

  

php artisan queue:listen

如果邮件是真实发送或在失败的作业中完成。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案: 我用

创建了 JOB
  

php artisan make:job sendComunicationEmail

在工作中我调用了一个Mailable类:

class ComunicationJobEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data;
    private $recipient;
    private $fromR;
    private $subjectR;
    private $template;

    public function __construct($template, $data, $recipient, $from, $subject)
    {
        //
        $this->data = $data;
        $this->recipient = $recipient;
        $this->fromR = $from;
        $this->subjectR = $subject;
        $this->viewData = $data;
        $this->template = $template;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //new mailable classe created
        Mail::send(new Comunication($this->template, $this->data, $this->recipient, $this->fromR, $this->subjectR));
    }
    public function failed()
    {
        // Called when the job is failing...
        Log::alert('error in queue mail');

    }
}

现在在我的控制器中有:

foreach ($users as $user){
   dispatch(new ComunicationJobEmail('view', array("data"=>""), $user->email, 'from@from.it', "subject"));
}