TemplatedEmail can't be send because of the missing html

109 Views Asked by At

My email looks like:

$email = (new TemplatedEmail())
    ->from(new Address('[email protected]', 'Support'))
    ->to('[email protected]')
    ->subject('Your password reset request')
    ->htmlTemplate('reset_password/email.html.twig')
    ->context([
        'resetToken' => '12312321',
    ]);

And it throws error like:

A message must have a text or an HTML part or attachments

I found that I have to add:

$loader = new FilesystemLoader($this->kernel->getProjectDir() . '/templates');
$twigEnv = new Environment($loader);
$twigBodyRenderer = new BodyRenderer($twigEnv);
$twigBodyRenderer->render($email);

But I am pretty sure that there is no way of this to be the correct approach. Do I really have to write these 4 lines in every email sending action ? Plus when I installed the symfonycasts/reset-password-bundle it doesn't have the FilesystemLoader and so on in it. So it was obviously not needed for the email to work. I am missing something here ?

1

There are 1 best solutions below

0
On

In order to find your templates without further code, your twig config should include something like this

twig:
     default_path: '%kernel.project_dir%/templates'