vendor/friendsofsymfony/user-bundle/Mailer/TwigSwiftMailer.php line 113

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