|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +/* | 
|  | 4 | + * This file is part of the FOSUserBundle package. | 
|  | 5 | + * | 
|  | 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | 
|  | 7 | + * | 
|  | 8 | + * For the full copyright and license information, please view the LICENSE | 
|  | 9 | + * file that was distributed with this source code. | 
|  | 10 | + */ | 
|  | 11 | + | 
|  | 12 | +namespace FOS\UserBundle\Mailer; | 
|  | 13 | + | 
|  | 14 | +use FOS\UserBundle\Model\UserInterface; | 
|  | 15 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * @author Christophe Coevoet <[email protected]> | 
|  | 19 | + */ | 
|  | 20 | +class TwigMailer implements MailerInterface | 
|  | 21 | +{ | 
|  | 22 | +    /** | 
|  | 23 | +     * @var \Swift_Mailer | 
|  | 24 | +     */ | 
|  | 25 | +    protected $mailer; | 
|  | 26 | + | 
|  | 27 | +    /** | 
|  | 28 | +     * @var UrlGeneratorInterface | 
|  | 29 | +     */ | 
|  | 30 | +    protected $router; | 
|  | 31 | + | 
|  | 32 | +    /** | 
|  | 33 | +     * @var \Twig_Environment | 
|  | 34 | +     */ | 
|  | 35 | +    protected $twig; | 
|  | 36 | + | 
|  | 37 | +    /** | 
|  | 38 | +     * @var array | 
|  | 39 | +     */ | 
|  | 40 | +    protected $parameters; | 
|  | 41 | + | 
|  | 42 | +    /** | 
|  | 43 | +     * TwigMailer constructor. | 
|  | 44 | +     * | 
|  | 45 | +     * @param \Swift_Mailer         $mailer | 
|  | 46 | +     * @param UrlGeneratorInterface $router | 
|  | 47 | +     * @param \Twig_Environment     $twig | 
|  | 48 | +     * @param array                 $parameters | 
|  | 49 | +     */ | 
|  | 50 | +    public function __construct($mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters) | 
|  | 51 | +    { | 
|  | 52 | +        $this->mailer = $mailer; | 
|  | 53 | +        $this->router = $router; | 
|  | 54 | +        $this->twig = $twig; | 
|  | 55 | +        $this->parameters = $parameters; | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    /** | 
|  | 59 | +     * {@inheritdoc} | 
|  | 60 | +     */ | 
|  | 61 | +    public function sendConfirmationEmailMessage(UserInterface $user) | 
|  | 62 | +    { | 
|  | 63 | +        $template = $this->parameters['template']['confirmation']; | 
|  | 64 | +        $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | 
|  | 65 | + | 
|  | 66 | +        $context = array( | 
|  | 67 | +            'user' => $user, | 
|  | 68 | +            'confirmationUrl' => $url, | 
|  | 69 | +        ); | 
|  | 70 | + | 
|  | 71 | +        $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail()); | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    /** | 
|  | 75 | +     * {@inheritdoc} | 
|  | 76 | +     */ | 
|  | 77 | +    public function sendResettingEmailMessage(UserInterface $user) | 
|  | 78 | +    { | 
|  | 79 | +        $template = $this->parameters['template']['resetting']; | 
|  | 80 | +        $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | 
|  | 81 | + | 
|  | 82 | +        $context = array( | 
|  | 83 | +            'user' => $user, | 
|  | 84 | +            'confirmationUrl' => $url, | 
|  | 85 | +        ); | 
|  | 86 | + | 
|  | 87 | +        $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail()); | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    /** | 
|  | 91 | +     * @param string $templateName | 
|  | 92 | +     * @param array  $context | 
|  | 93 | +     * @param array  $fromEmail | 
|  | 94 | +     * @param string $toEmail | 
|  | 95 | +     */ | 
|  | 96 | +    protected function sendMessage($templateName, $context, $fromEmail, $toEmail) | 
|  | 97 | +    { | 
|  | 98 | +        if (null === $this->mailer) { | 
|  | 99 | +            throw new \RuntimeException( | 
|  | 100 | +                'Sending email requires the "mailer" service to be available. '. | 
|  | 101 | +                'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.' | 
|  | 102 | +            ); | 
|  | 103 | +        } | 
|  | 104 | + | 
|  | 105 | +        $template = $this->twig->load($templateName); | 
|  | 106 | +        $subject = $template->renderBlock('subject', $context); | 
|  | 107 | +        $textBody = $template->renderBlock('body_text', $context); | 
|  | 108 | + | 
|  | 109 | +        $htmlBody = ''; | 
|  | 110 | + | 
|  | 111 | +        if ($template->hasBlock('body_html', $context)) { | 
|  | 112 | +            $htmlBody = $template->renderBlock('body_html', $context); | 
|  | 113 | +        } | 
|  | 114 | + | 
|  | 115 | +        $message = (new \Swift_Message()) | 
|  | 116 | +            ->setSubject($subject) | 
|  | 117 | +            ->setFrom($fromEmail) | 
|  | 118 | +            ->setTo($toEmail); | 
|  | 119 | + | 
|  | 120 | +        if (!empty($htmlBody)) { | 
|  | 121 | +            $message->setBody($htmlBody, 'text/html') | 
|  | 122 | +                ->addPart($textBody, 'text/plain'); | 
|  | 123 | +        } else { | 
|  | 124 | +            $message->setBody($textBody); | 
|  | 125 | +        } | 
|  | 126 | + | 
|  | 127 | +        $this->mailer->send($message); | 
|  | 128 | +    } | 
|  | 129 | +} | 
0 commit comments