forked from KnpLabs/KnpUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3068 from stof/symfony_mailer
Add a mailer implementation based on symfony/mailer
- Loading branch information
Showing
5 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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 Symfony\Component\Mailer\MailerInterface as SymfonyMailerInterface; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Twig\Environment; | ||
|
||
/** | ||
* @author Christophe Coevoet <[email protected]> | ||
*/ | ||
final class TwigSymfonyMailer implements MailerInterface | ||
{ | ||
private SymfonyMailerInterface $mailer; | ||
private UrlGeneratorInterface $router; | ||
private Environment $twig; | ||
private array $parameters; | ||
|
||
public function __construct(SymfonyMailerInterface $mailer, UrlGeneratorInterface $router, Environment $twig, array $parameters) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->router = $router; | ||
$this->twig = $twig; | ||
$this->parameters = $parameters; | ||
} | ||
|
||
public function sendConfirmationEmailMessage(UserInterface $user): void | ||
{ | ||
$template = $this->parameters['template']['confirmation']; | ||
$url = $this->router->generate('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = [ | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
]; | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], $user->getEmail()); | ||
} | ||
|
||
public function sendResettingEmailMessage(UserInterface $user): void | ||
{ | ||
$template = $this->parameters['template']['resetting']; | ||
$url = $this->router->generate('fos_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = [ | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
]; | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], $user->getEmail()); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
* @param array{address: string, sender_name: string} $fromEmail | ||
*/ | ||
private function sendMessage(string $templateName, array $context, array $fromEmail, string $toEmail): void | ||
{ | ||
$template = $this->twig->load($templateName); | ||
$subject = $template->renderBlock('subject', $context); | ||
$textBody = $template->renderBlock('body_text', $context); | ||
|
||
$htmlBody = ''; | ||
|
||
if ($template->hasBlock('body_html', $context)) { | ||
$htmlBody = $template->renderBlock('body_html', $context); | ||
} | ||
|
||
$message = (new Email()) | ||
->subject($subject) | ||
->from(new Address($fromEmail['address'], $fromEmail['sender_name'])) | ||
->to($toEmail) | ||
->text($textBody) | ||
; | ||
|
||
if (!empty($htmlBody)) { | ||
$message->html($htmlBody); | ||
} | ||
|
||
$this->mailer->send($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,14 @@ | |
<parameter key="fos_user.resetting.email.from_email" type="collection"> | ||
<parameter key="[email protected]">Acme Ltd</parameter> | ||
</parameter> | ||
<parameter key="fos_user.registration.confirmation.from_address" type="collection"> | ||
<parameter key="address">[email protected]</parameter> | ||
<parameter key="sender_name">Acme Ltd</parameter> | ||
</parameter> | ||
<parameter key="fos_user.resetting.email.from_address" type="collection"> | ||
<parameter key="address">[email protected]</parameter> | ||
<parameter key="sender_name">Acme Ltd</parameter> | ||
</parameter> | ||
</parameters> | ||
|
||
<services> | ||
|
@@ -33,6 +41,22 @@ | |
<tag name="fos_user.requires_swift" /> | ||
</service> | ||
|
||
<service id="fos_user.mailer.twig_symfony" class="FOS\UserBundle\Mailer\TwigSymfonyMailer" public="false"> | ||
<argument type="service" id="mailer" /> | ||
<argument type="service" id="router" /> | ||
<argument type="service" id="twig" /> | ||
<argument type="collection"> | ||
<argument key="template" type="collection"> | ||
<argument key="confirmation">%fos_user.registration.confirmation.template%</argument> | ||
<argument key="resetting">%fos_user.resetting.email.template%</argument> | ||
</argument> | ||
<argument key="from_email" type="collection"> | ||
<argument key="confirmation">%fos_user.registration.confirmation.from_address%</argument> | ||
<argument key="resetting">%fos_user.resetting.email.from_address%</argument> | ||
</argument> | ||
</argument> | ||
</service> | ||
|
||
<service id="fos_user.mailer.noop" class="FOS\UserBundle\Mailer\NoopMailer" public="false" /> | ||
</services> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters