Laravel Change Mail Driver Dynamically Example
Hello,
In this post, we will learn laravel change mail driver dynamically. let’s discuss about laravel change mail driver on the fly. This tutorial will give you a simple example of how to use multiple mail drivers in laravel. let’s discuss about laravel mailer example. Alright, let’s dive into the details.
You can use these tips with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
You can define multiple mail drivers like smtp, ses, mailgun, postmark etc in laravel. You can also use that drivers dynamically to send using mailer() method. so let's see the following example to send email with dynamic mail driver.
Define Multiple Mail Driver Configuration:
You need to go config/mail.php and set multiple connection as like the below:
config/mail.php
<?php
return [
...
...
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
....
Send Email Code:
You can follow the below tutorial to sending email from laravel. Then the below code i will show you how to send cc and bcc emails.
Follow Mail Sending Tutorial: Laravel Mail | Laravel Send Email Tutorial
.
Send Email with Different Mail Driver:
You need to update the following controller code:
app/Http/Controllers/MailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from ItSolutionStuff.com',
'body' => 'This is for testing email using smtp.'
];
Mail::mailer('postmark')
->to('your_email@gmail.com')
->send(new DemoMail($mailData));
Mail::mailer('mandrill')
->to('your_email@gmail.com')
->send(new DemoMail($mailData));
Mail::mailer('ses')
->to('your_email@gmail.com')
->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to set CC And BCC Email Address In Laravel Mail?
- Laravel 9 Send Mail using Gmail SMTP Server
- Laravel 9 Markdown | Laravel 9 Send Email using Markdown Mailables
- Laravel 9 Mail | Laravel 9 Send Email Tutorial
- Laravel - How to Set Subject in Mail?
- How to Change From Name in Laravel Mail?
- How to Send Mail using Sendinblue in Laravel?
- How to Send Mail using Mailjet in Laravel?
- Laravel Send Mail using Mailgun Example
- Laravel Send Mail using Mailtrap Example
- How to Send Mail using Sendgrid in Laravel?
- Laravel Mailgun Setup Example
- How to Send Mail using Gmail SMTP in Laravel?