1 package Catalyst::View::Email;
10 use Email::MIME::Creator;
12 use base qw|Catalyst::View|;
14 our $VERSION = '0.07';
16 #__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
17 __PACKAGE__->mk_accessors(qw(stash_key content_type mailer));
21 Catalyst::View::Email - Send Email from Catalyst
25 This module simply sends out email from a stash key specified in the
26 configuration settings.
30 In your app configuration (example in L<YAML>):
34 content_type: text/plain
37 # mailer_args is passed directly into Email::Send
39 Host: smtp.example.com # defaults to localhost
45 If you use SMTP and don't specify Host, it will default to localhost and
46 attempt delivery. This often times means an email will sit in a queue
47 somewhere and not be delivered.
54 content_type => 'text/html',
60 In your controller, simply forward to the view after populating the C<stash_key>
62 sub controller : Private {
63 my ( $self, $c ) = @_;
64 $c->stash->{email} = {
65 to => q{catalyst@rocksyoursocks.com},
66 from => q{no-reply@socksthatarerocked.com},
67 subject => qq{Your Subject Here},
68 body => qq{Body Body Body}
70 $c->forward('View::Email');
73 Alternatively, you can use a more raw interface, and specify the headers as
76 $c->stash->{email} = {
79 Subject => 'Note the capitalization differences'
81 body => qq{Ain't got no body, and nobody cares.},
86 content_type => 'text/plain',
87 disposition => 'attachment',
88 charset => 'US-ASCII',
90 body => qq{Got a body, but didn't get ahead.}
95 =head1 HANDLING FAILURES
97 If the email fails to send, the view will die (throw an exception). After
98 your forward to the view, it is a good idea to check for errors:
100 $c->forward('View::Email');
101 if ( scalar( @{ $c->error } ) ) {
102 $c->error(0); # Reset the error condition if you need to
103 $c->res->body('Oh noes!');
105 $c->res->body('Email sent A-OK! (At least as far as we can tell)');
110 Now, it's no fun to just send out email using plain strings. We also
111 have L<Catalyst::View::Email::Template> for use. You can also toggle
112 this as being used by setting up your configuration to look like this:
118 Then, Catalyst::View::Email will forward to your View::TT by default.
123 my $self = shift->next::method(@_);
125 my ( $c, $arguments ) = @_;
127 my $sender = Email::Send->new;
129 if ( my $mailer = $self->{sender}->{mailer} ) {
130 croak "$mailer is not supported, see Email::Send"
131 unless $sender->mailer_available($mailer);
132 $sender->mailer($mailer);
134 # Default case, run through the most likely options first.
135 for ( qw/SMTP Sendmail Qmail/ ) {
136 $sender->mailer($_) and last if $sender->mailer_available($_);
140 if ( my $args = $self->{sender}->{mailer_args} ) {
141 if ( ref $args eq 'HASH' ) {
142 $sender->mailer_args([ %$args ]);
144 elsif ( ref $args eq 'ARRAY' ) {
145 $sender->mailer_args($args);
147 croak "Invalid mailer_args specified, check pod for Email::Send!";
151 $self->mailer($sender);
157 my ( $self, $c ) = @_;
159 croak "Unable to send mail, bad mail configuration"
160 unless $self->mailer;
162 my $email = $c->stash->{$self->stash_key};
163 croak "Can't send email without a valid email structure"
166 if ( $self->content_type ) {
167 $email->{content_type} ||= $self->content_type;
170 my $header = $email->{header} || [];
171 push @$header, ('To' => delete $email->{to})
173 push @$header, ('From' => delete $email->{from})
175 push @$header, ('Subject' => delete $email->{subject})
176 if $email->{subject};
177 push @$header, ('Content-type' => delete $email->{content_type})
178 if $email->{content_type};
180 my $parts = $email->{parts};
181 my $body = $email->{body};
183 unless ( $parts or $body ) {
184 croak "Can't send email without parts or body, check stash";
187 my %mime = ( header => $header );
189 if ( $parts and ref $parts eq 'ARRAY' ) {
190 $mime{parts} = $parts;
195 my $message = Email::MIME->create(%mime);
198 my $return = $self->mailer->send($message);
199 croak "$return" if !$return;
201 croak "Unable to create message";
207 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
209 =head2 L<Catalyst::Manual> - The Catalyst Manual
211 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
215 J. Shirley <jshirley@gmail.com>
223 Daniel Westermann-Clark
225 Simon Elliott <cpan@browsing.co.uk> - ::Template
231 This library is free software, you can redistribute it and/or modify it under
232 the same terms as Perl itself.