Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Email notifications on new and updated data objects

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

Page: 1
Go to End
Author Topic: 337 Views
  • sajok
    Avatar
    Community Member
    82 Posts

    Email notifications on new and updated data objects Link to this post

    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

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Email notifications on new and updated data objects Link to this post

    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
    }
    }

    337 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.