2017-09-27 67 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")); 
} 
相關問題