1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace FOS\UserBundle\Mailer;
- use FOS\UserBundle\Model\UserInterface;
- use FOS\UserBundle\Mailer\MailerInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- /**
- * @author Christophe Coevoet <stof@notk.org>
- */
- class TwigSwiftMailer implements MailerInterface
- {
- protected $mailer;
- protected $router;
- protected $twig;
- protected $parameters;
- public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
- {
- $this->mailer = $mailer;
- $this->router = $router;
- $this->twig = $twig;
- $this->parameters = $parameters;
- }
- public function sendConfirmationEmailMessage(UserInterface $user)
- {
- $template = $this->parameters['template']['confirmation'];
- $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true);
- $context = array(
- 'user' => $user,
- 'confirmationUrl' => $url
- );
- $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], $user->getEmail());
- }
- public function sendResettingEmailMessage(UserInterface $user)
- {
- $template = $this->parameters['template']['resetting'];
- $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), true);
- $context = array(
- 'user' => $user,
- 'confirmationUrl' => $url
- );
- $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], $user->getEmail());
- }
- /**
- * @param string $templateName
- * @param array $context
- * @param string $fromEmail
- * @param string $toEmail
- */
- protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
- {
- $template = $this->twig->loadTemplate($templateName);
- $subject = $template->renderBlock('subject', $context);
- $textBody = $template->renderBlock('body_text', $context);
- $htmlBody = $template->renderBlock('body_html', $context);
- $message = \Swift_Message::newInstance()
- ->setSubject($subject)
- ->setFrom($fromEmail)
- ->setTo($toEmail);
- if (!empty($htmlBody)) {
- $message->setBody($htmlBody, 'text/html')
- ->addPart($textBody, 'text/plain');
- } else {
- $message->setBody($textBody);
- }
- $this->mailer->send($message);
- }
- }
|