[Catalyst] Suggestions for sending email from cron job using'Catalyst' templates?

Octavian Rasnita orasnita at gmail.com
Mon Apr 11 15:58:22 GMT 2011


From: "Charlie Garrison" <garrison at zeta.org.au>
Good evening,

First, I may not be approaching this the right way at all. So 
I'm happy to take any suggestions for a better way to do what I need.

I've got a daily task (cron job currently) which sends emails to 
users. I want to use existing TT templates to generate the email 
content. I'm using Mail::Builder::Simple for the message 
creation & sending.

All the simple tests are working fine, until I want to use the 
existing template setup that I've been using to send email from 
within Catalyst. Specifically, the wrapper template (with 
header, footer, etc) makes fairly extensive use of c.uri_for and 
c.uri_for_action. And of course the Catalyst context is not 
available from the cron job (at least no way that I could find).

One option is to move all the logic to a controller action and 
just have the cron job make an http request. That doesn't seem 
like the right approach though.

Does anyone have suggestions on how I can use my existing 
template structure and still be able to use `c` from a cron job? 
Or should I use a completely different approach?

Thanks,
Charlie



Hi Charlie,

The TT templates used by the Catalyst app might contain many things like c.user, c.uri_for_action, may display images, may depend on other templates which are loaded automaticly when those templates are specified in the app config file. So there is no sane way of using those wrapper templates for sending email from a cron job.
When you send a picture included in the email message you may want to embed it and not let the email message access it on your web server. Also, the Catalyst context depend on the request, so you'll need to make a web request in order to have the same context variable and it would affect much the performance if you'll load the Cat app just for sending email. You can do it this way, but... it isn't a way I prefer to do this thing.

Whenever you will find that a template used in the Cat app can't be used in a cron job, split that template into smaller templates, and for the templates that use the Catalyst context create another version that doesn't use it. Then make a template that include those templates which work in the Cat app and another template that works in the cron job.

This way, the Cat app and the standalone program will share those templates they can share, and there won't be dupplicate templates, but there won't be any problems with those parts of the templates that use the Catalyst context.

You need to use the same approach you use whenever you find that you need to use a part of a template into another template... extract it as a separate template and include it in both templates with INCLUDE or PROCESS.

If there are too many parts of the template that needs to be changed, it is a better idea to create a separate entire template for using it in the external program. The alternative of sharing the templates among the Cat app and other external program would involve using templates that don't depend on the Catalyst app, and this can be done easily, but we would miss many features offered by the Catalyst context, and we probably need to share too few templates to prefer to lose those features.

Octavian




More information about the Catalyst mailing list