2
votes

I have a webapp built with laravel 5.4. Now I have developed a function that send to all users a comunication.

So i have create a class 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);
    }

And in my controller I have a function send like:

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

And it works and put a mail in my table Jobs on db, but I would know if is possible check, when I run:

php artisan queue:listen

If mail is real sent or finish in failed jobs.

1

1 Answers

1
votes

I found a solution: I have created a JOB with

php artisan make:job sendComunicationEmail

And in job I call a Mailable class created:

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');

    }
}

And in my controller now there is:

foreach ($users as $user){
   dispatch(new ComunicationJobEmail('view', array("data"=>""), $user->email, '[email protected]', "subject"));
}