MailerBundle add a new TemplatedEmail class for the Symfony Mailer Component.
composer require jacquesndl/mailer-bundleThe bundle provides an official recipe to help you configure the bundle.
# config/packages/jacquesndl_mailer.yaml
jacquesndl_mailer:
    sender:
        name: '%env(JACQUESNDL_MAILER_SENDER_NAME)%'
        address: '%env(JACQUESNDL_MAILER_SENDER_ADDRESS)%'# .env
JACQUESNDL_MAILER_SENDER_NAME="Example"
JACQUESNDL_MAILER_SENDER_ADDRESS="[email protected]"
The env variables JACQUESNDL_MAILER_SENDER_NAME and JACQUESNDL_MAILER_SENDER_ADDRESS define the default value for the sender. You can overwrite it using the to() method of the TemplatedEmail class. You can see an example below.
// src/Controller/WelcomeController.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Jacquesndl\MailerBundle\Message\TemplatedEmail;
class WelcomeController extends AbstractController
{
    public function index(MailerInterface $mailer): Response
    {
        // ...
        $email = (new TemplatedEmail())
            ->from('[email protected]') // overwrite the default sender value
            ->to('[email protected]')
            ->replyTo('[email protected]')
            ->template('emails/welcome.email.twig')
            ->attachFromPath('/path/to/documents/coupe-du-monde-1998.pdf')
            ->context([
                'firstName' => 'Zinédine',
            ])
        ;
        
        $mailer->send($email);
        
        // ...
    }
}{# templates/emails/welcome.email.twig #}
{% block subject %}
   Welcome
{% endblock %}
{% block html %}
   <html>
   <head></head>
   <body>
   <h1>Welcome {{ firstName }}</h1>
   </body>
   </html>
{% endblock %}
{% block text %}
   Your text content
{% endblock %}If the block text is missing or empty, mailer will generate it automatically by converting the HTML contents into text. If you have league/html-to-markdown installed in your application, it uses that to turn HTML into Markdown (so the text email has some visual appeal). Otherwise, it applies the strip_tags PHP function to the original HTML contents.
The bundle provides a maker command to create an Email class that extends TemplatedEmail
php bin/console make:email WelcomeEmail// src/Email/WelcomeEmail.php
namespace App\Email;
use Jacquesndl\MailerBundle\Message\TemplatedEmail;
class WelcomeEmail extends TemplatedEmail
{
   protected $template = 'emails/welcome.email.twig';
}// src/Controller/WelcomeController.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use App\Email\WelcomeEmail;
class WelcomeController extends AbstractController
{
    public function index(MailerInterface $mailer): Response
    {
        // ...
        $email = (new WelcomeEmail())
            ->to('[email protected]');
        
        $mailer->send($email);
        
        // ...
    }
}