1 package Catalyst::Plugin::Email;
6 use Email::MIME::Creator;
12 Catalyst::Plugin::Email - Send emails with Catalyst
18 __PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];
22 From => 'sri@oook.de',
31 Send emails with Catalyst and L<Email::Send> and L<Email::MIME::Creator>.
35 C<config> accepts the same options as L<Email::Send>.
37 To send using the system's C<sendmail> program, set C<config> like so:
39 __PACKAGE__->config->{email} = ['Sendmail'];
41 To send using authenticated SMTP:
43 __PACKAGE__->config->{email} = [
46 username => $USERNAME,
47 password => $PASSWORD,
50 For different methods of sending emails, and appropriate C<config> options,
51 see L<Email::Send::NNTP>, L<Email::Send::Qmail>, L<Email::Send::SMTP> and
52 L<Email::Send::Sendmail>.
58 C<email()> accepts the same arguments as L<Email::MIME::Creator>'s
64 Subject => 'A TT Email',
66 body => $c->subreq( '/render_email' ),
69 To send a multipart message, include a C<parts> argument containing an
70 arrayref of Email::MIME objects.
75 content_type => 'application/pdf',
76 encoding => 'quoted-printable',
83 content_type => 'text/plain',
84 disposition => 'attachment',
85 charset => 'US-ASCII',
87 body => $c->subreq( '/render_email' ),
94 Subject => 'A TT Email',
103 my $email = $_[1] ? {@_} : $_[0];
104 $email = Email::MIME->create(%$email);
105 my $args = $c->config->{email} || [];
108 unless ( $class = shift @args ) {
110 unshift @args, 'localhost';
112 send $class => $email, @args;
115 =head1 USING WITH A VIEW
117 A common practice is to handle emails using the same template language used
118 for HTML pages. This can be accomplished by pairing this plugin with
119 L<Catalyst::Plugin::SubRequest>.
121 Here is a short example of rendering an email from a Template Toolkit source
122 file. The call to $c->subreq makes an internal call to the render_email
123 method just like an external call from a browser. The request will pass
124 through the end method to be processed by your View class.
126 sub send_email : Local {
127 my ( $self, $c ) = @_;
131 To => 'me@localhost',
132 Subject => 'A TT Email',
134 body => $c->subreq( '/render_email' ),
136 # redirect or display a message
139 sub render_email : Local {
140 my ( $self, $c ) = @_;
143 names => [ qw/andyg sri mst/ ],
144 template => 'email.tt',
150 [%- FOREACH name IN names -%]
170 L<Catalyst>, L<Catalyst::Plugin::SubRequest>, L<Email::Send>,
171 L<Email::MIME::Creator>
175 Sebastian Riedel, C<sri@cpan.org>
179 Andy Grundman - Additional documentation
181 Carl Franks - Additional documentation
185 This program is free software, you can redistribute it and/or modify it
186 under the same terms as Perl itself.