'Symfony 5 mail not send - MailHog

When I try to send a local mail I do not get anything and yet I have no mistake. Normally I should receive the mail on Mailhog. I tested by sending an email with phpmailer and it works perfectly.

Can you help me please

IndexController file

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;

class IndexController extends AbstractController
{
    /**
    * @Route("/", name="index")
    */
    public function index(MailerInterface $mailer): Response
    {
        $email = (new Email())
        ->from('[email protected]') 
        ->to('[email protected]') 
        ->subject('I love Me')
        ->html('<h1>Lorem ipsum</h1> <p>...</p>');
        
        try {
            $mailer->send($email);
        } catch (TransportExceptionInterface $e) {
            dump($e);
        }
    }
}

.env file

###> symfony/webapp-meta ###
MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
###< symfony/webapp-meta ###

###> symfony/mailer ###
MAILER_DSN=smtp://localhost:1025
###< symfony/mailer ###

mailer.yaml file

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'
  

messenger.yaml file

framework:
    messenger:
        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                retry_strategy:
                    max_retries: 3
                    multiplier: 2
            failed: 'doctrine://default?queue_name=failed'
            # sync: 'sync://'

        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: async
            Symfony\Component\Notifier\Message\ChatMessage: async
            Symfony\Component\Notifier\Message\SmsMessage: async

            # Route your messages to the transports
            # 'App\Message\YourMessage': async


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source