Share this:

Hello dev, Today we are going to learn Laravel Change Mail Driver Dynamically Example. This tutorial will cover on change mail driver dynamically in laravel application with example.

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 and laravel 9 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.

Also Read: Laravel Mail Send with PDF Attachment Example

Example on Laravel Change Mail Driver Dynamically

Define Multiple Mail Driver Configuration:

You need to go config/mail.php and set multiple connection as like the below:


return [
    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', ''),
            '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 we will show you how to send cc and bcc emails.

How to set CC And BCC Email Address In Laravel Mail?

Also Read: How to Check Running Laravel App Environment?

Send Email with Different Mail Driver:

You need to update the following controller code:


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',
            'body' => 'This is for testing email using smtp.'
                ->send(new DemoMail($mailData));
                ->send(new DemoMail($mailData));
                ->send(new DemoMail($mailData));
        dd("Email is sent successfully.");


Today, We had learn Laravel Change Mail Driver Dynamically Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Check If Request Has File in Laravel?

Share this:

Categorized in: