2

I'm building a website with symfony 4, today I try to send email with the emailBundle of symfony i'm using also the mailgun bundle as transport. Everything is well configured.

When I send email the classic way with ->text() or ->html() everything work but when I use twig templating for email I get this error :

Unable to send an email: Need at least one of 'text' or 'html' parameters specified (code 400).

This is the code part for mail sending :

<?php
            $email = (new TemplatedEmail())
                ->from(new NamedAddress('blabla','blabla'))
                ->to($user->getEmail())
                ->subject('Account registration')
                ->htmlTemplate('emails/signup.html.twig')
                ->context([
                    'firstName' => $user->getFirstName(),
                    'email' => $user->getEmail(),
                    'url' => $_ENV['blabla'].'/account'
                ]);
            $transport = new MailgunTransport($_ENV['blabla'],$_ENV['blabla']);
            $mailer = new Mailer($transport);
            $mailer->send($email);
?>

Thanks in advance

Floran
  • 304
  • 2
  • 14
  • Have you tried to print the content of `emails/signup.html.twig` with provided context ? This error is raised when the `htmlTemplate()` call returns an empty content. – smwhr Nov 19 '19 at 09:43
  • Yes I create a test route to render "emails/signup.html.twig" with the context and everything seems ok. But when i do a var_dump($email->getHtmlBody()) I get null response also when i do a var_dump($email->getHtmlTemplate()) i get "emails/signup.html.twig". – Floran Nov 19 '19 at 09:53

2 Answers2

2

Thanks @smwhr for your help but i find the solution,

So if someone is interested the solution was :

remove these 2 lines :

$transport = new YourTransportMethod($_ENV['blabla'],$_ENV['blabla']);
$mailer = new Mailer($transport);

and use instead the mailer interface in the method declaration like that :

blabla(LoginAuthenticator $authenticator, MailerInterface $mailer): Response

The working code :

$email = (new TemplatedEmail())
            ->from(new NamedAddress('blabla','blabla'))
            ->to(new NamedAddress($user->getEmail(),$user->getFirstName()))
            ->subject('Account registration')
            ->htmlTemplate('emails/signup.html.twig')
            ->context([
                'firstName' => $user->getFirstName(),
                'email' => $user->getEmail(),
                'url' => $_ENV['blabla'].'/account'
            ]);

        $mailer->send($email);

I hope this answer help you

Floran
  • 304
  • 2
  • 14
1

If the message is not rendered using TemplatedEmail, you need to manually render it using the associated Symfony\Bridge\Twig\Mime\BodyRenderer.

Your code would then looks something like :

use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Twig\Environment;

//$twig, an instance of Twig\Environment need to be injected

$email = (new TemplatedEmail());

//[...]

$mailer = new Mailer($transport);
$renderer = new BodyRenderer($twig);
$renderer->render($email);
$mailer->send($email);

See the test case for the BodyRenderer : https://github.com/symfony/symfony/blob/e60a876201b5b306d0c81a24d9a3db997192079c/src/Symfony/Bridge/Twig/Tests/Mime/BodyRendererTest.php

smwhr
  • 675
  • 6
  • 22