Sending HTML e-mail from Console App

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.

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

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.

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

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

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.

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!

Solved!

Easy - use CController:renderInternal

Would be nice to have an extension for this down the line. :slight_smile: