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