DEV Community

Cover image for How send transaction emails in Laravel using Brevo (ex Sendinblue)
Kirill
Kirill

Posted on

14 2 2 2 2

How send transaction emails in Laravel using Brevo (ex Sendinblue)

First, lets install Mailer driver:

"symfony/brevo-mailer": "^7.0"
Enter fullscreen mode Exit fullscreen mode

Now lets configure our Mailer. First put this code inside boot() method in your AppServiceProvider.php:

public function boot(): void
{
    //...

    $this->app['mail.manager']->extend('brevo', function ($config) {
        $configuration = $this->app->make('config');

        return (new BrevoTransportFactory())->create(
            Dsn::fromString($configuration->get('services.brevo.dsn'))
        );
    });
}
Enter fullscreen mode Exit fullscreen mode

Here you can see services.brevo.dsn config. I created file config/services and there the following code:

return [
    'brevo' => [
        'dsn' => env('MAILER_DSN'),
    ],
];
Enter fullscreen mode Exit fullscreen mode

Now lets go to config/mail.php and setup our mailer:

'mailers' => [
    // ...
    'brevo' => [
        'transport' => 'brevo',
    ],
]
Enter fullscreen mode Exit fullscreen mode

Then we should create our Email:

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public readonly string $username
    ) {
    }

    public function build(): self
    {
        $this->withSymfonyMessage(function (Email $message) {
            $message->getHeaders()
                ->addHeader('templateId', 1)
                ->addParameterizedHeader('params', 'params', [
                    'customerName' => $this->username,
                ]);
        });

        return $this;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we add headers with templateId and custom data. Brevo uses custom data to fill your variables inside your templates.

Feel free to ask any questions in comments 👇

Follow me on X (prev Twitter)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay