I have create a setting table in database for email settings. When email send function called I set my mail configuration on fly. It is working in Mail::send() but not in Mail::queue.
public function setMailConfig($from_address) {
$username = $from_address->email_address;
$fromname = $from_address->email_name;
$password = \Crypt::decrypt($from_address->password);
$smtpsecure = $from_address->sending_encryption;
$host = $from_address->sending_host;
$port = $from_address->sending_port;
$protocol = $from_address->sending_protocol;
$configs = [
'username' => $username,
'from' => ['address' => $username, 'name' => $fromname,],
'password' => $password,
'encryption' => $smtpsecure,
'host' => $host,
'port' => $port,
'driver' => $protocol,
];
foreach ($configs as $key => $config) {
if (is_array($config)) {
foreach ($config as $from) {
\Config::set('mail.' . $key, $config);
}
} else {
\Config::set('mail.' . $key, $config);
}
}
}
By default my queue driver is database. In job table it has populated. I called the command php artisan queue:listen --tries=1 . After 1 attempt queue saved to failed_job table.
public function laravelMail($to, $toname, $subject, $data, $cc, $attach) {
try {
$mail = Mail::queue('emails.mail', ['data' => $data], function ($m) use ($to, $subject, $toname, $cc, $attach) {
$m->to($to, $toname)->subject($subject);
if ($cc != null) {
foreach ($cc as $collaborator) {
//mail to collaborators
$collab_user_id = $collaborator->user_id;
$user_id_collab = User::where('id', '=', $collab_user_id)->first();
$collab_email = $user_id_collab->email;
$m->cc($collab_email);
}
}
// $mail->addBCC($bc);
if ($attach != null) {
$size = count($attach);
for ($i = 0; $i < $size; $i++) {
$file_name = $attach[$i]->getClientOriginalName();
$file_path = $attach[$i]->getRealPath();
$mime = $attach[$i]->getClientMimeType();
$m->attach($file_path, ['as' => $file_name, 'mime' => $mime]);
}
}
});
return $mail;
} catch (\Exception $e) {
\Log::info($e->getMessage());
}
}
I changed my .env file to MAIL_DRIVER= MAIL_HOST= MAIL_PORT= MAIL_USERNAME= MAIL_PASSWORD= CACHE_DRIVER= SESSION_DRIVER= QUEUE_DRIVER= But still Mail::queue is not working. Please suggest some idea.
What laravel version please?
laravel 5.2
Please provide the full version. :)
Laravel Framework version 5.2.39
Thanks.
Thanks, but I don't think this is an issue in the framework on invesitgation. Please feel free to ask around on the forums. We typically don't recommend setting config at runtime like that. If you need to construct a custom mailer, I'd recommend actually newing up this class manually.
Most helpful comment
Thanks.