How to Use Mail::Queue in Laravel

827 Views Asked by At

I want to send emails to 100 users. Mail::send() is taking too much load time and isn't able to cover all emails of users. I'm trying to use Mail::queue() in my application, but I'm getting the error below while running

php artisan queue:listen.

[ErrorExcepton] Undefined Property : SuperClosure\SerializableClosure::$binding.

Updated .env file with QUEUE_DRIVER = database.

Please help me find the solution to resolve this. Also, I am using the same code to run background jobs, using Laravel 5.3.

Here is my code

namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\DB;
class ProbationCronJobEmail extends Command
{
protected $signature = 'hrm:notify';
protected $description = "";

public function __construct()
{
 parent::__construct();
}

public function handle()
{
 $email = '[email protected]';
\Mail::queue('emails.probation', 
['empname'=>'abc','id'=>'123'],function($msg) use($email){
$msg->from('[email protected]');
$msg->to($email);
$msg->subject('Probation List as on '.date('Y-M-d'));
});
}
}    

abc@gmail is a dummy email, i am using my corporate email instead.

if i simple type php artisan hrm:notify, getting no error.

1

There are 1 best solutions below

0
Alberto Sanchez On

I have a little example. I hope this will help.

<?php   
   public function mails_meeting($meeting, $group, $place, $date, $message, $user)
    {
        $subject = "meeting " . $group;

        $cargos = Cargo::where('comision_id', '=', $meeting->comision_id)->where('active', '=', '1')->get();
        foreach ($cargos as $cargo) {
            $mail_reciever = $cargo->asambleista->user->email;
            Mail::queue('correos.comision_mail', ['group' => $group, 'place' => $place,
                'date' => $date,  'message' => $message, 'user' => $user],
                function ($mail) use ($subject, $mail_reciever) {
                    $mail->from('[email protected]', 'Automatic mail system');
                    $mail->to($mail_reciever);
                    $mail->subject($subject);
                });
        }
        return 0;
    }

In your_app/config/mail.php .

'sendmail' => '/usr/sbin/sendmail -bs',
    'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ],

.env file

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=your_conf