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 ac41fbc..fba2330 100644 (file)
@@ -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<YAML>):
+
     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<stash_key>
     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<Catalyst::View::Email::TT> 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<Catalyst::View::Email::Template> 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 <jshirley@gmail.com>
 
+=head1 CONTRIBUTORS
+
+(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