1 package Catalyst::View::Email;
10 use Email::MIME::Creator;
12 use base qw|Catalyst::View|;
14 our $VERSION = '0.04';
16 __PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
20 Catalyst::View::Email - Send Email from Catalyst
24 This module simply sends out email from a stash key specified in the
25 configuration settings.
29 In your app configuration (example in L<YAML>):
33 content_type: text/plain
36 # mailer_args is passed directly into Email::Send
38 - Host: smtp.example.com
50 In your controller, simply forward to the view after populating the C<stash_key>
52 sub controller : Private {
53 my ( $self, $c ) = @_;
54 $c->stash->{email} = {
55 to => q{catalyst@rocksyoursocks.com},
56 from => q{no-reply@socksthatarerocked.com},
57 subject => qq{Your Subject Here},
58 body => qq{Body Body Body}
60 $c->forward('View::Email');
63 Alternatively, you can use a more raw interface, and specify the headers as
66 $c->stash->{email} = {
69 Subject => 'Note the capitalization differences'
71 body => qq{Ain't got no body, and nobody cares.},
76 content_type => 'text/plain',
77 disposition => 'attachment',
78 charset => 'US-ASCII',
80 body => qq{Got a body, but didn't get ahead.}
85 =head1 HANDLING FAILURES
87 If the email fails to send, the view will die (throw an exception). After
88 your forward to the view, it is a good idea to check for errors:
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!');
95 $c->res->body('Email sent A-OK! (At least as far as we can tell)');
100 Now, it's no fun to just send out email using plain strings. We also
101 have L<Catalyst::View::Email::Template> for use. You can also toggle
102 this as being used by setting up your configuration to look like this:
107 Then, Catalyst::View::Email will forward to your View::TT by default.
112 my $self = shift->next::method(@_);
114 my ( $c, $arguments ) = @_;
116 my $mailer = Email::Send->new;
118 if ( my $method = $self->sender->{method} ) {
119 croak "$method is not supported, see Email::Send"
120 unless $mailer->mailer_available($method);
121 $mailer->mailer($method);
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($_);
129 if ( $self->sender->{mailer_args} ) {
130 $mailer->mailer_args($self->sender->{mailer_args});
133 $self->mailer($mailer);
139 my ( $self, $c ) = @_;
141 croak "Unable to send mail, bad mail configuration"
142 unless $self->mailer;
144 my $email = $c->stash->{$self->stash_key};
145 croak "Can't send email without a valid email structure"
148 if ( $self->content_type ) {
149 $email->{content_type} ||= $self->content_type;
152 my $header = $email->{header} || [];
153 push @$header, ('To' => delete $email->{to})
155 push @$header, ('From' => delete $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};
162 my $parts = $email->{parts};
163 my $body = $email->{body};
165 unless ( $parts or $body ) {
166 croak "Can't send email without parts or body, check stash";
169 my %mime = ( header => $header );
171 if ( $parts and ref $parts eq 'ARRAY' ) {
172 $mime{parts} = $parts;
177 my $message = Email::MIME->create(%mime);
180 $self->mailer->send($message);
182 croak "Unable to create message";
188 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
190 =head2 L<Catalyst::Manual> - The Catalyst Manual
192 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
196 J. Shirley <jshirley@gmail.com>
204 Daniel Westermann-Clark
208 This library is free software, you can redistribute it and/or modify it under
209 the same terms as Perl itself.