1 package Catalyst::Plugin::Email;
6 use Email::MIME::Creator;
13 Catalyst::Plugin::Email - Send emails with Catalyst
19 __PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];
23 From => 'sri@oook.de',
32 Send emails with Catalyst and L<Email::Send> and L<Email::MIME::Creator>.
36 C<config> accepts the same options as L<Email::Send>.
38 To send using the system's C<sendmail> program, set C<config> like so:
40 __PACKAGE__->config->{email} = ['Sendmail'];
42 To send using authenticated SMTP:
44 __PACKAGE__->config->{email} = [
47 username => $USERNAME,
48 password => $PASSWORD,
51 For different methods of sending emails, and appropriate C<config> options,
52 see L<Email::Send::NNTP>, L<Email::Send::Qmail>, L<Email::Send::SMTP> and
53 L<Email::Send::Sendmail>.
59 C<email()> accepts the same arguments as L<Email::MIME::Creator>'s
65 Subject => 'A TT Email',
67 body => $c->subreq( '/render_email' ),
70 To send a multipart message, include a C<parts> argument containing an
71 arrayref of Email::MIME objects.
76 content_type => 'application/pdf',
77 encoding => 'quoted-printable',
84 content_type => 'text/plain',
85 disposition => 'attachment',
86 charset => 'US-ASCII',
88 body => $c->subreq( '/render_email' ),
95 Subject => 'A TT Email',
104 my $email = $_[1] ? {@_} : $_[0];
105 $email = Email::MIME->create(%$email);
106 my $args = $c->config->{email} || [];
109 unless ( $class = shift @args ) {
111 unshift @args, 'localhost';
113 send $class => $email, @args;
116 =head1 USING WITH A VIEW
118 A common practice is to handle emails using the same template language used
119 for HTML pages. If your view supports the 'render' method (Like the TT view
120 does), you just set the body like this:
123 To => 'me@localhost',
124 Subject => 'A TT Email',
126 body => $c->view('TT')->render($c,'mytemplate.tt'),
129 If your view doesn't support render, you can just forward to it, then reset
132 sub send_email : Local {
133 my ( $self, $c ) = @_;
135 local $c->stash->{names} = [ qw/andyg sri mst/ ],
136 local $c->stash->{template}= 'mytemplate.tt';
137 $c->forward($c->view('MyView'));
140 To => 'me@localhost',
141 Subject => 'A TT Email',
143 body => $c->res->body,
145 $c->res->body(undef);
151 [%- FOREACH name IN names -%]
171 L<Catalyst>, L<Catalyst::Plugin::SubRequest>, L<Email::Send>,
172 L<Email::MIME::Creator>
176 Sebastian Riedel, C<sri@cpan.org>
179 Marcus Ramberg C<mramberg@cpan.org>
183 This program is free software, you can redistribute it and/or modify it
184 under the same terms as Perl itself.