To send emails with Twig HTML in Symfony using the mailer component, follow these steps:

 

Install the required packages:

Make sure you have the symfony/mailer, symfony/twig-bundle, and symfony/asset packages installed. If not, you can install them using Composer:

composer require symfony/mailer symfony/twig-bundle symfony/asset

 

Configure your mailer in the config/packages/mailer.yaml file:

# config/packages/mailer.yaml

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

Create a Twig template for the email content:

Create a new Twig template file (e.g., email_template.html.twig) in the templates directory of your Symfony project. This template will contain the HTML content of the email.

{# templates/email_template.html.twig #}

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{{ subject }}</title>
</head>
<body>
    <h1>{{ subject }}</h1>
    <p>{{ content }}</p>
</body>
</html>

 

Use the mailer service to send the email with Twig HTML:
Now, you can use the mailer service to send the email with the Twig template as the HTML content.

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

class MailController extends AbstractController
{
    /**
     * @Route("/send_email", name="send_email")
     */
    public function sendEmail(MailerInterface $mailer)
    {
        // Get the absolute path to the Twig template
        $templatePath = $this->get('kernel')->getProjectDir() . '/templates/email_template.html.twig';

        // Render the Twig template
        $templateContent = $this->renderView('email_template.html.twig', [
            'subject' => 'Test Email with Twig HTML',
            'content' => 'This is a test email sent using Twig HTML in Symfony mailer.',
        ]);

        // Create the email
        $email = (new Email())
            ->from(This email address is being protected from spambots. You need JavaScript enabled to view it.')
            ->to(This email address is being protected from spambots. You need JavaScript enabled to view it.')
            ->subject('Test Email with Twig HTML')
            ->html($templateContent);

        // send the email
        $mailer->send($email);

        return $this->redirectToRoute('homepage');
    }
}

In this example, we use the renderView() method to render the Twig template and obtain its HTML content. Then, we create an Email object and set its properties, including the HTML content obtained from the Twig template.

Now, when you call the /send_email route (you can adapt this to your application), it will send an email using the Twig HTML template. Make sure to adjust the email addresses, subject, and content as per your requirements.

 

If you want to send attachments along with the email:

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

// Inject MailerInterface in your controller or service
public function sendEmailWithAttachment(MailerInterface $mailer)
{
    // create the email
    $email = (new Email())
        ->from(This email address is being protected from spambots. You need JavaScript enabled to view it.')
        ->to(This email address is being protected from spambots. You need JavaScript enabled to view it.')
        ->subject('Email with attachment')
        ->text('Please see attached file.')
        ->attachFromPath('/path/to/attachment.pdf');

    // send the email
    $mailer->send($email);
}

make sure the path to the file in the attachment is valid and the file can be read.