PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Mailer/TwigSwiftMailer.php

http://github.com/FriendsOfSymfony/FOSUserBundle
PHP | 117 lines | 59 code | 20 blank | 38 comment | 3 complexity | 85078cea88019a59a0044b5a66b39368 MD5 | raw file
  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 Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. /**
  14. * @author Christophe Coevoet <stof@notk.org>
  15. */
  16. class TwigSwiftMailer implements MailerInterface
  17. {
  18. /**
  19. * @var \Swift_Mailer
  20. */
  21. protected $mailer;
  22. /**
  23. * @var UrlGeneratorInterface
  24. */
  25. protected $router;
  26. /**
  27. * @var \Twig_Environment
  28. */
  29. protected $twig;
  30. /**
  31. * @var array
  32. */
  33. protected $parameters;
  34. /**
  35. * TwigSwiftMailer constructor.
  36. */
  37. public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
  38. {
  39. $this->mailer = $mailer;
  40. $this->router = $router;
  41. $this->twig = $twig;
  42. $this->parameters = $parameters;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function sendConfirmationEmailMessage(UserInterface $user)
  48. {
  49. $template = $this->parameters['template']['confirmation'];
  50. $url = $this->router->generate('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
  51. $context = [
  52. 'user' => $user,
  53. 'confirmationUrl' => $url,
  54. ];
  55. $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail());
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function sendResettingEmailMessage(UserInterface $user)
  61. {
  62. $template = $this->parameters['template']['resetting'];
  63. $url = $this->router->generate('fos_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
  64. $context = [
  65. 'user' => $user,
  66. 'confirmationUrl' => $url,
  67. ];
  68. $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
  69. }
  70. /**
  71. * @param string $templateName
  72. * @param array $context
  73. * @param array $fromEmail
  74. * @param string $toEmail
  75. */
  76. protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
  77. {
  78. $template = $this->twig->load($templateName);
  79. $subject = $template->renderBlock('subject', $context);
  80. $textBody = $template->renderBlock('body_text', $context);
  81. $htmlBody = '';
  82. if ($template->hasBlock('body_html', $context)) {
  83. $htmlBody = $template->renderBlock('body_html', $context);
  84. }
  85. $message = (new \Swift_Message())
  86. ->setSubject($subject)
  87. ->setFrom($fromEmail)
  88. ->setTo($toEmail);
  89. if (!empty($htmlBody)) {
  90. $message->setBody($htmlBody, 'text/html')
  91. ->addPart($textBody, 'text/plain');
  92. } else {
  93. $message->setBody($textBody);
  94. }
  95. $this->mailer->send($message);
  96. }
  97. }