1 package Catalyst::View::Email;
9 use Email::MIME::Creator;
11 use base qw|Catalyst::View|;
13 our $VERSION = '0.01';
15 __PACKAGE__->mk_accessors('mailer');
19 Catalyst::View::Email - Send Email from Catalyst
23 This module simply sends out email from a stash key specified in the
24 configuration settings.
28 In your app configuration (example in L<YAML>):
31 content_type: text/plain
46 In your controller, simply forward to the view after populating the C<stash_key>
48 sub controller : Private {
49 my ( $self, $c ) = @_;
50 $c->stash->{email} = {
51 to => qq{catalyst@rocksyoursocks.com},
52 from => qq{no-reply@socksthatarerocked.com},
53 subject => qq{Your Subject Here},
54 body => qq{Body Body Body}
56 $c->forward('View::Email');
59 Alternatively, you can use a more raw interface, and specify the headers as
62 $c->stash->{email} = {
65 Subject => 'Note the capitalization differences'
67 body => qq{Ain't got no body, and nobody cares.},
72 content_type => 'text/plain',
73 disposition => 'attachment',
74 charset => 'US-ASCII',
76 body => qq{Got a body, but didn't get ahead.}
81 =head1 HANDLING FAILURES
83 If the email fails to send, the view will die (throw an exception). After
84 your forward to the view, it is a good idea to check for errors:
86 $c->forward('View::Email');
87 if ( scalar( @{ $c->error } ) ) {
88 $c->error(0); # Reset the error condition if you need to
89 $c->res->body('Oh noes!');
91 $c->res->body('Email sent A-OK! (At least as far as we can tell)');
96 Now, it's no fun to just send out email using plain strings. We also have
97 L<Catalyst::View::Email::TT> for use. You can also toggle this as being used
98 by setting up your configuration to look like this:
103 Then, Catalyst::View::Email will forward to View::Email::TT by default.
108 my ( $class ) = shift;
109 my $self = $class->next::method(@_);
111 my $mailer = Email::Send->new;
113 if ( my $method = $self->config->{sender}->{method} ) {
114 croak "$method is not supported, see Email::Send"
115 unless $mailer->mailer_available($method);
116 $mailer->mailer($method);
118 # Default case, run through the most likely options first.
119 for ( qw/SMTP Sendmail Qmail/ ) {
120 $mailer->mailer($_) and last if $mailer->mailer_available($_);
124 if ( $mailer->mailer eq 'SMTP' ) {
125 my $host = $self->config->{sender}->{host} || 'localhost';
126 $mailer->mailer_args([ Host => $host ]);
129 $self->mailer($mailer);
135 my ( $self, $c ) = @_;
137 croak "Unable to send mail, bad mail configuration"
138 unless $self->mailer;
140 my $email = $c->stash->{$self->config->{stash_key}};
141 croak "Can't send email without a valid email structure"
144 if ( $self->config->{content_type} ) {
145 $email->{content_type} ||= $self->config->{content_type};
148 my $header = $email->{header} || [];
149 push @$header, ('To' => delete $email->{to})
151 push @$header, ('From' => delete $email->{from})
153 push @$header, ('Subject' => delete $email->{subject})
154 if $email->{subject};
155 push @$header, ('Content-type' => delete $email->{content_type})
156 if $email->{content_type};
158 my $parts = $email->{parts};
159 my $body = $email->{body};
161 unless ( $parts or $body ) {
162 croak "Can't send email without parts or body, check stash";
165 my %mime = ( header => $header );
167 if ( $parts and ref $parts eq 'ARRAY' ) {
168 $mime{parts} = $parts;
173 my $message = Email::MIME->create(%mime);
176 $self->mailer->send($message);
178 croak "Unable to create message";
184 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat
186 =head2 L<Catalyst::Manual> - The Catalyst Manual
188 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
192 J. Shirley <jshirley@gmail.com>
196 This library is free software, you can redistribute it and/or modify it under
197 the same terms as Perl itself.