added setup_attributes to Email.pm and use it in Template.pm
[catagits/Catalyst-View-Email.git] / lib / Catalyst / View / Email.pm
index 773d343..bb304cf 100644 (file)
@@ -11,7 +11,7 @@ use Email::MIME::Creator;
 
 use base qw/ Catalyst::View /;
 
-our $VERSION = '0.09999_01';
+our $VERSION = '0.09999_02';
 
 __PACKAGE__->mk_accessors(qw/ mailer /);
 
@@ -41,6 +41,14 @@ In your app configuration (example in L<YAML>):
             # Defines the default content type (mime type).
             # mandatory
             content_type: text/plain
+            # Defines the default charset for every MIME part with the content
+            # type text.
+            # According to RFC2049 a MIME part without a charset should
+            # be treated as US-ASCII by the mail client.
+            # If the charset is not set it won't be set for all MIME parts
+            # without an overridden one.
+            # Default: none
+            charset: utf-8
         # Setup how to send the email
         # all those options are passed directly to Email::Send
         sender:
@@ -174,7 +182,7 @@ sub process {
     my $email  = $c->stash->{$self->{stash_key}};
     croak "Can't send email without a valid email structure"
         unless $email;
-    
+
     if ( exists $self->{content_type} ) {
         $email->{content_type} ||= $self->{content_type};
     }
@@ -207,8 +215,16 @@ sub process {
     } else {
         $mime{body} = $body;
     }
+    
+    if ( $mime{attributes} and not $mime{attributes}->{charset} and
+         $self->{default}->{charset} )
+    {
+        $mime{attributes}->{charset} = $self->{default}->{charset};
+    }
+
+    my $message = $self->generate_message( $c, \%mime );
 
-    my $message = Email::MIME->create(%mime);
+    #my $message = Email::MIME->create(%mime);
 
     if ( $message ) {
         my $return = $self->mailer->send($message);
@@ -218,6 +234,41 @@ sub process {
     }
 }
 
+sub setup_attributes {
+    my ( $self, $c, $attrs ) = @_;
+    
+    my $default_content_type    = $self->{default}->{content_type};
+    my $default_charset         = $self->{default}->{charset};
+
+    my $e_m_attrs = {};
+
+    if (exists $attrs->{content_type} && defined $attrs->{content_type} && $attrs->{content_type} ne '') {
+        $c->log->debug('C::V::Email uses specified content_type ' . $attrs->{content_type} . '.') if $c->debug;
+        $e_m_attrs->{content_type} = $attrs->{content_type};
+    }
+    elsif (defined $default_content_type && $default_content_type ne '') {
+        $c->log->debug("C::V::Email uses default content_type $default_content_type.") if $c->debug;
+        $e_m_attrs->{content_type} = $default_content_type;
+    }
+   
+    if (exists $attrs->{charset} && defined $attrs->{charset} && $attrs->{charset} ne '') {
+        $e_m_attrs->{charset} = $attrs->{charset};
+    }
+    elsif (defined $default_charset && $default_charset ne '') {
+        $e_m_attrs->{charset} = $default_charset;
+    }
+
+    return $e_m_attrs;
+}
+
+sub generate_message {
+    my ( $self, $c, $attr ) = @_;
+
+    # setup the attributes (merge with defaults)
+    $attr->{attributes} = $self->setup_attributes($c, $attr->{attributes});
+    return Email::MIME->create(%$attr);
+}
+
 =head1 SEE ALSO
 
 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat