I've just pulled an all-nighter trying to get this to work, and I'm probably missing something foolish, but help me out.
I have a symfony2 service which sends emails. I have injected the @templating
service into my service's constructor like so:
services.yml
order_service:
class: AppBundle\Services\OrderService
arguments: [ "@doctrine.orm.entity_manager", "@mailer", "@templating" ]
OrderService.php
/**
* @var EntityManager
*/
protected $em;
protected $twig;
protected $mailer;
public function __construct(EntityManager $_entityManager, $_mailer, $_twig)
{
$this->em = $_entityManager;
$this->mailer = $_mailer;
$this->twig = $_twig;
}
I have my templates located in AppBundle/Resources/views/Emails
. Further down in my service is the function that tries to send emails.
protected function send_emails($vendors, $order)
{
$customer = $order->getCustInfo();
// Send email(s) to customer
foreach ($order->getVouchers() as $voucher)
{
$message = \Swift_Message::newInstance()
->setSubject('Voucher # ' . $voucher->getId())
->setFrom('[email protected]')
->setTo($customer->email)
->setBody($this->twig->render(
'Emails/email-voucher.html.twig', [
'order' => $order,
'voucher' => $voucher,
'customer' => $customer
]
), 'text/html');
$this->mailer->send($message);
}
}
I always get the error Unable to find template \"Emails/email-voucher.html.twig\".
So, here's a summary of what I've tried:
- Basically everything suggested on similar StackOverflow questions
- Moving templates to
app/Resources/views
- Every path/relative path I can think of in the call to
->render()
- Pulling my hair out
Any ideas?