DEV Community

Cover image for Allowing users to send emails with their own SMTP settings in Laravel 9
Geni Jaho
Geni Jaho

Posted on • Originally published at blog.genijaho.dev

Allowing users to send emails with their own SMTP settings in Laravel 9

Various guides exist on how to do this in Laravel, but all the ones I found (this and this) worked only up to Laravel 8. The reason is that in v9 the Swift Mailer was no longer supported in favor of 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:

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

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:

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();
    });
}
Enter fullscreen mode Exit fullscreen mode

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.

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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

You would utilize our setup so far like this:

/** @var Mailer $mailer */
$mailer = app('user.mailer', $smtpParams);
$mailer->send($mailable);
Enter fullscreen mode Exit fullscreen mode

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.

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Latest comments (1)

Collapse
 
lacernest profile image
LacErnest

Great article! Looking for the laravel version 8 of this