prepared for c-p-e 0.07
[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
b6568b03 119for HTML pages. This can be accomplished by pairing this plugin with
120L<Catalyst::Plugin::SubRequest>.
95b3de58 121
122Here is a short example of rendering an email from a Template Toolkit source
b6568b03 123file. The call to $c->subreq makes an internal call to the render_email
124method just like an external call from a browser. The request will pass
125through the end method to be processed by your View class.
95b3de58 126
127 sub send_email : Local {
128 my ( $self, $c ) = @_;
129
130 $c->email(
131 header => [
132 To => 'me@localhost',
133 Subject => 'A TT Email',
134 ],
b6568b03 135 body => $c->subreq( '/render_email' ),
95b3de58 136 );
137 # redirect or display a message
138 }
b6568b03 139
140 sub render_email : Local {
141 my ( $self, $c ) = @_;
142
143 $c->stash(
144 names => [ qw/andyg sri mst/ ],
145 template => 'email.tt',
146 );
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>
177
0da82223 178=head1 THANKS
179
180Andy Grundman - Additional documentation
181
182Carl Franks - Additional documentation
183
5a5a0df4 184=head1 COPYRIGHT
185
1ab6815b 186This program is free software, you can redistribute it and/or modify it
187under the same terms as Perl itself.
5a5a0df4 188
189=cut
190
1911;