Croaking on errors, need to write tests. Adding Net::SMTP feature in Makefile so...
[catagits/Catalyst-View-Email.git] / lib / Catalyst / View / Email.pm
index fe9afca..fba2330 100644 (file)
@@ -11,9 +11,9 @@ use Email::MIME::Creator;
 
 use base qw|Catalyst::View|;
 
-our $VERSION = '0.02';
+our $VERSION = '0.07';
 
-__PACKAGE__->mk_accessors('mailer');
+__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
 
 =head1 NAME
 
@@ -35,9 +35,15 @@ In your app configuration (example in L<YAML>):
             method:     SMTP
             # mailer_args is passed directly into Email::Send 
             mailer_args:
-                - Host:       smtp.example.com
-                - username:   username
-                - password:   password
+                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
 
@@ -112,11 +118,10 @@ sub new {
     my $self = shift->next::method(@_);
 
     my ( $c, $arguments ) = @_;
-    $self->config($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);
@@ -127,8 +132,15 @@ sub new {
         }
     }
 
-    if ( $self->config->{sender}->{mailer_args} ) {
-        $mailer->mailer_args($self->config->{sender}->{mailer_args});
+    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);
@@ -142,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} || [];
@@ -178,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";
     }
@@ -200,8 +213,14 @@ J. Shirley <jshirley@gmail.com>
 
 (Thanks!)
 
+Matt S Trout
+
 Daniel Westermann-Clark
 
+Simon Elliott <cpan@browsing.co.uk> - ::Template
+
+Roman Filippov
+
 =head1 LICENSE
 
 This library is free software, you can redistribute it and/or modify it under