Email 0.05, added doc section on using with TT
[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;
7
95b3de58 8our $VERSION = '0.05';
5a5a0df4 9
10=head1 NAME
11
12Catalyst::Plugin::Email - Send emails with Catalyst
13
14=head1 SYNOPSIS
15
16 use Catalyst 'Email';
17
18 __PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];
19
20 $c->email(
21 header => [
22 From => 'sri@oook.de',
23 To => 'sri@cpan.org',
24 Subject => 'Hello!'
25 ],
26 body => 'Hello sri'
27 );
28
29=head1 DESCRIPTION
30
31Send emails with Catalyst and L<Email::Send> and L<Email::MIME::Creator>.
32
95b3de58 33=head1 USING WITH A VIEW
5a5a0df4 34
95b3de58 35A common practice is to handle emails using the same template language used
36for HTML pages. This can be accomplished by pairing this plugin with
37L<Catalyst::Plugin::SubRequest>.
38
39Here is a short example of rendering an email from a Template Toolkit source
40file. The call to $c->subreq makes an internal call to the render_email
41method just like an external call from a browser. The request will pass
42through the end method to be processed by your View class.
43
44 sub send_email : Local {
45 my ( $self, $c ) = @_;
46
47 $c->email(
48 header => [
49 To => 'me@localhost',
50 Subject => 'A TT Email',
51 ],
52 body => $c->subreq( '/render_email' ),
53 );
54 # redirect or display a message
55 }
56
57 sub render_email : Local {
58 my ( $self, $c ) = @_;
59
60 $c->stash(
61 names => [ qw/andyg sri mst/ ],
62 template => 'email.tt',
63 );
64 }
65
66And the template:
67
68 [%- FOREACH name IN names -%]
69 Hi, [% name %]!
70 [%- END -%]
71
72 --
73 Regards,
74 Us
75
76Output:
77
78 Hi, andyg!
79 Hi, sri!
80 Hi, mst!
81
82 --
83 Regards,
84 Us
85
86=head1 METHODS
87
88=head2 email
5a5a0df4 89
90=cut
91
92sub email {
93 my $c = shift;
94 my $email = $_[1] ? {@_} : $_[0];
95 $email = Email::MIME->create(%$email);
96 my $args = $c->config->{email} || [];
97 my @args = @{$args};
98 my $class;
99 unless ( $class = shift @args ) {
100 $class = 'SMTP';
101 unshift @args, 'localhost';
102 }
103 send $class => $email, @args;
104}
105
106=head1 SEE ALSO
107
95b3de58 108L<Catalyst>, L<Catalyst::Plugin::SubRequest>, L<Email::Send>,
109L<Email::MIME::Creator>
5a5a0df4 110
111=head1 AUTHOR
112
113Sebastian Riedel, C<sri@cpan.org>
114
115=head1 COPYRIGHT
116
1ab6815b 117This program is free software, you can redistribute it and/or modify it
118under the same terms as Perl itself.
5a5a0df4 119
120=cut
121
1221;