X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FEmail.pm;h=fba2330b1d397b54a4ebc534fa84a8596091ca6b;hb=95629d4625192029f3f84a42950f87792c6552c8;hp=ac41fbcce35e5f8bcffcffc566bc01c47d68ff59;hpb=29840e4acd9d9a12b893a950c31254a47222a34b;p=catagits%2FCatalyst-View-Email.git diff --git a/lib/Catalyst/View/Email.pm b/lib/Catalyst/View/Email.pm index ac41fbc..fba2330 100644 --- a/lib/Catalyst/View/Email.pm +++ b/lib/Catalyst/View/Email.pm @@ -11,9 +11,9 @@ use Email::MIME::Creator; use base qw|Catalyst::View|; -our $VERSION = '0.01'; +our $VERSION = '0.07'; -__PACKAGE__->mk_accessors('mailer'); +__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer)); =head1 NAME @@ -27,14 +27,23 @@ configuration settings. =head1 CONFIGURATION In your app configuration (example in L): + View::Email: stash_key: email content_type: text/plain sender: method: SMTP - host: smtp.myhost.com - username: username - password: password + # mailer_args is passed directly into Email::Send + mailer_args: + Host: smtp.example.com # defaults to localhost + username: username + password: password + +=head2 NOTE ON SMTP + +If you use SMTP and don't specify Host, it will default to localhost and +attempt delivery. This often times means an email will sit in a queue +somewhere and not be delivered. =cut @@ -49,8 +58,8 @@ In your controller, simply forward to the view after populating the C sub controller : Private { my ( $self, $c ) = @_; $c->stash->{email} = { - to => qq{catalyst@rocksyoursocks.com}, - from => qq{no-reply@socksthatarerocked.com}, + to => q{catalyst@rocksyoursocks.com}, + from => q{no-reply@socksthatarerocked.com}, subject => qq{Your Subject Here}, body => qq{Body Body Body} }; @@ -94,24 +103,25 @@ your forward to the view, it is a good idea to check for errors: =head1 OTHER MAILERS -Now, it's no fun to just send out email using plain strings. We also have -L for use. You can also toggle this as being used -by setting up your configuration to look like this: +Now, it's no fun to just send out email using plain strings. We also +have L for use. You can also toggle +this as being used by setting up your configuration to look like this: View::Email: - default: TT + default_view: TT -Then, Catalyst::View::Email will forward to View::Email::TT by default. +Then, Catalyst::View::Email will forward to your View::TT by default. =cut sub new { - my ( $class ) = shift; - my $self = $class->next::method(@_); + my $self = shift->next::method(@_); + + my ( $c, $arguments ) = @_; my $mailer = Email::Send->new; - if ( my $method = $self->config->{sender}->{method} ) { + if ( my $method = $self->sender->{method} ) { croak "$method is not supported, see Email::Send" unless $mailer->mailer_available($method); $mailer->mailer($method); @@ -122,9 +132,15 @@ sub new { } } - if ( $mailer->mailer eq 'SMTP' ) { - my $host = $self->config->{sender}->{host} || 'localhost'; - $mailer->mailer_args([ Host => $host ]); + if ( my $args = $self->sender->{mailer_args} ) { + if ( ref $args eq 'HASH' ) { + $mailer->mailer_args([ %$args ]); + } + elsif ( ref $args eq 'ARRAY' ) { + $mailer->mailer_args($args); + } else { + croak "Invalid mailer_args specified, check pod for Email::Send!"; + } } $self->mailer($mailer); @@ -138,12 +154,12 @@ sub process { croak "Unable to send mail, bad mail configuration" unless $self->mailer; - my $email = $c->stash->{$self->config->{stash_key}}; + my $email = $c->stash->{$self->stash_key}; croak "Can't send email without a valid email structure" unless $email; - if ( $self->config->{content_type} ) { - $email->{content_type} ||= $self->config->{content_type}; + if ( $self->content_type ) { + $email->{content_type} ||= $self->content_type; } my $header = $email->{header} || []; @@ -174,7 +190,8 @@ sub process { my $message = Email::MIME->create(%mime); if ( $message ) { - $self->mailer->send($message); + my $return = $self->mailer->send($message); + croak "$return" if !$return; } else { croak "Unable to create message"; } @@ -192,6 +209,18 @@ sub process { J. Shirley +=head1 CONTRIBUTORS + +(Thanks!) + +Matt S Trout + +Daniel Westermann-Clark + +Simon Elliott - ::Template + +Roman Filippov + =head1 LICENSE This library is free software, you can redistribute it and/or modify it under