Allowing users to send emails with their own SMTP settings in Laravel 9
Tags: php, laravel, refactoring, webdev
Published: 2023-01-22 22:09:57

Various guides exist on how to do this in Laravel, but all the ones I found ([this](https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel) and [this](https://blog.creekorful.org/2021/11/laravel-dynamic-smtp-mail-configuration/)) worked only up to Laravel 8. The reason is that in v9 the Swift Mailer was no longer supported in favor of [Symfony Mailer](https://laravel.com/docs/9.x/releases#symfony-mailer). The Laravel docs show how to write your own custom transports, but that's not what we need here. There might be a package that sends emails with custom SMTP params when reading this post, so you might want to take a look first.

## What Laravel does behind the scenes

The first thing we want to handle is related to a class called `MailManager`. It is responsible for configuring and resolving the various transport drivers. We already know that we want to use the SMTP transport, we just need to know how to override the default configuration coming from the `.env` file. This `MailManager` has a method for that:

```php
protected function getConfig(string $name)
{
    return $this->app['config']['mail.driver']
        ? $this->app['config']['mail']
        : $this->app['config']["mail.mailers.{$name}"];
}
```

This is the method that we want to override. Before we do that, we need to know how the `MailManager` is resolved and used by the framework. In the framework's `MailServiceProvider` we find this piece of code:

```php
protected function registerIlluminateMailer()
{
    $this->app->singleton('mail.manager', function ($app) {
        return new MailManager($app);
    });
    $this->app->bind('mailer', function ($app) {
        return $app->make('mail.manager')->mailer();
    });
}
```

This tells us that to work with another mailer instead of the default one, we have to use another instance of a `MailManager` and get the mailer from it.

## Implementing the custom Mailer

So we make this new class called `App\Extensions\UserMailManager`, place it or call it anything you want, that accepts a configuration when created and overrides the `getConfig()` method we saw earlier to use that configuration.

```php
namespace App\Extensions;

use Illuminate\Mail\MailManager;

class UserMailManager extends MailManager
{
    public function __construct($app, private readonly array $customConfig)
    {
        parent::__construct($app);
    }

    protected function getConfig(string $name)
    {
        return $this->customConfig;
    }
}
```

In your `AppServiceProvider`, or any other provider, we bind an instance of this new `MailManager` to the container, which will allow us to pass custom SMTP parameters when needed. It could've been a singleton, not sure.

```php
class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind('user.mailer', function ($app, $parameters) {
            return (new UserMailManager($app, $parameters))->mailer();
        });
    }
    ...
}
```

You would utilize our setup so far like this:

```php
/** @var Mailer $mailer */
$mailer = app('user.mailer', $smtpParams);
$mailer->send($mailable);
```

This does not look all that handy though. Usually, you have your SMTP params in a column in your `users` table (or another table, doesn't matter). We're going to use that to our advantage, by creating a helper method in the `User` model.

```php
public function sendMail(Mailable $mailable)
{
    if ($this->smtp_enabled) {
        $smtpParams = [
            'transport' => 'smtp',
            'host' => $this->smtp_params['smtp_host'] ?? null,
            'port' => $this->smtp_params['smtp_port'] ?? null,
            'username' => $this->smtp_params['smtp_user'] ?? null,
            'password' => $this->smtp_params['smtp_password'] ?? null,
            'timeout' => null,
            'verify_peer' => false,
        ];

        /** @var Mailer $mailer */
        $mailer = app('creator.mailer', $smtpParams);
        $mailer->send($mailable);
    } else {
        Mail::send($mailable);
    }
}
```

## Using the mailer with custom SMTP params

Now, in the other parts of the code where you want to use the custom params, you can swap `Mail::send(new TestMail($testVariable))` with `$user->sendMail(new TestMail($testVariable))`. Handy, right?

If you want to queue the mail instead, I'd suggest you add a new method called `queueMail($mailable, ...)`, and tailor it to your needs. The only difference would probably be using `$mailer->queue($mailable);` inside the method.

That's it. Leave a thumbs up if this helped you, and thank you for making it this far.