Send mails to a list of email with queue jobs

Posted 2 years ago by Chiranjeevi

I've an application, in this I've to send a mail to a list of users. so I'm using queues for this as

$lists = ListUsers::where('list_id',$request->list_id)->pluck('email')->toArray();
            $jobs = (new SendEmailToList($lists));
            $this->dispatch($jobs);
            return 'success'; 

public $lists;
    public function __construct($lists)
    {
        $this->lists = $lists;
    }
    public function handle()
    {
        $lists = $this->lists;
        foreach ($lists as $key=>$value) {
            Mail::queue('email.test', array('email' => 'Sample'), function ($message) use ($value) {
                $message->to($value);
            });
 }
  }
 
[program:send_mail_queue]
command= php /var/www/html/project/artisan queue:listen
autostart=true
autorestart=true
stderr_logfile=/var/log/send_email_queue_err.log
stdout_logfile=/var/log/send_email_queue_out.log

And I've added a program to "queue:listen" in supervisor of my linux system to run in background as above: I've added all the required functionality for this sending email in queue jobs as given in Laravel Docs, but still it's not working. The requirement is to send mail to list after selecting in a form, but after submitting the details it's taking time to send mails to that list after it's showing success message, but user can't wait until all the mails sent. So it's must show success message directly after submitting form, and it's to be run in background for sending mail using Queue Jobs. Please help me in this, I'm new to laravel. Thank you

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.