1 package Catalyst::View::Email;
6 use Encode qw(encode decode);
7 use Email::Sender::Simple qw/ sendmail /;
8 use Email::MIME::Creator;
9 use Email::Sender::Transport;
10 extends 'Catalyst::View';
12 our $VERSION = '0.19';
25 default => sub { "email" }
31 default => sub { { content_type => 'text/plain' } },
39 default => sub { { mailer => shift->transport } }
42 has 'content_type' => (
45 default => sub { shift->default->{content_type} },
58 Catalyst::View::Email - Send Email from Catalyst
62 This module sends out emails from a stash key specified in the
63 configuration settings.
67 WARNING: since version 0.10 the configuration options slightly changed!
69 Use the helper to create your View:
71 $ script/myapp_create.pl view Email Email
73 In your app configuration:
77 # Where to look in the stash for the email information.
78 # 'email' is the default, so you don't have to specify it.
80 # Define the defaults for the mail
82 # Defines the default content type (mime type). Mandatory
83 content_type => 'text/plain',
84 # Defines the default charset for every MIME part with the
86 # According to RFC2049 a MIME part without a charset should
87 # be treated as US-ASCII by the mail client.
88 # If the charset is not set it won't be set for all MIME parts
89 # without an overridden one.
93 # Setup how to send the email
94 # all those options are passed directly to Email::Send
97 # mailer_args is passed directly into Email::Send
99 Host => 'smtp.example.com', # defaults to localhost
100 username => 'username',
101 password => 'password',
109 If you use SMTP and don't specify Host, it will default to localhost and
110 attempt delivery. This often means an email will sit in a queue and
117 Sending email is just filling the stash and forwarding to the view:
119 sub controller : Private {
120 my ( $self, $c ) = @_;
122 $c->stash->{email} = {
123 to => 'jshirley@gmail.com',
124 cc => 'abraxxa@cpan.org',
125 bcc => join ',', qw/hidden@secret.com hidden2@foobar.com/,
126 from => 'no-reply@foobar.com',
127 subject => 'I am a Catalyst generated email',
128 body => 'Body Body Body',
131 $c->forward( $c->view('Email') );
134 Alternatively you can use a more raw interface and specify the headers as
135 an array reference like it is passed to L<Email::MIME::Creator>.
136 Note that you may also mix both syntaxes if you like ours better but need to
137 specify additional header attributes.
138 The attributes are appended to the header array reference without overwriting
141 $c->stash->{email} = {
143 To => 'jshirley@gmail.com',
144 Cc => 'abraxxa@cpan.org',
145 Bcc => join ',', qw/hidden@secret.com hidden2@foobar.com/,
146 From => 'no-reply@foobar.com',
147 Subject => 'Note the capitalization differences',
149 body => qq{Ain't got no body, and nobody cares.},
154 content_type => 'text/plain',
155 disposition => 'attachment',
156 charset => 'US-ASCII',
158 body => qq{Got a body, but didn't get ahead.},
163 =head1 HANDLING ERRORS
165 If the email fails to send, the view will die (throw an exception).
166 After your forward to the view, it is a good idea to check for errors:
168 $c->forward( $c->view('Email') );
170 if ( scalar( @{ $c->error } ) ) {
171 $c->error(0); # Reset the error condition if you need to
172 $c->response->body('Oh noes!');
174 $c->response->body('Email sent A-OK! (At least as far as we can tell)');
177 =head1 USING TEMPLATES FOR EMAIL
179 Now, it's no fun to just send out email using plain strings.
180 Take a look at L<Catalyst::View::Email::Template> to see how you can use your
181 favourite template engine to render the mail body.
189 Validates the base config and creates the L<Email::Sender::Simple> object for later use
197 my $stash_key = $self->stash_key;
198 croak "$self stash_key isn't defined!"
199 if ( $stash_key eq '' );
205 The process method does the actual processing when the view is dispatched to.
207 This method sets up the email parts and hands off to L<Email::Send> to handle
208 the actual email delivery.
213 my ( $self, $c ) = @_;
215 croak "Unable to send mail, bad mail configuration"
216 unless $self->mailer;
218 my $email = $c->stash->{ $self->stash_key };
219 croak "Can't send email without a valid email structure"
222 # Default content type
223 if ( $self->content_type and not $email->{content_type} ) {
224 $email->{content_type} = $self->content_type;
227 my $header = $email->{header} || [];
228 push @$header, ( 'To' => delete $email->{to} )
230 push @$header, ( 'Cc' => delete $email->{cc} )
232 push @$header, ( 'Bcc' => delete $email->{bcc} )
234 push @$header, ( 'From' => delete $email->{from} )
237 ( 'Subject' => Encode::encode( 'MIME-Header', delete $email->{subject} ) )
238 if $email->{subject};
239 push @$header, ( 'Content-type' => $email->{content_type} )
240 if $email->{content_type};
242 my $parts = $email->{parts};
243 my $body = $email->{body};
245 unless ( $parts or $body ) {
246 croak "Can't send email without parts or body, check stash";
249 my %mime = ( header => $header, attributes => {} );
251 if ( $parts and ref $parts eq 'ARRAY' ) {
252 $mime{parts} = $parts;
258 $mime{attributes}->{content_type} = $email->{content_type}
259 if $email->{content_type};
260 if ( $mime{attributes}
261 and not $mime{attributes}->{charset}
262 and $self->{default}->{charset} )
264 $mime{attributes}->{charset} = $self->{default}->{charset};
267 my $message = $self->generate_message( $c, \%mime );
270 my $return = sendmail( $message, { transport => $self->mailer } );
272 # return is a Return::Value object, so this will stringify as the error
273 # in the case of a failure.
274 croak "$return" if !$return;
277 croak "Unable to create message";
281 =item setup_attributes($c, $attr)
283 Merge attributes with the configured defaults. You can override this method to
284 return a structure to pass into L<generate_message> which subsequently
285 passes the return value of this method to Email::MIME->create under the
290 sub setup_attributes {
291 my ( $self, $c, $attrs ) = @_;
293 my $default_content_type = $self->default->{content_type};
294 my $default_charset = $self->default->{charset};
298 if ( exists $attrs->{content_type}
299 && defined $attrs->{content_type}
300 && $attrs->{content_type} ne '' )
302 $c->log->debug( 'C::V::Email uses specified content_type '
303 . $attrs->{content_type}
306 $e_m_attrs->{content_type} = $attrs->{content_type};
308 elsif ( defined $default_content_type && $default_content_type ne '' ) {
310 "C::V::Email uses default content_type $default_content_type.")
312 $e_m_attrs->{content_type} = $default_content_type;
315 if ( exists $attrs->{charset}
316 && defined $attrs->{charset}
317 && $attrs->{charset} ne '' )
319 $e_m_attrs->{charset} = $attrs->{charset};
321 elsif ( defined $default_charset && $default_charset ne '' ) {
322 $e_m_attrs->{charset} = $default_charset;
328 =item generate_message($c, $attr)
330 Generate a message part, which should be an L<Email::MIME> object and return it.
332 Takes the attributes, merges with the defaults as necessary and returns a
337 sub generate_message {
338 my ( $self, $c, $attr ) = @_;
340 # setup the attributes (merge with defaultis)
341 $attr->{attributes} = $self->setup_attributes($c, $attr->{attributes});
349 =head1 TROUBLESHOOTING
351 As with most things computer related, things break. Email even more so.
352 Typically any errors are going to come from using SMTP as your sending method,
353 which means that if you are having trouble the first place to look is at
354 L<Email::Send::SMTP>. This module is just a wrapper for L<Email::Send>,
355 so if you get an error on sending, it is likely from there anyway.
357 If you are using SMTP and have troubles sending, whether it is authentication
358 or a very bland "Can't send" message, make sure that you have L<Net::SMTP> and,
359 if applicable, L<Net::SMTP::SSL> installed.
361 It is very simple to check that you can connect via L<Net::SMTP>, and if you
362 do have sending errors the first thing to do is to write a simple script
363 that attempts to connect. If it works, it is probably something in your
364 configuration so double check there. If it doesn't, well, keep modifying
365 the script and/or your mail server configuration until it does!
369 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
371 =head2 L<Catalyst::Manual> - The Catalyst Manual
373 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
377 J. Shirley <jshirley@gmail.com>
379 Alexander Hartmaier <abraxxa@cpan.org>
387 Daniel Westermann-Clark
389 Simon Elliott <cpan@browsing.co.uk>
393 Lance Brown <lance@bearcircle.net>
395 Devin Austin <dhoss@cpan.org>
399 Copyright (c) 2007 - 2009
400 the Catalyst::View::Email L</AUTHORS> and L</CONTRIBUTORS>
405 This library is free software, you can redistribute it and/or modify it under
406 the same terms as Perl itself.