0

My email looks like:

$email = (new TemplatedEmail())
    ->from(new Address('example@example.com', 'Support'))
    ->to('example@example.com')
    ->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 ?

nakhodkin
  • 1,327
  • 1
  • 17
  • 27
Toma Tomov
  • 1,476
  • 19
  • 55

1 Answers1

0

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

twig:
     default_path: '%kernel.project_dir%/templates'
IanMcL
  • 366
  • 2
  • 10