Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Combine Akismet spam checking with comment email notifications

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 683 Views
  • sophiedennis
    Avatar
    Community Member
    2 Posts

    Combine Akismet spam checking with comment email notifications Link to this post

    I thought I'd post this in case it helps anyone else, as it took us a bit of head-scratching to get right.

    We're using Marijn Kampf's code* for emailing the site admin whenever a new comment is posted. However, the site was - inevitably - getting a lot of spam comments. Akismet proved great for marking these comments as spam in the CMS, but the site admin was still getting an email for each one.

    The site is running SS 2.4.

    So we added an extra line to Marijn's code to check whether the comment had been marked as spam before creating the email.

    File: mysite/code/PageCommentEmailNotification.php

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

    We've also changed Marijn's email template a bit, partly because the site doesn't require people to be logged in or members to comment. We now pass the commenters given name and website URL to the template as well. Here's our email template:

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

    <!DOCTYPE html>
    <html>
       <head>
    <style>
          body {
             font-size: .8em;
             max-width: 45em;
          }
       </style>
       </head>
       <body>
          <p>You have had a new comment on the blog post '$PageTitle'<br>
    <a href="{$URL}">$URL</a>
    </p>
    <hr>
          <p><strong>$Name</strong> (<a href="$CommenterURL.ATT">$CommenterURL.ATT</a>) wrote:</p>

          <p><% if bbCodeEnabled %>
           $ParsedBBCode   
    <% else %>
    $Comment.XML   
    <% end_if %>
    </p>
    <hr>   
          <p>Click here to <a href="{$BaseHref}admin/comments/">approve or reject this comment</a>.</p>
       
       </body>
    </html>

    * You'll need to set an admin email address and enable the extension as per Marijn's original blog post here http://www.exadium.com/tools/silverstripe/silverstripe-page-comment-email-notification/

    The version I actually used is this one on github https://github.com/gordonbanderson/Silverstripe-Comment-Email-Notification-Module

    683 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.