Yii Framework Forum: Sending HTML e-mail from Console App - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Sending HTML e-mail from Console App Rate Topic: -----

#1 User is offline   Yeti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 30-May 09

Posted 19 September 2009 - 03:30 PM

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

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 19 September 2009 - 05:53 PM

View PostYeti, 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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Yeti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 30-May 09

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

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   liysd 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-June 10

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
0

#6 User is offline   sandeep 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 24-September 10

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

#7 User is offline   sandeep 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 24-September 10

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!
0

#8 User is offline   sandeep 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 24-September 10

Posted 16 January 2011 - 03:04 AM

View Postsandeep, 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!


Solved!

Easy - use CController:renderInternal

Would be nice to have an extension for this down the line. :-)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users