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:

Data Model Questions /

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

Issue with email->SetTemplate within onAfterWrite

Go to End

3 Posts   2635 Views


Community Member, 75 Posts

31 March 2011 at 10:56pm

Edited: 31/03/2011 10:57pm

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($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);
				$data['Image'] = $data['Link'] = '';
				$data['Message'] = 'Your message has been approved'.;
				$data['Feature'] = 'Your entry id is '.$entry->UniqueID;

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?


Forum Moderator, 5523 Posts

1 April 2011 at 6:08pm

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.


Community Member, 75 Posts

3 April 2011 at 7:44pm

Edited: 03/04/2011 7:44pm

Thanks Willr - that worked.

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

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.