[Catalyst] Sending TT'd email
Matthew Hodgson
arathorn at theonering.net
Mon Jul 25 06:05:23 CEST 2005
Hi all,
Further to some chat on #catalyst today (and last month), I put together a
very simple plugin for sending e-mail formatted using TT, which extends
Catalyst::Plugin::Email and maintains its own Template instance & config
(nothing to do with ::View::TT).
sri has pointed out however that:
<sri> guess i'd use subrequest to render a mail
* Arathorn nods
<Arathorn> yeah, i guess there isn't much difference between that and
doing a $c->email(header => [...], body=> $c->subreq("/rendermail", {
template=>'email/template.eml', foo=>'bar', ... }) );
and I've yet to decide whether there's any advantage to my plugin over
that (which obviously allows you to use all the existing View::TT goodness
with about the same level of syntactic sugar).
I'm enclosing a first draft of my module here anyway, just in case it's
ever of use or interest to anyone in the future. If anyone can think of a
reason why it'd be useful to go into cpan, please say ;)
cheers;
M.
package Catalyst::Plugin::Email::TT;
use strict;
use base qw(Class::Data::Inheritable Catalyst::Plugin::Email);
use Template;
our $VERSION = '0.01';
__PACKAGE__->mk_classdata('template');
=head1 NAME
Catalyst::Plugin::Email::TT - Send emails templated using Template Toolkit with
Catalyst (subclasses Catalyst::Plugin::Email)
=head1 SYNOPSIS
package MyApp;
use Catalyst 'Email::TT';
# configure Email
__PACKAGE__->config->{email} = [qw/SMTP smtp.arasphere.net/];
# configure TT
__PACKAGE__->config->{emailtt}->{DEBUG} = 'all';
package MyApp::C::MyController;
$c->email(
header => [
From => 'matthew at arasphere.net',
To => 'sri at cpan.org',
Subject => 'Hello!'
],
template => 'hello.eml',
stash => { greeting => 'Hi',
name => 'Sebastian',
signoff => 'cheers,',
sender => 'Arathorn',
},
);
=head1 DESCRIPTION
Send emails templated by Template Toolkit with Catalyst and L<Email::Send> and
L<Email::MIME::Creator>.
If you want to override TT config settings, you can do it there by setting C<<
__PACKAGE__->config->{emailtt}->{OPTION} >> as shown in the synopsis. Of
interest might be C<EVAL_PERL>, which is disabled by default, and
C<LOAD_TEMPLATES>, which is set to use the provider.
=head2 METHODS
=head3 email
=cut
sub email {
my $c = shift;
my $email = $_[1] ? {@_} : $_[0];
if (exists $email->{template}) {
my $body;
my $name = $email->{template};
$c->log->debug(qq/Rendering template "$name"/) if $c->debug;
unless (
$c->template->process(
$name,
{
%{ $email->{stash} }
},
\$body
)
)
{
my $error = $c->template->error;
$error = qq/Couldn't render template "$error"/;
$c->log->error($error);
$c->error($error);
}
$email->{body} = $body;
}
$c->SUPER::email($email);
}
=head3 setup
=cut
sub setup {
my $self = shift;
my $root = $self->config->{root};
$self->config->{emailtt} ||= {};
my %config = (
EVAL_PERL => 0,
INCLUDE_PATH => [ $root, "$root/base" ],
%{ $self->config->{emailtt} }
);
$self->template( Template->new( \%config ) );
return $self->NEXT::setup(@_);
}
=head1 SEE ALSO
L<Catalyst>, L<Catalyst::Plugin::Email>, L<Template::Toolkit>
=head1 AUTHOR
Matthew Hodgson, C<matthew at arasphere.net>
=head1 COPYRIGHT
This program is free software, you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
More information about the Catalyst
mailing list