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 croak "Can't send mail without recipient"
106 unless length($email->{To});
107 $email = Email::MIME->create(%$email);
108 my $args = $c->config->{email} || [];
111 unless ( $class = shift @args ) {
113 unshift @args, 'localhost';
115 send $class => $email, @args;
118 =head1 USING WITH A VIEW
120 A common practice is to handle emails using the same template language used
121 for HTML pages. This can be accomplished by pairing this plugin with
122 L<Catalyst::Plugin::SubRequest>.
124 Here is a short example of rendering an email from a Template Toolkit source
125 file. The call to $c->subreq makes an internal call to the render_email
126 method just like an external call from a browser. The request will pass
127 through the end method to be processed by your View class.
129 sub send_email : Local {
130 my ( $self, $c ) = @_;
134 To => 'me@localhost',
135 Subject => 'A TT Email',
137 body => $c->subreq( '/render_email' ),
139 # redirect or display a message
142 sub render_email : Local {
143 my ( $self, $c ) = @_;
146 names => [ qw/andyg sri mst/ ],
147 template => 'email.tt',
153 [%- FOREACH name IN names -%]
173 L<Catalyst>, L<Catalyst::Plugin::SubRequest>, L<Email::Send>,
174 L<Email::MIME::Creator>
178 Sebastian Riedel, C<sri@cpan.org>
182 Andy Grundman - Additional documentation
184 Carl Franks - Additional documentation
188 This program is free software, you can redistribute it and/or modify it
189 under the same terms as Perl itself.