|
| 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\Mailer\MailerInterface as SymfonyMailerInterface; |
| 16 | +use Symfony\Component\Mime\Address; |
| 17 | +use Symfony\Component\Mime\Email; |
| 18 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 19 | +use Twig\Environment; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Christophe Coevoet <stof@notk.org> |
| 23 | + */ |
| 24 | +final class TwigSymfonyMailer implements MailerInterface |
| 25 | +{ |
| 26 | + private SymfonyMailerInterface $mailer; |
| 27 | + private UrlGeneratorInterface $router; |
| 28 | + private Environment $twig; |
| 29 | + private array $parameters; |
| 30 | + |
| 31 | + public function __construct(SymfonyMailerInterface $mailer, UrlGeneratorInterface $router, Environment $twig, array $parameters) |
| 32 | + { |
| 33 | + $this->mailer = $mailer; |
| 34 | + $this->router = $router; |
| 35 | + $this->twig = $twig; |
| 36 | + $this->parameters = $parameters; |
| 37 | + } |
| 38 | + |
| 39 | + public function sendConfirmationEmailMessage(UserInterface $user): void |
| 40 | + { |
| 41 | + $template = $this->parameters['template']['confirmation']; |
| 42 | + $url = $this->router->generate('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL); |
| 43 | + |
| 44 | + $context = [ |
| 45 | + 'user' => $user, |
| 46 | + 'confirmationUrl' => $url, |
| 47 | + ]; |
| 48 | + |
| 49 | + $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], $user->getEmail()); |
| 50 | + } |
| 51 | + |
| 52 | + public function sendResettingEmailMessage(UserInterface $user): void |
| 53 | + { |
| 54 | + $template = $this->parameters['template']['resetting']; |
| 55 | + $url = $this->router->generate('fos_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL); |
| 56 | + |
| 57 | + $context = [ |
| 58 | + 'user' => $user, |
| 59 | + 'confirmationUrl' => $url, |
| 60 | + ]; |
| 61 | + |
| 62 | + $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], $user->getEmail()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array<string, mixed> $context |
| 67 | + * @param array{address: string, sender_name: string} $fromEmail |
| 68 | + */ |
| 69 | + private function sendMessage(string $templateName, array $context, array $fromEmail, string $toEmail): void |
| 70 | + { |
| 71 | + $template = $this->twig->load($templateName); |
| 72 | + $subject = $template->renderBlock('subject', $context); |
| 73 | + $textBody = $template->renderBlock('body_text', $context); |
| 74 | + |
| 75 | + $htmlBody = ''; |
| 76 | + |
| 77 | + if ($template->hasBlock('body_html', $context)) { |
| 78 | + $htmlBody = $template->renderBlock('body_html', $context); |
| 79 | + } |
| 80 | + |
| 81 | + $message = (new Email()) |
| 82 | + ->subject($subject) |
| 83 | + ->from(new Address($fromEmail['address'], $fromEmail['sender_name'])) |
| 84 | + ->to($toEmail) |
| 85 | + ->text($textBody) |
| 86 | + ; |
| 87 | + |
| 88 | + if (!empty($htmlBody)) { |
| 89 | + $message->html($htmlBody); |
| 90 | + } |
| 91 | + |
| 92 | + $this->mailer->send($message); |
| 93 | + } |
| 94 | +} |
0 commit comments