forked from KnpLabs/KnpUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Make "mailer" service optional #2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
* Checks to see if the mailer service exists. | ||
* | ||
* @author Ryan Weaver <[email protected]> | ||
* | ||
* @deprecated since 2.1, "mailer" service should be optional | ||
*/ | ||
class CheckForMailerPass implements CompilerPassInterface | ||
{ | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
<?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\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* @author Christophe Coevoet <[email protected]> | ||
*/ | ||
class TwigMailer implements MailerInterface | ||
{ | ||
/** | ||
* @var \Swift_Mailer | ||
*/ | ||
protected $mailer; | ||
|
||
/** | ||
* @var UrlGeneratorInterface | ||
*/ | ||
protected $router; | ||
|
||
/** | ||
* @var \Twig_Environment | ||
*/ | ||
protected $twig; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $parameters; | ||
|
||
/** | ||
* TwigMailer constructor. | ||
* | ||
* @param \Swift_Mailer $mailer | ||
* @param UrlGeneratorInterface $router | ||
* @param \Twig_Environment $twig | ||
* @param array $parameters | ||
*/ | ||
public function __construct($mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->router = $router; | ||
$this->twig = $twig; | ||
$this->parameters = $parameters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendConfirmationEmailMessage(UserInterface $user) | ||
{ | ||
$template = $this->parameters['template']['confirmation']; | ||
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = array( | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
); | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendResettingEmailMessage(UserInterface $user) | ||
{ | ||
$template = $this->parameters['template']['resetting']; | ||
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = array( | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
); | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail()); | ||
} | ||
|
||
/** | ||
* @param string $templateName | ||
* @param array $context | ||
* @param array $fromEmail | ||
* @param string $toEmail | ||
*/ | ||
protected function sendMessage($templateName, $context, $fromEmail, $toEmail) | ||
{ | ||
if (null === $this->mailer) { | ||
throw new \RuntimeException( | ||
'Sending email requires the "mailer" service to be available. '. | ||
'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.' | ||
); | ||
} | ||
|
||
$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 \Swift_Message()) | ||
->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); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -11,34 +11,15 @@ | |
|
||
namespace FOS\UserBundle\Mailer; | ||
|
||
use FOS\UserBundle\Model\UserInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* @author Christophe Coevoet <[email protected]> | ||
* | ||
* @deprecated TwigSwiftMailer class is deprecated since 2.1, use TwigMailer instead. | ||
*/ | ||
class TwigSwiftMailer implements MailerInterface | ||
class TwigSwiftMailer extends TwigMailer | ||
{ | ||
/** | ||
* @var \Swift_Mailer | ||
*/ | ||
protected $mailer; | ||
|
||
/** | ||
* @var UrlGeneratorInterface | ||
*/ | ||
protected $router; | ||
|
||
/** | ||
* @var \Twig_Environment | ||
*/ | ||
protected $twig; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $parameters; | ||
|
||
/** | ||
* TwigSwiftMailer constructor. | ||
* | ||
|
@@ -49,74 +30,10 @@ class TwigSwiftMailer implements MailerInterface | |
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendConfirmationEmailMessage(UserInterface $user) | ||
{ | ||
$template = $this->parameters['template']['confirmation']; | ||
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = array( | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
); | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendResettingEmailMessage(UserInterface $user) | ||
{ | ||
$template = $this->parameters['template']['resetting']; | ||
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$context = array( | ||
'user' => $user, | ||
'confirmationUrl' => $url, | ||
); | ||
|
||
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail()); | ||
} | ||
|
||
/** | ||
* @param string $templateName | ||
* @param array $context | ||
* @param array $fromEmail | ||
* @param string $toEmail | ||
*/ | ||
protected function sendMessage($templateName, $context, $fromEmail, $toEmail) | ||
{ | ||
$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 \Swift_Message()) | ||
->setSubject($subject) | ||
->setFrom($fromEmail) | ||
->setTo($toEmail); | ||
|
||
if (!empty($htmlBody)) { | ||
$message->setBody($htmlBody, 'text/html') | ||
->addPart($textBody, 'text/plain'); | ||
} else { | ||
$message->setBody($textBody); | ||
} | ||
@trigger_error(sprintf( | ||
'%s is deprecated, use %s instead.', __CLASS__, TwigMailer::class | ||
), E_USER_DEPRECATED); | ||
|
||
$this->mailer->send($message); | ||
parent::__construct($mailer, $router, $twig, $parameters); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$mailer Should be typed as \Swift_Mailer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it should not =) With a type hint it would not be possible to pass
null
value. OldTwigSwiftMailer
had that type hint. I could not remove it from there because it would be BC break. So - i created a new classTwigMailer
, a parent for oldTwigSwiftMailer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, what's about
?
Currently you can pass anything as
$mailer
... :DUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turned out that there are more then one mailer! I found this out in @stof's comment last year in #2562 (comment) =) That is a reason why
symfony/swiftmailer-bundle
is not required dependency, btw. So, it is not necessary, i guessThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that there is no another mailer which uses the Swift_Message class (which comes from the swiftmailer package)
https://github.com/covex-nn/FOSUserBundle/blob/b3000d0088536a1384b7057aa0888a9caa42a45e/Mailer/TwigMailer.php#L115
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devtronic actualy, i agree with you, and i am on your side! May be
symfony/swiftmailer-bundle
should be inrequire
section of composer.json! Create a PR and i will be very glad if it will be accepted, then i will close this one =)But,
Swift_Message
comes fromswiftmailer/swiftmailer
and it is not a bundle. So, it is possible to create a bundle, that providesmailer
service and usesSwift_Message
as a first argument ofsend
method at the same time.