01446 620436

SilverStripe Page Comment Email Notification

Receive an email when a new page comment has been posted. Works with SilverStripe 2.3+ including version 2.4.

Code

Either download the source code from the PageCommentEmailNotification.zip file or copy and paste the code from below.

Configuration

Add the following two lines to your /mysite/_config.php file to enable email notifications. Remember to change the email to your address!

Email::setAdminEmail("your@emailaddre.ss");
DataObject::add_extension('PageComment', 'PageCommentEmailNotification');

Source code

Download source PageCommentEmailNotification.zipor copy and paste the code from below into files you create:

File: mysite/code/PageCommentEmailNotification.php

<?php

class PageCommentEmailNotification extends DataObjectDecorator {
  function onAfterWrite() {
      parent::onAfterWrite();
      if ($this->NeedsModeration) {
            $email = new Email();
            $email->setTemplate('NewComment');
            $email->setTo(Email::getAdminEmail());
            $email->addCustomHeader('Reply-To', Member::currentUser()->Email);
            $email->setSubject('New Comment ' . str_replace(array("http://", "https://"), array("", ""), Director::absoluteBaseURL()));
            $email->populateTemplate(array(
                'URL' => Director::absoluteBaseURL() . $this->owner->Parent()->URLSegment,
                'PageTitle' => $this->owner->Parent()->Title,
                'Comment' => $this->owner->Comment,
            ));
            $email->send();
        }
  }
}

File: themes/yourtheme/templates/email/NewComment.ss

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <style>
            p{
                font-size: 1.2em;
                color: #444;
            }
            p.comments{
                font-size: 1.4em;
                color: #222;
                padding: 20px;
            }
        </style>
    </head>
    <body>

        <p><% if CurrentMember.Email %><a href="mailto:$CurrentMember.Email">$CurrentMember.Name</a><% else %>$CurrentMember.Name<% end_if %>,  posted a new comment on the page <a href="{$URL}">$PageTitle</a>.</p>

        <p>Their comment:<br/>
        $Comment</p>
   
        <p>You can <a href="{$BaseHref}admin/comments/">administrate the comment</a>. You can reply to the sender direct by email.</p>
   
    </body>
</html>