I try to send multiple email in queue laravel but its not working working single in single email, in my controller
$mail_id = Email::select('email')->whereNotNull('news')->get();
$emailid = [];
foreach($mail_id as $key => $value){
$emailid[] = $value->email;
}
$email = $tags = implode(',', $emailid);
$details = [
'emails' => $email,
'subject' => 'We add New News plese vist our news page and view there <a href="https://example.com/news">News</a>'
];
$job = (new \App\Jobs\SendEmailQueueJob($details))->delay(now()->addSeconds(2));
$send_job = dispatch($job);
and my SendEmailQueueJob file lclass SendEmailQueueJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
public $timeout = 7200; // 2hr
/**
* Create a new job instance.
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*/
public function handle(): void
{
$input[] = $this->details['emails'];
$email = new SendNewsAlert();
Mail::to($value->email)->send($input);
}
}
in single email its working fine but pass it show error in error log file
[2023-04-27 15:55:25] local.ERROR: Undefined array key "emaildata" {"exception":"[object] (ErrorException(code: 0): Undefined array key \"emaildata\" at C:\\xampp\\htdocs\\project\\app\\Jobs\\SendEmailQueueJob.php:36)
[stacktrace]
#0 C:\\xampp\\htdocs\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php(250): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Undefined array...', 'C:\\\\xampp\\\\htdocs...', 36)
why it's?
I think you are passing the email addresses as a string separated by commas. Try passing it as an array. So modify your handle
method like:
public function handle()
{
$emails = explode(',', $this->details['emails']);
foreach ($emails as $email) {
Mail::to($email)->send(new SendNewsAlert());
}
}