TwigSwiftMailer.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Mailer;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use FOS\UserBundle\Mailer\MailerInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. /**
  15. * @author Christophe Coevoet <stof@notk.org>
  16. */
  17. class TwigSwiftMailer implements MailerInterface
  18. {
  19. protected $mailer;
  20. protected $router;
  21. protected $twig;
  22. protected $parameters;
  23. public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
  24. {
  25. $this->mailer = $mailer;
  26. $this->router = $router;
  27. $this->twig = $twig;
  28. $this->parameters = $parameters;
  29. }
  30. public function sendConfirmationEmailMessage(UserInterface $user)
  31. {
  32. $template = $this->parameters['template']['confirmation'];
  33. $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true);
  34. $context = array(
  35. 'user' => $user,
  36. 'confirmationUrl' => $url
  37. );
  38. $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], $user->getEmail());
  39. }
  40. public function sendResettingEmailMessage(UserInterface $user)
  41. {
  42. $template = $this->parameters['template']['resetting'];
  43. $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), true);
  44. $context = array(
  45. 'user' => $user,
  46. 'confirmationUrl' => $url
  47. );
  48. $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], $user->getEmail());
  49. }
  50. /**
  51. * @param string $templateName
  52. * @param array $context
  53. * @param string $fromEmail
  54. * @param string $toEmail
  55. */
  56. protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
  57. {
  58. $template = $this->twig->loadTemplate($templateName);
  59. $subject = $template->renderBlock('subject', $context);
  60. $textBody = $template->renderBlock('body_text', $context);
  61. $htmlBody = $template->renderBlock('body_html', $context);
  62. $message = \Swift_Message::newInstance()
  63. ->setSubject($subject)
  64. ->setFrom($fromEmail)
  65. ->setTo($toEmail);
  66. if (!empty($htmlBody)) {
  67. $message->setBody($htmlBody, 'text/html')
  68. ->addPart($textBody, 'text/plain');
  69. } else {
  70. $message->setBody($textBody);
  71. }
  72. $this->mailer->send($message);
  73. }
  74. }