Implementing Mailjet Mailer in Symfony 6

Aghar Saifeddine
2 min readMar 13, 2024

--

In this tutorial, we will walk through the process of integrating Mailjet mailer to send emails in a Symfony 6 application. Mailjet is a popular email delivery service that provides a reliable and efficient way to send transactional and marketing emails.

Step 1: Install Symfony Mailjet Mailer

To get started, open your terminal and run the following command to install the Symfony Mailjet Mailer package:

composer require symfony/mailjet-mailer

Once the package is installed, Symfony will automatically add the necessary configuration to your .env file, including MAILER_DSN.

###> symfony/mailjet-mailer ###
MAILER_DSN=mailjet+api://PUBLIC_KEY:PRIVATE_KEY@api.mailjet.com
# #MAILER_DSN=mailjet+smtp://PUBLIC_KEY:PRIVATE_KEY@in-v3.mailjet.com
###< symfony/mailjet-mailer ###

Step 2: Set Up Mailjet Account

If you haven’t already, create an account on Mailjet to obtain your Mailjet API keys (Public and Private keys). Update your .env file with these keys as provided by Mailjet.

Step 3: Create Mailjet Service

Next, let’s create a service to handle sending emails using Mailjet. Create a new PHP class MailjetService in your src/Service directory:

<?php

namespace App\Service;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

class MailjetService
{
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}

public function sendMail($mailData)
{
$email = (new Email())
->from('from@example.com')
->to($mailData);

$this->mailer->send($email);
}
}

Step 4: Create Emails Template Sender Service

Now, let’s create another service EmailsTemplateSenderService to handle specific email templates. This service will utilize Twig for rendering email templates:

<?php

namespace App\Service;

use Twig\Environment;

class EmailsTemplateSenderService
{
private $mailer;
private $twig;

public function __construct(Environment $twig, MailjetService $mailer)
{
$this->mailer = $mailer;
$this->twig = $twig;
}

public function sendOTPMail($recipient)
{
$mailData = [
'to' => $recipient,
'subject' => 'OTP MAIL',
'message' => $this->twig->render('emails/otp_mail.html.twig', [
'recipient' => $recipient,
]),
];
$this->mailer->sendMail($mailData);
}
}

Step 5: Test Email Sending

<?php

namespace App\Controller;

use App\Service\EmailsTemplateSenderService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class MailController extends AbstractController
{
#[Route('/otp_email', name: 'otp_email')]
public function sendOTPMail(EmailsTemplateSenderService $emailService)
{
$emailService->sendOTPMail('recipient@example.com');

return $this->json(['message' => 'OTP email sent successfully']);
}
}

Conclusion

You have now successfully integrated Mailjet mailer into your Symfony 6 application. You can extend this setup to handle various types of emails and templates according to your application’s needs.

Feel free to adjust the code snippets and explanations as needed for your blog post. This example provides a basic structure for integrating Mailjet with Symfony 6 for sending emails.

--

--