Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Issue with email->SetTemplate within onAfterWrite

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

Page: 1
Go to End
Author Topic: 1730 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Issue with email->SetTemplate within onAfterWrite Link to this post

    Hi guys,

    I've got a scenario where if a moderator approves a dataobject (entry) within ModelAdmin (alters its status from Pending to Public), the submitter of the entry gets notified by email.

    My code is as below:

    function onAfterWrite(){
       
          $changed = $this->getChangedFields();
          if(isset($changed['EntryStatus'])){
             if($changed['EntryStatus']['before'] == 'Pending' && $changed['EntryStatus']['after'] == 'Public'){
                /* -- SEND USER EMAIL --*/
                $From = SERVERFROM;
                $entry = $this;

                $To = $entry->Email;
                $Subject = 'Entry Approved';
                $email = new Email($From, $To, $Subject);
                //$email->setTemplate('EmailAdmin');
                      
                $data['Image'] = $data['Link'] = '';
                $data['Message'] = 'Your message has been approved'.;
                $data['Feature'] = 'Your entry id is '.$entry->UniqueID;
                      
                //$email->populateTemplate($data);
                $email->send();
             }
          }
          parent::onAfterWrite();
       }

    The email sends fine if I send it without using a template. However, if I try to use a template, I get the eternal spinning Save in ModelAdmin, and no emails are sent. Nothing in the error logs. The template exists as I'm using it to send other emails within page controllers.

    Has anyone come across this before?

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Issue with email->SetTemplate within onAfterWrite Link to this post

    I get the eternal spinning Save in ModelAdmin

    Using Firebug is a good tool for debugging this sort of thing. Install it, and look at the console panel when you save the page. You should see the request go through.

    Taking a stab in the dark it could be something like your theme isn't set in the CMS so if you do something like SSViewer::set_theme('..'); specifically before your setTemplate it may work.

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Issue with email->SetTemplate within onAfterWrite Link to this post

    Thanks Willr - that worked.

    The request was returning "500 Warning: "None of these templates can be found in theme '': EmailAdmin.ss""

    I'm setting the theme in mysite/_config.php using SSViewer::set_theme('theme') -- it's also selected in the CMS cms dropdown, but it won't work in my onAfterWrite() function unless I call it before setting the template as you suggested.

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