NAME
Catalyst::Plugin::Email - Send emails with Catalyst
SYNOPSIS
use Catalyst 'Email';
__PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];
$c->email(
header => [
From => 'sri@oook.de',
To => 'sri@cpan.org',
Subject => 'Hello!'
],
body => 'Hello sri'
);
DESCRIPTION
Send emails with Catalyst and Email::Send and Email::MIME::Creator.
CONFIGURATION
config
accepts the same options as Email::Send.
To send using the system's sendmail
program, set config
like so:
__PACKAGE__->config->{email} = ['Sendmail'];
To send using authenticated SMTP:
__PACKAGE__->config->{email} = [
'SMTP',
'smtp.myhost.com',
username => $USERNAME,
password => $PASSWORD,
];
For different methods of sending emails, and appropriate config
options, see Email::Send::NNTP, Email::Send::Qmail, Email::Send::SMTP and Email::Send::Sendmail.
METHODS
email()
accepts the same arguments as Email::MIME::Creator's create()
.
$c->email(
header => [
To => 'me@localhost',
Subject => 'A TT Email',
],
body => $c->subreq( '/render_email' ),
);
To send a multipart message, include a parts
argument containing an arrayref of Email::MIME objects.
my @parts = (
Email::MIME->create(
attributes => {
content_type => 'application/pdf',
encoding => 'quoted-printable',
name => 'report.pdf',
},
body => $FILE_DATA,
),
Email::MIME->create(
attributes => {
content_type => 'text/plain',
disposition => 'attachment',
charset => 'US-ASCII',
},
body => $c->subreq( '/render_email' ),
),
);
$c->email(
header => [
To => 'me@localhost',
Subject => 'A TT Email',
],
parts => \@parts,
);
USING WITH A VIEW
A common practice is to handle emails using the same template language used for HTML pages. This can be accomplished by pairing this plugin with Catalyst::Plugin::SubRequest.
Here is a short example of rendering an email from a Template Toolkit source file. The call to $c->subreq makes an internal call to the render_email method just like an external call from a browser. The request will pass through the end method to be processed by your View class.
sub send_email : Local {
my ( $self, $c ) = @_;
$c->email(
header => [
To => 'me@localhost',
Subject => 'A TT Email',
],
body => $c->subreq( '/render_email' ),
);
# redirect or display a message
}
sub render_email : Local {
my ( $self, $c ) = @_;
$c->stash(
names => [ qw/andyg sri mst/ ],
template => 'email.tt',
);
}
And the template:
[%- FOREACH name IN names -%]
Hi, [% name %]!
[%- END -%]
--
Regards,
Us
Output:
Hi, andyg!
Hi, sri!
Hi, mst!
--
Regards,
Us
SEE ALSO
Catalyst, Catalyst::Plugin::SubRequest, Email::Send, Email::MIME::Creator
AUTHOR
Sebastian Riedel, sri@cpan.org
THANKS
Andy Grundman - Additional documentation
Carl Franks - Additional documentation
COPYRIGHT
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.