redo dwc's patch to actually fix the problem rather than creating an extra one
[catagits/Catalyst-View-Email.git] / lib / Catalyst / View / Email.pm
CommitLineData
529915ab 1package Catalyst::View::Email;
2
3use warnings;
4use strict;
5
29840e4a 6use Class::C3;
529915ab 7use Carp;
8
9use Email::Send;
10use Email::MIME::Creator;
11
12use base qw|Catalyst::View|;
13
25650747 14our $VERSION = '0.02';
529915ab 15
d49594f5 16__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
529915ab 17
18=head1 NAME
19
20Catalyst::View::Email - Send Email from Catalyst
21
22=head1 SYNOPSIS
23
24This module simply sends out email from a stash key specified in the
25configuration settings.
26
27=head1 CONFIGURATION
28
29In your app configuration (example in L<YAML>):
25650747 30
529915ab 31 View::Email:
32 stash_key: email
33 content_type: text/plain
34 sender:
35 method: SMTP
b3d2ac28 36 # mailer_args is passed directly into Email::Send
37 mailer_args:
38 - Host: smtp.example.com
39 - username: username
40 - password: password
529915ab 41
42=cut
43
44__PACKAGE__->config(
45 stash_key => 'email',
46);
47
48=head1 SENDING EMAIL
49
50In your controller, simply forward to the view after populating the C<stash_key>
51
52 sub controller : Private {
53 my ( $self, $c ) = @_;
54 $c->stash->{email} = {
b3d2ac28 55 to => q{catalyst@rocksyoursocks.com},
56 from => q{no-reply@socksthatarerocked.com},
529915ab 57 subject => qq{Your Subject Here},
58 body => qq{Body Body Body}
59 };
60 $c->forward('View::Email');
61 }
62
63Alternatively, you can use a more raw interface, and specify the headers as
64an array reference.
65
66 $c->stash->{email} = {
67 header => [
68 To => 'foo@bar.com',
69 Subject => 'Note the capitalization differences'
70 ],
71 body => qq{Ain't got no body, and nobody cares.},
72 # Or, send parts
73 parts => [
74 Email::MIME->create(
75 attributes => {
76 content_type => 'text/plain',
77 disposition => 'attachment',
78 charset => 'US-ASCII',
79 },
80 body => qq{Got a body, but didn't get ahead.}
81 )
82 ],
83 };
84
85=head1 HANDLING FAILURES
86
87If the email fails to send, the view will die (throw an exception). After
88your forward to the view, it is a good idea to check for errors:
89
90 $c->forward('View::Email');
91 if ( scalar( @{ $c->error } ) ) {
92 $c->error(0); # Reset the error condition if you need to
93 $c->res->body('Oh noes!');
94 } else {
95 $c->res->body('Email sent A-OK! (At least as far as we can tell)');
96 }
97
98=head1 OTHER MAILERS
99
25650747 100Now, it's no fun to just send out email using plain strings. We also
101have L<Catalyst::View::Email::Template> for use. You can also toggle
102this as being used by setting up your configuration to look like this:
529915ab 103
104 View::Email:
25650747 105 default_view: TT
529915ab 106
25650747 107Then, Catalyst::View::Email will forward to your View::TT by default.
529915ab 108
109=cut
110
111sub new {
25650747 112 my $self = shift->next::method(@_);
113
114 my ( $c, $arguments ) = @_;
529915ab 115
116 my $mailer = Email::Send->new;
117
d49594f5 118 if ( my $method = $self->sender->{method} ) {
529915ab 119 croak "$method is not supported, see Email::Send"
120 unless $mailer->mailer_available($method);
121 $mailer->mailer($method);
122 } else {
123 # Default case, run through the most likely options first.
124 for ( qw/SMTP Sendmail Qmail/ ) {
125 $mailer->mailer($_) and last if $mailer->mailer_available($_);
126 }
127 }
128
d49594f5 129 if ( $self->sender->{mailer_args} ) {
130 $mailer->mailer_args($self->sender->{mailer_args});
529915ab 131 }
132
133 $self->mailer($mailer);
134
135 return $self;
136}
137
138sub process {
139 my ( $self, $c ) = @_;
140
141 croak "Unable to send mail, bad mail configuration"
142 unless $self->mailer;
143
d49594f5 144 my $email = $c->stash->{$self->stash_key};
529915ab 145 croak "Can't send email without a valid email structure"
146 unless $email;
147
d49594f5 148 if ( $self->content_type ) {
149 $email->{content_type} ||= $self->content_type;
529915ab 150 }
151
152 my $header = $email->{header} || [];
153 push @$header, ('To' => delete $email->{to})
154 if $email->{to};
155 push @$header, ('From' => delete $email->{from})
156 if $email->{from};
157 push @$header, ('Subject' => delete $email->{subject})
158 if $email->{subject};
159 push @$header, ('Content-type' => delete $email->{content_type})
160 if $email->{content_type};
161
162 my $parts = $email->{parts};
163 my $body = $email->{body};
164
165 unless ( $parts or $body ) {
166 croak "Can't send email without parts or body, check stash";
167 }
168
169 my %mime = ( header => $header );
170
171 if ( $parts and ref $parts eq 'ARRAY' ) {
172 $mime{parts} = $parts;
173 } else {
174 $mime{body} = $body;
175 }
176
177 my $message = Email::MIME->create(%mime);
178
179 if ( $message ) {
180 $self->mailer->send($message);
181 } else {
182 croak "Unable to create message";
183 }
184}
185
186=head1 SEE ALSO
187
188=head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
189
190=head2 L<Catalyst::Manual> - The Catalyst Manual
191
192=head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
193
194=head1 AUTHORS
195
196J. Shirley <jshirley@gmail.com>
197
25650747 198=head1 CONTRIBUTORS
199
200(Thanks!)
201
202Daniel Westermann-Clark
203
529915ab 204=head1 LICENSE
205
206This library is free software, you can redistribute it and/or modify it under
207the same terms as Perl itself.
208
209=cut
210
2111;