Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Combine Akismet spam checking with comment email notifications

Go to End



Community Member, 2 Posts

21 November 2012 at 7:05am

Edited: 21/11/2012 7:06am

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() {
  	if ($this->owner->SpamClass() != 'spam') {
		$email = new Email();
		$email->addCustomHeader('Reply-To', Member::currentUser()->Email);
		$email->setSubject('New Comment on "' . $this->owner->Parent()->Title . '"');
				'URL' => Director::absoluteBaseURL() . $this->owner->Parent()->URLSegment,
				'PageTitle' => $this->owner->Parent()->Title,
				'Comment' => $this->owner->Comment,
				'Name' => $this->owner->Name,
				'CommenterURL' => $this->owner->CommenterURL,

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/

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

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

* You'll need to set an admin email address and enable the extension as per Marijn's original blog post here

The version I actually used is this one on github