DEV Community

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

Posted on

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)

Top comments (0)