Changed docs to use view('TT')->render not $c->subreq
[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
0da82223 9our $VERSION = '0.06';
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 ],
ea0b5860 67 body => $c->view('TT')->render($c, 'email.tt', {
68 additional_template_paths => [ $c->config->{root} . '/email_templates'],
69 }
70 ),
0da82223 71 );
72
73To send a multipart message, include a C<parts> argument containing an
74arrayref of Email::MIME objects.
75
76 my @parts = (
77 Email::MIME->create(
78 attributes => {
79 content_type => 'application/pdf',
80 encoding => 'quoted-printable',
81 name => 'report.pdf',
82 },
83 body => $FILE_DATA,
84 ),
85 Email::MIME->create(
86 attributes => {
87 content_type => 'text/plain',
88 disposition => 'attachment',
89 charset => 'US-ASCII',
90 },
ea0b5860 91 body => $c->view('TT')->render($c, 'email.tt', {
92 additional_template_paths => [ $c->config->{root} . '/email_templates'],
93 names => [qw/foo bar baz/]
94 }
95 ),
0da82223 96 ),
97 );
98
99 $c->email(
100 header => [
101 To => 'me@localhost',
102 Subject => 'A TT Email',
103 ],
104 parts => \@parts,
105 );
106
107=cut
108
109sub email {
110 my $c = shift;
111 my $email = $_[1] ? {@_} : $_[0];
23938968 112 croak "Can't send mail without recipient"
113 unless length($email->{To});
0da82223 114 $email = Email::MIME->create(%$email);
115 my $args = $c->config->{email} || [];
116 my @args = @{$args};
117 my $class;
118 unless ( $class = shift @args ) {
119 $class = 'SMTP';
120 unshift @args, 'localhost';
121 }
122 send $class => $email, @args;
123}
124
95b3de58 125=head1 USING WITH A VIEW
5a5a0df4 126
95b3de58 127A common practice is to handle emails using the same template language used
ea0b5860 128for HTML pages. This is best accomplished by capturing the output from the
129template. For TT this is done using the C<render> method as described in
130L<Catalyst::View:TT/CAPTURING TEMPLATE OUTPUT>.
95b3de58 131
132Here is a short example of rendering an email from a Template Toolkit source
ea0b5860 133file. For more information on render (or how to do this with a view other
134than TT, consult the docs for your view)
95b3de58 135
136 sub send_email : Local {
137 my ( $self, $c ) = @_;
138
139 $c->email(
140 header => [
141 To => 'me@localhost',
142 Subject => 'A TT Email',
143 ],
ea0b5860 144 body => $c->view('TT')->render('email.tt',
145 { additional_template_paths => [ $c->config->{root} . '/email_templates'],
146 names => [ qw/andyg sri mst ash/ ],
147 } ),
148
95b3de58 149 );
150 # redirect or display a message
151 }
ea0b5860 152
95b3de58 153And the template:
154
155 [%- FOREACH name IN names -%]
156 Hi, [% name %]!
157 [%- END -%]
158
159 --
160 Regards,
161 Us
162
163Output:
164
165 Hi, andyg!
166 Hi, sri!
167 Hi, mst!
ea0b5860 168 Hi, ash!
95b3de58 169
170 --
171 Regards,
172 Us
173
5a5a0df4 174=head1 SEE ALSO
175
ea0b5860 176L<Catalyst>, L<Catalyst::View::TT>, L<Email::Send>, L<Email::MIME::Creator>
5a5a0df4 177
178=head1 AUTHOR
179
180Sebastian Riedel, C<sri@cpan.org>
181
0da82223 182=head1 THANKS
183
184Andy Grundman - Additional documentation
185
186Carl Franks - Additional documentation
187
5a5a0df4 188=head1 COPYRIGHT
189
1ab6815b 190This program is free software, you can redistribute it and/or modify it
191under the same terms as Perl itself.
5a5a0df4 192
193=cut
194
1951;