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.

Blog Module /

Discuss the Blog Module.

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

[Solved] PageCommentEmailNotification


Reply


5 Posts   771 Views

Avatar
borriej

Community Member, 267 Posts

2 March 2011 at 3:23am

Hello,

I couldn't find the topic, so im posting it again:

Got this little script:

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,
'Name' => $this->owner->Name,
));
$email->send();
}
}
}

It sends emails when new comments are posted on my blog, this works well when your logged in as a admin..

But when your not logged in: you get a pop-up with the html code of a error page. The message content:

   
      <h2>Server error</h2>
   
      <p>Sorry, there was a problem with handling your request.</p>

How do i fix this?

   

Avatar
Willr

Forum Moderator, 5513 Posts

2 March 2011 at 10:35pm

Put the site into devmode. That will give you a better error message.

http://doc.silverstripe.org/sapphire/en/topics/debugging#environment-types

Avatar
borriej

Community Member, 267 Posts

2 March 2011 at 10:53pm

Site is already in dev mode:
Director::set_environment_type("dev");

..

Avatar
borriej

Community Member, 267 Posts

2 March 2011 at 11:06pm

im debugging it now by removing and adding line by line.

found out that the problem occurs when:

if ($this->NeedsModeration) {

}

Is added into this code:

class PageCommentEmailNotification extends DataObjectDecorator {
function onAfterWrite() {
parent::onAfterWrite();
if ($this->NeedsModeration) {

}

What to do now?s

Avatar
borriej

Community Member, 267 Posts

2 March 2011 at 11:11pm

Edited: 02/03/2011 11:12pm

Solved!

For the people who are looking for this code:

config:

DataObject::add_extension('PageComment', 'PageCommentEmailNotification');

PageCommentEmailNotification.php

<?php

class PageCommentEmailNotification extends DataObjectDecorator {
function onAfterWrite() {
parent::onAfterWrite();

       $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,
'Name' => $this->owner->Name,
));
$email->send();
}
}