Back to Homepage

Laravel Mail Switcher Package

3 min read


Laravel Mail Switcher Package

We'll explore the Laravel Mail Switcher and walk you through its installation steps. Laravel is one of the most popular PHP frameworks when it comes to web applications, as email is a cornerstone of modern communication. Although Laravel has an excellent built-in email system, sometimes you need more control. In this article,

Laravel Mail Switcher: An Overview

Designed to improve the flexibility and control of email configuration in your Laravel projects. By switching between different mail drivers with this package, you can manage a variety of email services or configurations for a variety of environments more easily.

Key Features of Laravel Mail Switcher

  • Using Laravel Mail Switcher, you can switch between different email configurations dynamically, which is particularly useful if you need different settings for local development, staging, and production.

  • You can use a variety of email drivers, such as SMTP, Mailgun, Sendmail, and more, to meet your specific email requirements.

  • The installation and configuration is straightforward, making it accessible to developers of all levels.

Steps for installation

Here are the steps for installing:

To install the Laravel Mail Switcher package, you'll need Composer. In your project's root directory, run:

composer require sethsandaru/laravel-mail-switcher

Step 2: Migrate

php artisan migrate

Step 3: Configuration

Once the package is installed, you can add email credential by running below php artisan command

//To add
php artisan ms:add

// To Delete
php artisan ms:delete {credentialId}

//Reset Threshold of expired Credentials
php artisan ms:reset

Step 4: Configuration File

Open the config/mail-switcher.php file in your text editor. Here, you can define different mail configurations for various environments. For example:

return [
    'default' => env('MAIL_DRIVER', 'smtp'),
    'connections' => [
        'smtp' => [
            'driver' => 'smtp',
            'host' => env('MAIL_HOST', ''),
            'port' => env('MAIL_PORT', 587),
            'username' => env('MAIL_USERNAME', 'username'),
            'password' => env('MAIL_PASSWORD', 'password'),
        'mailgun' => [
            'driver' => 'mailgun',
            'domain' => env('MAILGUN_DOMAIN', 'domain'),
            'secret' => env('MAILGUN_SECRET', 'secret'),
        // Add more configurations as needed

Step 5: Usage

To switch between mail drivers, you can use the MailSwitcher facade in your Laravel application. For example:

use MailSwitcher;

MailSwitcher::to('mailgun')->send(new MyMailable());

You can change the 'mailgun' value to any configuration defined in your mail-switcher.php file.


Laravel Mail Switcher simplifies email configuration management in Laravel projects. With its dynamic configuration capabilities and support for multiple drivers, it's a valuable addition to your Laravel development toolkit. With these installation steps, you can seamlessly integrate it into your project and take control of your email settings.

Complete Documentation can be found at the repository

Follow @LaravelSage on X → Follow @LaravelSage on Facebook →
Aniket Singh

Aniket Singh

View All Articles

Full-stack developer with a knack for Merging creativity with technical expertise for standout solutions.

Related Articles

data_forget Helper for Laravel

data_forget Helper for Laravel

Since Laravel version 10.15, there is a new utility function called data_forget that allows you to remove keys from an array or object using a "dot" notation.

Laravel Tenant Application with Tenancy

Laravel Tenant Application with Tenancy

You can make your Laravel app multi-tenant using the Tenancy for Laravel Tenant package. This tenancy package lets you make any Laravel application multi-tenant without rewriting it.

Top Laravel Packages for Building Powerful Applications

Top Laravel Packages for Building Powerful Applications

Are you ready to take your Laravel skills to the next level and build extraordinary applications? Look no further! In this blog post, we will unveil a treasure trove of top packages that will revolutionize your development process.

MJML PHP: Simplifying Email Template Creation

MJML PHP: Simplifying Email Template Creation

The MJML PHP package by Spatie can help you create email templates more easily. But there is a catch. It can be difficult to craft visually appealing and functional email templates that work across a variety of email clients.

Subscribe for 20+ new Laravel tutorials every week

You can unsubscribe at any time. You'll also get -20% off my courses!

© 2024


Laravel Sage

   |    Privacy Policy