-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Notify post author about new comments by sending email #421
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
Merged
javiereguiluz
merged 11 commits into
symfony:master
from
voronkovich:swiftmailer-example
Jan 12, 2017
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b6ba9a
Notify post author about new comments
voronkovich b4a97be
Use event listener and custom event trigger
voronkovich f3b258a
Remove default value for sender property
voronkovich 4125af9
Add note about event listener's method
voronkovich f37e552
Minor tweaks in the translation strings
javiereguiluz ec601b6
Minor reword in the help note
javiereguiluz a596761
Improved some help notes
javiereguiluz bbc4077
Improved more help notes
javiereguiluz 1e83a1d
Final round of help notes improvements
javiereguiluz f918956
Use '_fragment' parameter
voronkovich 7dc958c
Change prefix for translation messages
voronkovich 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
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
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 |
---|---|---|
|
@@ -31,9 +31,17 @@ services: | |
tags: | ||
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } | ||
|
||
# Event subscribers are similar to event listeners but they don't need to add | ||
# a separate tag for each listened event. Instead, the PHP class of the event | ||
# subscriber includes a method that returns the list of listened events. | ||
app.comment_notification: | ||
class: AppBundle\EventListener\CommentNotificationListener | ||
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%'] | ||
# The "method" attribute of this tag is optional and defaults to "on + camelCasedEventName" | ||
# If the event is "comment.created" the method executed by default is "onCommentCreated()". | ||
tags: | ||
- { name: kernel.event_listener, event: comment.created, method: onCommentCreated } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have two questions about the
|
||
|
||
# Event subscribers are similar to event listeners but they don't need service tags. | ||
# Instead, the PHP class of the event subscriber includes a method that returns | ||
# the list of events listened by that class. | ||
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber | ||
app.console_subscriber: | ||
class: AppBundle\EventListener\ConsoleEventSubscriber | ||
|
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
96 changes: 96 additions & 0 deletions
96
src/AppBundle/EventListener/CommentNotificationListener.php
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,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AppBundle\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Notifies post's author about new comments. | ||
* | ||
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru> | ||
*/ | ||
class CommentNotificationListener | ||
{ | ||
/** | ||
* @var \Swift_Mailer | ||
*/ | ||
private $mailer; | ||
|
||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
/** | ||
* @var UrlGeneratorInterface | ||
*/ | ||
private $urlGenerator; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $sender; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Swift_Mailer $mailer | ||
* @param UrlGeneratorInterface $urlGenerator | ||
* @param TranslatorInterface $translator | ||
* @param string $sender | ||
*/ | ||
public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, $sender) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->translator = $translator; | ||
$this->sender = $sender; | ||
} | ||
|
||
/** | ||
* @param GenericEvent $event | ||
*/ | ||
public function onCommentCreated(GenericEvent $event) | ||
{ | ||
$comment = $event->getSubject(); | ||
$post = $comment->getPost(); | ||
|
||
$linkToPost = $this->urlGenerator->generate('blog_post', [ | ||
'slug' => $post->getSlug(), | ||
'_fragment' => 'comment_'.$comment->getId(), | ||
], UrlGeneratorInterface::ABSOLUTE_URL); | ||
|
||
$subject = $this->translator->trans('notification.comment_created'); | ||
$body = $this->translator->trans('notification.comment_created.description', [ | ||
'%title%' => $post->getTitle(), | ||
'%link%' => $linkToPost, | ||
]); | ||
|
||
// Symfony uses a library called SwiftMailer to send emails. That's why | ||
// email messages are created instantiating a Swift_Message class. | ||
// See http://symfony.com/doc/current/email.html#sending-emails | ||
$message = \Swift_Message::newInstance() | ||
->setSubject($subject) | ||
->setTo($post->getAuthorEmail()) | ||
->setFrom($this->sender) | ||
->setBody($body, 'text/html') | ||
; | ||
|
||
// In app/config/config_dev.yml the 'disable_delivery' option is set to 'true'. | ||
// That's why in the development environment you won't actually receive any email. | ||
// However, you can inspect the contents of those unsent emails using the debug toolbar. | ||
// See http://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar | ||
$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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AppBundle; | ||
|
||
/** | ||
* This class defines the names of all the events dispatched in | ||
* the Symfony Demo application. It's not mandatory to create a | ||
* class like this, but it's considered a good practice. | ||
* | ||
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru> | ||
*/ | ||
final class Events | ||
{ | ||
/** | ||
* For the event naming conventions, see: | ||
* http://symfony.com/doc/current/components/event_dispatcher.html#naming-conventions. | ||
* | ||
* @Event("Symfony\Component\EventDispatcher\GenericEvent") | ||
* | ||
* @var string | ||
*/ | ||
const COMMENT_CREATED = 'comment.created'; | ||
} |
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.
where is the
app.notifications.email_sender
parameter initialized ?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, it is already part of the codebase although not used for notifications today