2017-09-27 98 views
2

我有一个使用laravel 5.4构建的webapp。 现在我开发了一个函数发送给所有用户一个通信。知道队列中的邮件是否已发送Laravel 5.4

所以我要创建一个类可邮寄:

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, '[email protected]', "subject")); 
} 

而且它的工作原理,并把邮件在我的表乔布斯分贝,但我会知道是否有可能检查,当我运行时:

php工匠队列:听

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

回答

0

我找到了一个解决方案: 我创建了一个JOB

PHP工匠制作:工作sendComunicationEmail

而在工作中,我呼吁创建一个可邮寄类:

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, '[email protected]', "subject")); 
} 
相关问题