Symfony 5 | Send mail with SwiftMailer and Maildev

Prerequisite:

Install SwiftMailerhttps://symfony.com/doc/5.2/email.html
Install Maildev :

Configure SwiftMailer :

In the file swiftmailer.yaml which is in the config/packages folder we have access to the SMTP url which is in the .env.

swiftmailer:
    url: '%env(MAILER_URL)%'
    spool: { type: 'memory' }

In the .env change the MAILER_URL.

MAILER_URL=smtp://localhost:1025

Which will send all mails on port 1025 which is the maildev reception port.

Start Maildev :

Go to a command prompt and type maildev, the following message will appear to tell you where to send the mail and where the interface is located.

MailDev webapp running at http://0.0.0.0:1080
MailDev SMTP Server running at 0.0.0.0:1025

Send emails via a Symfony controller:

We will send a mail without HTML format and a mail with HTML format.

So we define a Swift_Message with the different options needed, and we send it with $swiftmailer which is the Swift_Mailer object passed in dependency injection.

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class AppController extends AbstractController
{
    /**
     * @Route("/app", name="app")
     * We call Swift_Mailer in dependency injection
     */
    public function app(\Swift_Mailer $swiftmailer): Response
    {
        //First mail without HTML format, only text
        $message = (new \Swift_Message('Tutorial'))
            ->setFrom('from@email.fr')
            ->setTo('to@email.fr')
            ->setBody('Sending mail !', 'text/html');
        $swiftmailer->send($message);
        //Here is a mail with an HTML template and dynamic variables
        $message = (new \Swift_Message('Tutorial with template'))
            ->setFrom('from@email.fr')
            ->setTo('to@email.fr')
            ->setBody(
                $this->renderView(
                    'emails/tutorial.html.twig',
                    ['subject' => 'Tutorial with template', 'text' => 'Your text !', "datas" => [1, 
                     2, 3, 4, 5]]
                ),
                'text/html'
            );
        $swiftmailer->send($message);

        return $this->json('mail');
    }
}

First mail without HTML format:

$message = (new \Swift_Message('Tutorial'))
            ->setFrom('from@email.fr')
            ->setTo('to@email.fr')
            ->setBody('Sending mail !', 'text/html');
        $swiftmailer->send($message);

Second mail with a Twig template for an HTML format :

$message = (new \Swift_Message('Tutorial with template'))
            ->setFrom('from@email.fr')
            ->setTo('to@email.fr')
            ->setBody(
                $this->renderView(
                    'emails/tutorial.html.twig',
                    ['subject' => 'Tutorial with template', 'text' => 'Your text !', "datas" => [1, 
                     2, 3, 4, 5]]
                ),
                'text/html'
            );

The Twig template with style to show the possibilities:

<h1 style="color: red;">
  {{ subject }}
</h1>
<p>
  {{ text }}
</p>
<ul>
  {% for li in datas %}
    <li>{{ li }}</li>
  {% endfor %}
</ul>

So we have a dynamic template with the variables passed as parameters to the renderView function of the controller.

Receiving mails with Maildev :

Go to the maildev interface on localhost:1080 and run the script to send mails.

Text mail : 

Swiftmailer text

HTML mail :

Swiftmailer html

Here is the result, you can separate the sending in a service to be able to use it everywhere in your code easily.

This tutorial is finished, thanks for reading.