Hi,
Our application is an online booking system. Once a day we want to run a cron job to send out reminder emails if someone's account becomes due; we would like those emails to be nicely formatted in HTML. We have an emailer class that is extended from CController that we use for sending emails based on events, e.g. creating a booking, and this allows us to use controller rendering, layouts, etc. which allows us to nicely keep content and layout separated.
Running a cron job seems to be a job for a console application. Problem with this is that when it comes to rendering the emails Yii::app()->getTheme is called. Yii::app() being a console app doesn't have the getTheme method (it's in CWebApplication).
We could of course run the cron jobs as web apps, but we think that this makes them available to the world - something we don't want for obvious reasons.
Have we missed something? Any help to achieve what we want to do is appreciated.
Page 1 of 1
Sending HTML e-mail from Console App
#2
Posted 19 September 2009 - 05:53 PM
Yeti, on 19 September 2009 - 03:30 PM, said:
Running a cron job seems to be a job for a console application. Problem with this is that when it comes to rendering the emails Yii::app()->getTheme is called. Yii::app() being a console app doesn't have the getTheme method (it's in CWebApplication).
I'm not sure about this, but looking at the implementation of getTheme() in CWebApplication, I think you can create an instance of CThemeManager or extend ConsoleApplication with a themeManager core component, a _theme property and the methods getTheme()/setTheme().
$this->_theme = getThemeManager()->getTheme($this->_theme);
/Tommy
#3
Posted 20 September 2009 - 09:13 AM
Thanks to Tommy for the reply; it got us thinking.
We have got this working, but it feels like a cludge - the first time that's happened with Yii, and we still wonder if we are doing it the best way.
What we have done is write a Yii bootstrap for the console app that overrides YiiBase::createConsoleApplication. This returns our EConsoleApplication that is extended from CConsoleApplication.
In EConsoleApplication we have put the methods and properties from CWebApplication we need to get things to work; namely:
Methods: getTheme(), setTheme($value), getViewPath(), setViewPath($path), getSystemViewPath(), setSystemViewPath($path), getLayoutPath(), setLayoutPath($path), getClientScript(), getViewRenderer()
Properties: $_viewPath, $_systemViewPath, $_layoutPath, $_theme
Right now it's only the getters we need, but have put in the setters to be safe for the future.
If anyone has a better (right??) way of doing this please let us know; I'm sure we aren't the only team wanting to do this.
We have got this working, but it feels like a cludge - the first time that's happened with Yii, and we still wonder if we are doing it the best way.
What we have done is write a Yii bootstrap for the console app that overrides YiiBase::createConsoleApplication. This returns our EConsoleApplication that is extended from CConsoleApplication.
In EConsoleApplication we have put the methods and properties from CWebApplication we need to get things to work; namely:
Methods: getTheme(), setTheme($value), getViewPath(), setViewPath($path), getSystemViewPath(), setSystemViewPath($path), getLayoutPath(), setLayoutPath($path), getClientScript(), getViewRenderer()
Properties: $_viewPath, $_systemViewPath, $_layoutPath, $_theme
Right now it's only the getters we need, but have put in the setters to be safe for the future.
If anyone has a better (right??) way of doing this please let us know; I'm sure we aren't the only team wanting to do this.
#4
Posted 20 September 2009 - 09:29 AM
I'm not sure about this either, but take a look at how the yiic shell command is sort of morphing itself into a webb app.
/Tommy
/Tommy
#5
Posted 20 November 2010 - 11:25 AM
Hi,
now I have the same problem as you. Could you write about you experiences with adding extra functionality to ConsoleApplication to be usable.
Greetings,
KL
now I have the same problem as you. Could you write about you experiences with adding extra functionality to ConsoleApplication to be usable.
Greetings,
KL
#6
Posted 15 January 2011 - 03:46 PM
Hi - has anyone been able to figure out how to accomplish the above. I am extending the right classes and overriding, but Yii::app()->theme always has a NULL reference.
Thanks.
Thanks.
#7
Posted 16 January 2011 - 12:04 AM
Ok, so now I figured out how to wire in and access Yii::app()->theme from within the console application - question now is how do we go about GENERATING the html as we don't have access to the render/renderPartial methods of CController?
I want to basically be able to pass in dynamic data to the view and the view styles it in a nice format and the rendering engine returns the ENTIRE html contents which I just push to the end user via PHP's native mail API.
thanks!
I want to basically be able to pass in dynamic data to the view and the view styles it in a nice format and the rendering engine returns the ENTIRE html contents which I just push to the end user via PHP's native mail API.
thanks!
#8
Posted 16 January 2011 - 03:04 AM
sandeep, on 16 January 2011 - 12:04 AM, said:
Ok, so now I figured out how to wire in and access Yii::app()->theme from within the console application - question now is how do we go about GENERATING the html as we don't have access to the render/renderPartial methods of CController?
I want to basically be able to pass in dynamic data to the view and the view styles it in a nice format and the rendering engine returns the ENTIRE html contents which I just push to the end user via PHP's native mail API.
thanks!
I want to basically be able to pass in dynamic data to the view and the view styles it in a nice format and the rendering engine returns the ENTIRE html contents which I just push to the end user via PHP's native mail API.
thanks!
Solved!
Easy - use CController:renderInternal
Would be nice to have an extension for this down the line. :-)
Share this topic:
Page 1 of 1

Help
This topic is locked











