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->view('TT')->render($c, 'email.tt', {
68 additional_template_paths => [ $c->config->{root} . '/email_templates'],
73 To send a multipart message, include a C<parts> argument containing an
74 arrayref of Email::MIME objects.
79 content_type => 'application/pdf',
80 encoding => 'quoted-printable',
87 content_type => 'text/plain',
88 disposition => 'attachment',
89 charset => 'US-ASCII',
91 body => $c->view('TT')->render($c, 'email.tt', {
92 additional_template_paths => [ $c->config->{root} . '/email_templates'],
93 names => [qw/foo bar baz/]
101 To => 'me@localhost',
102 Subject => 'A TT Email',
111 my $email = $_[1] ? {@_} : $_[0];
112 croak "Can't send mail without recipient"
113 unless length($email->{To});
114 $email = Email::MIME->create(%$email);
115 my $args = $c->config->{email} || [];
118 unless ( $class = shift @args ) {
120 unshift @args, 'localhost';
122 send $class => $email, @args;
125 =head1 USING WITH A VIEW
127 A common practice is to handle emails using the same template language used
128 for HTML pages. This is best accomplished by capturing the output from the
129 template. For TT this is done using the C<render> method as described in
130 L<Catalyst::View:TT/CAPTURING TEMPLATE OUTPUT>.
132 Here is a short example of rendering an email from a Template Toolkit source
133 file. For more information on render (or how to do this with a view other
134 than TT, consult the docs for your view)
136 sub send_email : Local {
137 my ( $self, $c ) = @_;
141 To => 'me@localhost',
142 Subject => 'A TT Email',
144 body => $c->view('TT')->render('email.tt',
145 { additional_template_paths => [ $c->config->{root} . '/email_templates'],
146 names => [ qw/andyg sri mst ash/ ],
150 # redirect or display a message
155 [%- FOREACH name IN names -%]
176 L<Catalyst>, L<Catalyst::View::TT>, L<Email::Send>, L<Email::MIME::Creator>
180 Sebastian Riedel, C<sri@cpan.org>
184 Andy Grundman - Additional documentation
186 Carl Franks - Additional documentation
190 This program is free software, you can redistribute it and/or modify it
191 under the same terms as Perl itself.