From 15df3e7edc1cfe5b874194c7ce2d065c9f9cbab4 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 8 Oct 2015 22:19:15 +0200 Subject: [PATCH] Optionally send email notifications to set emails --- README.md | 5 +- comments.php | 36 +++++- comments.yaml | 4 +- languages.yaml | 4 + templates/email/base.html.twig | 194 +++++++++++++++++++++++++++++++++ 5 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 templates/email/base.html.twig diff --git a/README.md b/README.md index 86c3385..bcf7587 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,14 @@ When the plugin is installed and enabled, the `Comments` menu will appear in the Further improvements to the comments visualization will be added in the next releases. +# Email notifications + +Upon receiving a comment, if `enable_email_notifications` is enabled, the Comments plugin will send an email to the `notifications_email_to` address set in the plugin options. + # Things still missing - Add language file - Allow to delete comments from the Admin Plugin -- Email the comment to the site admins (default to all with admin.super, could be configured) - Allow some pages to disable adding comments - Ability to see all comments of a page in the Admin Plugin - Ability to reply to a comment from the Admin Plugin diff --git a/comments.php b/comments.php index 1bb9632..3d1eee3 100644 --- a/comments.php +++ b/comments.php @@ -6,9 +6,10 @@ use Grav\Common\Grav; use Grav\Common\Page\Page; use Grav\Common\Page\Pages; use Grav\Common\Plugin; +use Grav\Common\Filesystem\RecursiveFolderFilterIterator; +use Grav\Common\User\User; use RocketTheme\Toolbox\File\File; use RocketTheme\Toolbox\Event\Event; -use Grav\Common\Filesystem\RecursiveFolderFilterIterator; use Symfony\Component\Yaml\Yaml; class CommentsPlugin extends Plugin @@ -127,9 +128,42 @@ class CommentsPlugin extends Plugin $file->save(Yaml::dump($data)); + if (isset($this->grav['Email']) && $this->grav['config']->get('plugins.comments.enable_email_notifications')) { + $this->sendEmailNotification(array( + 'title' => $title, + 'comment' => array( + 'text' => $text, + 'date' => gmdate('D, d M Y H:i:s', time()), + 'author' => $name, + 'email' => $email + ) + )); + } + exit(); } + private function sendEmailNotification($comment) { + /** @var Language $l */ + $l = $this->grav['language']; + + $sitename = $this->grav['config']->get('site.title', 'Website'); + $from = $this->grav['config']->get('plugins.email.from', 'noreply@getgrav.org'); + $to = $this->grav['config']->get('plugins.email.email'); + + $subject = $l->translate(['PLUGIN_COMMENTS.NEW_COMMENT_EMAIL_SUBJECT', $sitename]); + $content = $l->translate(['PLUGIN_COMMENTS.NEW_COMMENT_EMAIL_BODY', $sitename, $comment['title'], $comment['comment']['text'], $comment['comment']['author'], $comment['comment']['email']]); + + $twig = $this->grav['twig']; + $body = $twig->processTemplate('email/base.html.twig', ['content' => $content]); + + $message = $this->grav['Email']->message($subject, $body, 'text/html') + ->setFrom($from) + ->setTo($to); + + $sent = $this->grav['Email']->send($message); + } + private function getFilesOrderedByModifiedDate($path = '') { $files = []; diff --git a/comments.yaml b/comments.yaml index 5167834..c0882a8 100644 --- a/comments.yaml +++ b/comments.yaml @@ -1,4 +1,6 @@ enabled: true use_captcha: false recatpcha_site_key: '' -recatpcha_secret: '' \ No newline at end of file +recatpcha_secret: '' +enable_email_notifications: true +notifications_email_to: 'copesc@gmail.com' \ No newline at end of file diff --git a/languages.yaml b/languages.yaml index 7235528..8c82d23 100644 --- a/languages.yaml +++ b/languages.yaml @@ -1,3 +1,7 @@ en: PLUGIN_COMMENTS: COMMENTS: Comments + EMAIL_NOT_CONFIGURED: Email not configured + NEW_COMMENT_EMAIL_SUBJECT: 'New comment on %1$s' + NEW_COMMENT_EMAIL_BODY: '

A new comment was made on %1$s by %3$s (%4$s).

Post: %2$s

Text: %5$s

' + EMAIL_FOOTER: '' diff --git a/templates/email/base.html.twig b/templates/email/base.html.twig new file mode 100644 index 0000000..5405424 --- /dev/null +++ b/templates/email/base.html.twig @@ -0,0 +1,194 @@ + + + + + + Comments Email Template + + + + + + + + + + + + +
+
+ + + + +
+ {{ content }} +
+
+
+ + + + + + + + + + + + + +