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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Email notifications on new and updated data objects


Reply


2 Posts   407 Views

Avatar
sajok

Community Member, 82 Posts

17 May 2013 at 12:19am

Hello,

I have a data object "product". I'm using the code below to send an email to the administrator when a new product is created. It will also send a different email when a product record was updated. Can someone till if I'm doing it in the correct way, or if there is a better way to set different email notifcations?

function onBeforeWrite() {
if(!$this->ID) {
$email = new Email();
$email->setTo('admin@domain.com');
$email->setSubject('New Record Created');
$email->setFrom('admin@domain.com');
$email->setBody("A new record was added");
$email->send();
}
if( $this->isChanged() && $this->ID) {
$email = new Email();
$email->setTo('admin@domain.comt');
$email->setSubject('Record details updated');
$email->setFrom('admin@domain.com');
$email->setBody("Details a record are updated");
$email->send();
}
parent::onBeforeWrite();
}

Thanks

Avatar
Bambii7

Community Member, 254 Posts

17 May 2013 at 12:27pm

Thats not a bad way to do it, looks good.

Depending on how frequently products are updated I would find the update email somewhat annoying if it was occurring multiple times a day. I would add a new boolean field to the object, mark it as updated, then send out an email daily or even weekly depending on the requirements.

Using Scheduled tasks, even though it's 2.4 docs it will be the same for 3.x
http://api.silverstripe.org/2.4/class-ScheduledTask.html

class ProductUpdateTask extends DailyTask {
function process() {
// email product summary here
}
}