List Info

Thread: Sending email from Catalyst and scripts




Sending email from Catalyst and scripts
user name
2007-05-22 13:10:58
Hello list,

I'm looking for best practices for sending email from the
catalyst app
that would also work with command-line scripts (cronjobs,
one-time
scripts, etc.).

In my current setup, Email model takes care of storing
metadata in the
database for each sent email, and Email controller has a
private
method which takes care of generating and sending actual
emails. Is
this the right way to go, or would it make more sense to
move the
email sending functionality to some utility module so it
works equally
well for non-catalyst scripts as well? Or is it easy enough
to
setup/prepare the catalyst context in the scripts so that I
could use
the send_email method from the catalyst Email controller?

What is the most common email framework setup among catalyst
users? I
think it would be useful to add a few possible examples for
this to
Catalyst::Manual::Cookbook as well.

Thanks.
-- 
-----------------------------------------------------
Evaldas Imbrasas
http://www.imbrasas.com

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: Sending email from Catalyst and scripts
country flaguser name
United Kingdom
2007-05-22 16:31:31
On Tue, May 22, 2007 at 11:10:58AM -0700, Evaldas Imbrasas
wrote:
> I'm looking for best practices for sending email from
the catalyst app
> that would also work with command-line scripts
(cronjobs, one-time
> scripts, etc.).

Personally I use a database table as a crude email queue. I
should
probably use an MTA, but I don't.

The table is pretty simple; id, queued_time, recipient, cc,
bcc, sender,
subject, text_content, html_content.
Obviously you can move the recipient, etc out into join
tables, etc if
you want, but this suits my needs.

Then my application can dump emails there, in fact anything
can if it
wishes.
The application uses the ->render() method on a view that
doesn't
automatically include header.tt and footer.tt from the
application
itself. (MyApp::View::Plain vs MyApp::View::TT - both use
TT, they're
just configured differently)

While I'm developing, no emails get sent, and I poke the db
to double
check things. From time to time I'll run a simple daemon
that picks up
emails, builds and delivers them (using MIME::Lite).


I don't like sending emails directly from controller
actions. I'm
worried about things taking a long time, no idea what's gone
through the
system, and so on.


-- 
Chisel Wright
e: chiselherlpacker.co.uk
w: http://www.herlpacker.co
.uk/

  If you were MEANT to understand it, we wouldn't have
called it 'code'

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: Sending email from Catalyst and scripts
user name
2007-05-22 17:15:39
On 5/22/07, Evaldas Imbrasas < evaldasimbrasas.com">evaldasimbrasas.com> wrote:
Hello list,

I&#39;m looking for best practices for sending email from the catalyst app
that would also work with command-line scripts (cronjobs, one-time
scripts, etc.).

In my current setup, Email model takes care of storing metadata in the
database for each sent email, and Email controller has a private
method which takes care of generating and sending actual emails. Is
this the right way to go, or would it make more sense to move the
email sending functionality to some utility module so it works equally
well for non-catalyst scripts as well? Or is it easy enough to
setup/prepare the catalyst context in the scripts so that I could use
the send_email method from the catalyst Email controller?

What is the most common email framework setup among catalyst users? I
think it would be useful to add a few possible examples for this to
Catalyst::Manual::Cookbook as well.

Thanks.
--
-----------------------------------------------------
Evaldas Imbrasas
http://www.imbrasas.com

Hi Evaldas,

I've recently been working on an Email view, which I hope will become the recommended way to send Email from Catalyst.&nbsp; It hitches into Email::Send, and also ties into your Template view (which, sadly, only works with View::TT at the moment but I'm hoping to get the other views supported soon)

I haven't posted it to CPAN yet, as I'm waiting to get some tests written up for it, but you can download it from the Catalyst SVN repository at: http://dev.catalystframework.org/repos/Catalyst/trunk/Catalyst-View-Email/

I also have a tarball at http://staff.toeat.com/~jshirley/Catalyst-View-Email-0.01.tar.gz

I recommend you read through the pod for both Catalyst::View::Email and Catalyst::View::Email::Template

And, as for sending out an email via the command line, there are many ways to achieve this depending upon what your goal and full use case is.

Good luck, and happy hacking,
-J

--
J. Shirley :: jshirleygmail.com">jshirleygmail.com :: Killing two stones with one bird...
http://www.toeat.com
Re: Sending email from Catalyst and scripts
user name
2007-05-22 18:02:27
J,

I've looked at your module, but I'm afraid it would actually
make
sending emails even more dependent on catalyst, and I want
to be able
to use the same email framework from both catalyst and
command-line
scripts.

In my setup, each email belongs to one emailclass. Metadata
for each
emailclass, including template_name, is stored in the
database.
template_name defines the location for the template files
for this
email class. A list of template files usually consists of
templates
for text version, HTML version, email subject, and email
headers (all
of them are TT templates). Each email class also has its own
test
script which sends a sample email for that class.

I have two TT views - one for the text emails, and another
for HTML
emails (each view has its own wrappers). As I've mentioned
before,
send_email method in Controller::Email does the bulk of
work: based on
the emailclass, it fetches the template files and uses the
TT views to
render the parts of the email, which is then contructed
using
Email::MIME::Creator (although I could have used
Catalyst::Plugin::Email as well), stores metadata for this
email into
database, and then actually sends it.

So, to rephrase, my question is actually not about how to
send email
from catalyst - the above setup works really well. I just
want to be
able to use the same method to send emails from catalyst
AND
command-line scripts. I see two ways to consider (there
might be
more):

1) Somehow setup the catalyst context $c in the command-line
scripts,
so I could call the same send_email method from
Controller::Email. Is
this possible?

2) Move the bulk of send_email out of catalyst and just
leave a
wrapper for that method in Controller::Email. The problem
would be
that catalyst provides many helpful methods (i.e., easy
access to
preconfigured views and models), and those wouldn't be
available
outside of catalyst.


On 5/22/07, J. Shirley <jshirleygmail.com> wrote:
> I've recently been working on an Email view, which I
hope will become the
> recommended way to send Email from Catalyst.  It
hitches into Email::Send,
> and also ties into your Template view (which, sadly,
only works with
> View::TT at the moment but I'm hoping to get the other
views supported soon)
>
> I haven't posted it to CPAN yet, as I'm waiting to get
some tests written up
> for it, but you can download it from the Catalyst SVN
repository at:
> http://dev.catalystframework.org/repos
/Catalyst/trunk/Catalyst-View-Email/
>
> I also have a tarball at
> http://staff.toeat.com/~jshirley/Catalyst-View-E
mail-0.01.tar.gz
>
> I recommend you read through the pod for both
Catalyst::View::Email and
> Catalyst::View::Email::Template
>
> And, as for sending out an email via the command line,
there are many ways
> to achieve this depending upon what your goal and full
use case is.
>
> Good luck, and happy hacking,
> -J

-- 
-----------------------------------------------------
Evaldas Imbrasas
http://www.imbrasas.com

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: Sending email from Catalyst and scripts
country flaguser name
United Kingdom
2007-05-23 01:41:21
Why can you not do something like
$c->stash-> and then just 
fire them off in the end() action after the page has been
returned to the 
remote browser?

M


On Tuesday 22 May 2007 10:31 pm, Chisel Wright wrote:
> On Tue, May 22, 2007 at 11:10:58AM -0700, Evaldas
Imbrasas wrote:
> > I'm looking for best practices for sending email
from the catalyst app
> > that would also work with command-line scripts
(cronjobs, one-time
> > scripts, etc.).
>
> Personally I use a database table as a crude email
queue. I should
> probably use an MTA, but I don't.
>
> The table is pretty simple; id, queued_time, recipient,
cc, bcc, sender,
> subject, text_content, html_content.
> Obviously you can move the recipient, etc out into join
tables, etc if
> you want, but this suits my needs.
>
> Then my application can dump emails there, in fact
anything can if it
> wishes.
> The application uses the ->render() method on a view
that doesn't
> automatically include header.tt and footer.tt from the
application
> itself. (MyApp::View::Plain vs MyApp::View::TT - both
use TT, they're
> just configured differently)
>
> While I'm developing, no emails get sent, and I poke
the db to double
> check things. From time to time I'll run a simple
daemon that picks up
> emails, builds and delivers them (using MIME::Lite).
>
>
> I don't like sending emails directly from controller
actions. I'm
> worried about things taking a long time, no idea what's
gone through the
> system, and so on.

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: Sending email from Catalyst and scripts
country flaguser name
United Kingdom
2007-05-23 04:32:31
On Wed, May 23, 2007 at 07:41:21AM +0100, Mark Zealey
wrote:
> Why can you not do something like
$c->stash-> and then just 
> fire them off in the end() action after the page has
been returned to the 
> remote browser?
> 

Because I don't want to spend any longer between *click* and
"page
loaded" than I have to.
I feel happier having a queue of emails that I can process
at my
leisure.

I don't have any benchmarking or test cases to show that my
way is
better, or faster - but I'm not claiming this. It's Just The
Way I Do It
and it makes me feel more comfortable with the process.
-- 
Chisel Wright
e: chiselherlpacker.co.uk
w: http://www.herlpacker.co
.uk/

  Sorry isn't an excuse when you do something stupid on
purpose.

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )