bumped version
[catagits/Catalyst-View-Email.git] / lib / Catalyst / View / Email.pm
index 5ace975..8b346fd 100644 (file)
@@ -8,7 +8,7 @@ use Email::Sender::Simple qw/ sendmail /;
 use Email::MIME::Creator;
 extends 'Catalyst::View';
 
-our $VERSION = '0.23';
+our $VERSION = '0.33';
 $VERSION = eval $VERSION;
 
 has 'mailer' => (
@@ -20,7 +20,7 @@ has 'mailer' => (
 
 has '_mailer_obj' => (
     is      => 'rw',
-    isa     => 'Email::Sender::Transport',
+    does    => 'Email::Sender::Transport',
     lazy    => 1,
     builder => '_build_mailer_obj',
 );
@@ -48,9 +48,9 @@ has 'sender' => (
 
 has 'content_type' => (
     is      => 'rw',
-       isa     => 'Str',
-       default => sub { shift->default->{content_type} },
-       lazy    => 1,
+    isa     => 'Str',
+    default => sub { shift->default->{content_type} },
+    lazy    => 1,
 );
 
 =head1 NAME
@@ -91,16 +91,16 @@ In your app configuration:
                 charset => 'utf-8'
             },
             # Setup how to send the email
-            # all those options are passed directly to Email::Send
+            # all those options are passed directly to Email::Sender::Simple
             sender => {
-                # if mailer doesn't start with Email::Sender::Transport::,
+                # if mailer doesn't start with Email::Sender::Simple::Transport::,
                 # then this is prepended.
                 mailer => 'SMTP',
-                # mailer_args is passed directly into Email::Send 
+                # mailer_args is passed directly into Email::Sender::Simple 
                 mailer_args => {
                     host     => 'smtp.example.com', # defaults to localhost
-                    username => 'username',
-                    password => 'password',
+                    sasl_username => 'sasl_username',
+                    sasl_password => 'sasl_password',
             }
           }
         }
@@ -124,7 +124,6 @@ Sending email is just filling the stash and forwarding to the view:
         $c->stash->{email} = {
             to      => 'jshirley@gmail.com',
             cc      => 'abraxxa@cpan.org',
-            bcc     => join ',', qw/hidden@secret.com hidden2@foobar.com/,
             from    => 'no-reply@foobar.com',
             subject => 'I am a Catalyst generated email',
             body    => 'Body Body Body',
@@ -162,6 +161,19 @@ contained ones.
         ],
     };
 
+You can set the envelope sender and recipient as well:
+
+  $c->stash->{email} = {
+
+    envelope_from => 'envelope-from@example.com',
+    from          => 'header-from@example.com',
+
+    envelope_to   => [ 'foo@example.com', 'bar@example.com' ],
+    to            => 'Undisclosed Recipients:;',
+
+    ...
+  };
+
 =head1 HANDLING ERRORS
 
 If the email fails to send, the view will die (throw an exception).
@@ -188,7 +200,7 @@ favourite template engine to render the mail body.
 
 =item new
 
-Validates the base config and creates the L<Email::Send> object for later use
+Validates the base config and creates the L<Email::Sender::Simple> object for later use
 by process.
 
 =cut
@@ -203,24 +215,24 @@ sub BUILD {
 }
 
 sub _build_mailer_obj {
-  my ($self) = @_;
-  my $transport_class = ucfirst $self->sender->{mailer};
+    my ($self) = @_;
+    my $transport_class = ucfirst $self->sender->{mailer};
 
-  # borrowed from Email::Sender::Simple -- apeiron, 2010-01-26 
-  if ($transport_class !~ /^Email::Sender::Transport::/) {
-    $transport_class = "Email::Sender::Transport::$transport_class";
-  }
+    # borrowed from Email::Sender::Simple -- apeiron, 2010-01-26
+    if ( $transport_class !~ /^Email::Sender::Transport::/ ) {
+        $transport_class = "Email::Sender::Transport::$transport_class";
+    }
 
-  Class::MOP::load_class($transport_class);
+    Class::MOP::load_class($transport_class);
 
-  return $transport_class->new($self->sender->{mailer_args} || {});
+    return $transport_class->new( $self->sender->{mailer_args} || {} );
 }
 
 =item process($c)
 
 The process method does the actual processing when the view is dispatched to.
 
-This method sets up the email parts and hands off to L<Email::Send> to handle
+This method sets up the email parts and hands off to L<Email::Sender::Simple> to handle
 the actual email delivery.
 
 =cut
@@ -245,8 +257,6 @@ sub process {
       if $email->{to};
     push @$header, ( 'Cc' => delete $email->{cc} )
       if $email->{cc};
-    push @$header, ( 'Bcc' => delete $email->{bcc} )
-      if $email->{bcc};
     push @$header, ( 'From' => delete $email->{from} )
       if $email->{from};
     push @$header,
@@ -280,10 +290,18 @@ sub process {
         $mime{attributes}->{charset} = $self->{default}->{charset};
     }
 
+    $mime{attributes}->{encoding} = $email->{encoding} 
+        if $email->{encoding};
+
     my $message = $self->generate_message( $c, \%mime );
 
     if ($message) {
-        my $return = sendmail( $message, { transport => $self->_mailer_obj } );
+        my $return = sendmail( $message,
+          {
+            exists $email->{envelope_from} ? ( from => $email->{envelope_from} ) : (),
+            exists $email->{envelope_to}   ? ( to   => $email->{envelope_to}   ) : (),
+            transport => $self->_mailer_obj,
+          } );
 
         # return is a Return::Value object, so this will stringify as the error
         # in the case of a failure.
@@ -307,7 +325,8 @@ sub setup_attributes {
     my ( $self, $c, $attrs ) = @_;
 
     my $default_content_type = $self->default->{content_type};
-    my $default_charset      = $self->default->{charset};
+    my $default_charset      = $self->default->{charset}; 
+    my $default_encoding     = $self->default->{encoding};
 
     my $e_m_attrs = {};
 
@@ -338,6 +357,24 @@ sub setup_attributes {
         $e_m_attrs->{charset} = $default_charset;
     }
 
+    if ( exists $attrs->{encoding}
+         && defined $attrs->{encoding}
+         && $attrs->{encoding} ne '' )
+    {
+        $c->log->debug(
+        'C::V::Email uses specified encoding '
+        . $attrs->{encoding}
+        . '.' )
+         if $c->debug;
+         $e_m_attrs->{encoding} = $attrs->{encoding};
+    }
+     elsif ( defined $default_encoding && $default_encoding ne '' ) {
+         $c->log->debug(
+         "C::V::Email uses default encoding $default_encoding.")
+         if $c->debug;
+         $e_m_attrs->{encoding} = $default_encoding;
+     }
+
     return $e_m_attrs;
 }
 
@@ -354,10 +391,8 @@ sub generate_message {
     my ( $self, $c, $attr ) = @_;
 
     # setup the attributes (merge with defaultis)
-       $attr->{attributes} = $self->setup_attributes($c, $attr->{attributes});
-    Email::MIME->create(
-           %$attr
-       );
+    $attr->{attributes} = $self->setup_attributes( $c, $attr->{attributes} );
+    Email::MIME->create( %$attr );
 }
 
 =back
@@ -368,7 +403,7 @@ sub generate_message {
 As with most things computer related, things break.  Email even more so.  
 Typically any errors are going to come from using SMTP as your sending method,
 which means that if you are having trouble the first place to look is at
-L<Email::Send::SMTP>.  This module is just a wrapper for L<Email::Send>,
+L<Email::Sender::Transport::SMTP>.  This module is just a wrapper for L<Email::Sender::Simple>,
 so if you get an error on sending, it is likely from there anyway.
 
 If you are using SMTP and have troubles sending, whether it is authentication