1 package Catalyst::View::Email;
10 use Email::MIME::Creator;
12 use base qw|Catalyst::View|;
14 our $VERSION = '0.01';
16 __PACKAGE__->mk_accessors('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>):
32 content_type: text/plain
35 # mailer_args is passed directly into Email::Send
37 - Host: smtp.example.com
49 In your controller, simply forward to the view after populating the C<stash_key>
51 sub controller : Private {
52 my ( $self, $c ) = @_;
53 $c->stash->{email} = {
54 to => q{catalyst@rocksyoursocks.com},
55 from => q{no-reply@socksthatarerocked.com},
56 subject => qq{Your Subject Here},
57 body => qq{Body Body Body}
59 $c->forward('View::Email');
62 Alternatively, you can use a more raw interface, and specify the headers as
65 $c->stash->{email} = {
68 Subject => 'Note the capitalization differences'
70 body => qq{Ain't got no body, and nobody cares.},
75 content_type => 'text/plain',
76 disposition => 'attachment',
77 charset => 'US-ASCII',
79 body => qq{Got a body, but didn't get ahead.}
84 =head1 HANDLING FAILURES
86 If the email fails to send, the view will die (throw an exception). After
87 your forward to the view, it is a good idea to check for errors:
89 $c->forward('View::Email');
90 if ( scalar( @{ $c->error } ) ) {
91 $c->error(0); # Reset the error condition if you need to
92 $c->res->body('Oh noes!');
94 $c->res->body('Email sent A-OK! (At least as far as we can tell)');
99 Now, it's no fun to just send out email using plain strings. We also have
100 L<Catalyst::View::Email::TT> for use. You can also toggle this as being used
101 by setting up your configuration to look like this:
106 Then, Catalyst::View::Email will forward to View::Email::TT by default.
111 my ( $class ) = shift;
112 my $self = $class->next::method(@_);
114 my $mailer = Email::Send->new;
116 if ( my $method = $self->config->{sender}->{method} ) {
117 croak "$method is not supported, see Email::Send"
118 unless $mailer->mailer_available($method);
119 $mailer->mailer($method);
121 # Default case, run through the most likely options first.
122 for ( qw/SMTP Sendmail Qmail/ ) {
123 $mailer->mailer($_) and last if $mailer->mailer_available($_);
127 if ( $self->config->{sender}->{mailer_args} ) {
128 $mailer->mailer_args($self->config->{sender}->{mailer_args});
131 $self->mailer($mailer);
137 my ( $self, $c ) = @_;
139 croak "Unable to send mail, bad mail configuration"
140 unless $self->mailer;
142 my $email = $c->stash->{$self->config->{stash_key}};
143 croak "Can't send email without a valid email structure"
146 if ( $self->config->{content_type} ) {
147 $email->{content_type} ||= $self->config->{content_type};
150 my $header = $email->{header} || [];
151 push @$header, ('To' => delete $email->{to})
153 push @$header, ('From' => delete $email->{from})
155 push @$header, ('Subject' => delete $email->{subject})
156 if $email->{subject};
157 push @$header, ('Content-type' => delete $email->{content_type})
158 if $email->{content_type};
160 my $parts = $email->{parts};
161 my $body = $email->{body};
163 unless ( $parts or $body ) {
164 croak "Can't send email without parts or body, check stash";
167 my %mime = ( header => $header );
169 if ( $parts and ref $parts eq 'ARRAY' ) {
170 $mime{parts} = $parts;
175 my $message = Email::MIME->create(%mime);
178 $self->mailer->send($message);
180 croak "Unable to create message";
186 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
188 =head2 L<Catalyst::Manual> - The Catalyst Manual
190 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
194 J. Shirley <jshirley@gmail.com>
198 This library is free software, you can redistribute it and/or modify it under
199 the same terms as Perl itself.