update docs not to use subrequest
[catagits/Catalyst-Plugin-Email.git] / Email.pm
CommitLineData
5a5a0df4 1package Catalyst::Plugin::Email;
2
3use strict;
4use Email::Send;
5use Email::MIME;
6use Email::MIME::Creator;
23938968 7use Carp qw/croak/;
5a5a0df4 8
b6568b03 9our $VERSION = '0.07';
5a5a0df4 10
11=head1 NAME
12
13Catalyst::Plugin::Email - Send emails with Catalyst
14
15=head1 SYNOPSIS
16
17 use Catalyst 'Email';
18
19 __PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];
20
21 $c->email(
22 header => [
23 From => 'sri@oook.de',
24 To => 'sri@cpan.org',
25 Subject => 'Hello!'
26 ],
27 body => 'Hello sri'
28 );
29
30=head1 DESCRIPTION
31
32Send emails with Catalyst and L<Email::Send> and L<Email::MIME::Creator>.
33
0da82223 34=head1 CONFIGURATION
35
36C<config> accepts the same options as L<Email::Send>.
37
38To send using the system's C<sendmail> program, set C<config> like so:
39
40 __PACKAGE__->config->{email} = ['Sendmail'];
41
42To send using authenticated SMTP:
43
44 __PACKAGE__->config->{email} = [
45 'SMTP',
46 'smtp.myhost.com',
47 username => $USERNAME,
48 password => $PASSWORD,
49 ];
50
51For different methods of sending emails, and appropriate C<config> options,
52see L<Email::Send::NNTP>, L<Email::Send::Qmail>, L<Email::Send::SMTP> and
53L<Email::Send::Sendmail>.
54
55=head1 METHODS
56
57=head2 email
58
59C<email()> accepts the same arguments as L<Email::MIME::Creator>'s
60C<create()>.
61
62 $c->email(
63 header => [
64 To => 'me@localhost',
65 Subject => 'A TT Email',
66 ],
b6568b03 67 body => $c->subreq( '/render_email' ),
0da82223 68 );
69
70To send a multipart message, include a C<parts> argument containing an
71arrayref of Email::MIME objects.
72
73 my @parts = (
74 Email::MIME->create(
75 attributes => {
76 content_type => 'application/pdf',
77 encoding => 'quoted-printable',
78 name => 'report.pdf',
79 },
80 body => $FILE_DATA,
81 ),
82 Email::MIME->create(
83 attributes => {
84 content_type => 'text/plain',
85 disposition => 'attachment',
86 charset => 'US-ASCII',
87 },
b6568b03 88 body => $c->subreq( '/render_email' ),
0da82223 89 ),
90 );
91
92 $c->email(
93 header => [
94 To => 'me@localhost',
95 Subject => 'A TT Email',
96 ],
97 parts => \@parts,
98 );
99
100=cut
101
102sub email {
103 my $c = shift;
104 my $email = $_[1] ? {@_} : $_[0];
105 $email = Email::MIME->create(%$email);
106 my $args = $c->config->{email} || [];
107 my @args = @{$args};
108 my $class;
109 unless ( $class = shift @args ) {
110 $class = 'SMTP';
111 unshift @args, 'localhost';
112 }
113 send $class => $email, @args;
114}
115
95b3de58 116=head1 USING WITH A VIEW
5a5a0df4 117
95b3de58 118A common practice is to handle emails using the same template language used
acbb02e2 119for HTML pages. If your view supports the 'render' method (Like the TT view
120does), you just set the body like this:
121 $c->email(
122 header => [
123 To => 'me@localhost',
124 Subject => 'A TT Email',
125 ],
126 body => $c->view('TT')->render($c,'mytemplate.tt'),
127 }
128
129If your view doesn't support render, you can just forward to it, then reset
130the body like this:
95b3de58 131
132 sub send_email : Local {
133 my ( $self, $c ) = @_;
acbb02e2 134 {
135 local $c->stash->{names} = [ qw/andyg sri mst/ ],
136 local $c->stash->{template}= 'mytemplate.tt';
137 $c->forward($c->view('MyView'));
95b3de58 138 $c->email(
139 header => [
140 To => 'me@localhost',
141 Subject => 'A TT Email',
142 ],
acbb02e2 143 body => $c->res->body,
b6568b03 144 );
acbb02e2 145 $c->res->body(undef);
146 }
b6568b03 147 }
148
95b3de58 149And the template:
150
151 [%- FOREACH name IN names -%]
152 Hi, [% name %]!
153 [%- END -%]
154
155 --
156 Regards,
157 Us
158
159Output:
160
161 Hi, andyg!
162 Hi, sri!
163 Hi, mst!
164
165 --
166 Regards,
167 Us
168
5a5a0df4 169=head1 SEE ALSO
170
b6568b03 171L<Catalyst>, L<Catalyst::Plugin::SubRequest>, L<Email::Send>,
172L<Email::MIME::Creator>
5a5a0df4 173
174=head1 AUTHOR
175
176Sebastian Riedel, C<sri@cpan.org>
acbb02e2 177Andy Grundman
178Carl Franks
179Marcus Ramberg C<mramberg@cpan.org>
0da82223 180
5a5a0df4 181=head1 COPYRIGHT
182
1ab6815b 183This program is free software, you can redistribute it and/or modify it
184under the same terms as Perl itself.
5a5a0df4 185
186=cut
187
1881;