[Catalyst-commits] r7041 - in Catalyst-View-Email/0.07: . lib/Catalyst/Helper/View lib/Catalyst/Helper/View/Email lib/Catalyst/View lib/Catalyst/View/Email t/lib t/lib/TestApp/Controller

abraxxa at dev.catalyst.perl.org abraxxa at dev.catalyst.perl.org
Mon Oct 22 17:52:54 GMT 2007


Author: abraxxa
Date: 2007-10-22 17:52:53 +0100 (Mon, 22 Oct 2007)
New Revision: 7041

Modified:
   Catalyst-View-Email/0.07/Changes
   Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email.pm
   Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email/Template.pm
   Catalyst-View-Email/0.07/lib/Catalyst/View/Email.pm
   Catalyst-View-Email/0.07/lib/Catalyst/View/Email/Template.pm
   Catalyst-View-Email/0.07/t/lib/TestApp.pm
   Catalyst-View-Email/0.07/t/lib/TestApp/Controller/Root.pm
Log:
- added cc and bcc attributes to simple api
- fallback to Catalysts default view if none is configured and passed to complex api
- bumped version to 0.09999_01 (first developer release)


Modified: Catalyst-View-Email/0.07/Changes
===================================================================
--- Catalyst-View-Email/0.07/Changes	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/Changes	2007-10-22 16:52:53 UTC (rev 7041)
@@ -1,6 +1,7 @@
 Revision history for Perl extension Catalyst::View::Email.
 
-0.07    - refactored by Alexander Hartmaier with slight api changes
+0.10
+        - Refactored by Alexander Hartmaier with api changes
 
 0.06
         - Fixing some slight issues with configuration not being handled

Modified: Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email/Template.pm
===================================================================
--- Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email/Template.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email/Template.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -8,7 +8,7 @@
 
 =head1 SYNOPSIS
 
-    script/create.pl view Email::Template Email::Template
+    $ script/myapp_create.pl view Email::Template Email::Template
 
 =head1 DESCRIPTION
 

Modified: Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email.pm
===================================================================
--- Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/lib/Catalyst/Helper/View/Email.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -8,7 +8,7 @@
 
 =head1 SYNOPSIS
 
-    script/create.pl view Email Email
+    $ script/myapp_create.pl view Email Email
 
 =head1 DESCRIPTION
 

Modified: Catalyst-View-Email/0.07/lib/Catalyst/View/Email/Template.pm
===================================================================
--- Catalyst-View-Email/0.07/lib/Catalyst/View/Email/Template.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/lib/Catalyst/View/Email/Template.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -5,13 +5,13 @@
 
 use Class::C3;
 use Carp;
-use Scalar::Util qw( blessed );
+use Scalar::Util qw/ blessed /;
 
 use Email::MIME::Creator;
 
-use base qw|Catalyst::View::Email|;
+use base qw/ Catalyst::View::Email /;
 
-our $VERSION = '0.07';
+our $VERSION = '0.09999_01';
 
 =head1 NAME
 
@@ -19,35 +19,48 @@
 
 =head1 SYNOPSIS
 
-Sends Templated mail, based upon your Default View.  Will capture the output
+Sends Templated mail, based upon your default view. It captures the output
 of the rendering path, slurps in based on mime-types and assembles a multi-part
-email and sends it out.
-It uses Email::MIME to create the mail.
+email using Email::MIME::Creator and sends it out.
 
-=head2 CONFIGURATION
+=head1 CONFIGURATION
 
+Use the helper to create your View:
+    
+    $ script/myapp_create.pl view Email::Template Email::Template
+
+In your app configuration (example in L<YAML>):
+
     View::Email::Template:
         # Optional prefix to look somewhere under the existing configured
-        # template  paths. Default is none.
+        # template  paths.
+        # Default: none
         template_prefix: email
         # Where to look in the stash for the email information.
-        # 'email' is the default, so you don't have to specify it.
+        # Default: email
         stash_key: email
         # Define the defaults for the mail
         default:
-            # Defines the default content type (mime type) for every template.
+            # Defines the default content type (mime type).
+            # Mandatory
             content_type: text/html
             # Defines the default charset for every MIME part with the content
-            # type 'text'.
-            # According to RFC2049 such a MIME part without a charset should
+            # 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
-            # Defines the default view which is used to render the templates.
+            # Defines the default view used to render the templates.
+            # If none is specified neither here nor in the stash
+            # Catalysts default view is used.
+            # Warning: if you don't tell Catalyst explicit which of your views should
+            # be its default one, C::V::Email::Template may choose the wrong one!
             view: TT
         # Setup how to send the email
-        # all those options are passed directly to Email::Send
+        # All those options are passed directly to Email::Send,
+        # for all available options look at its docs.
         sender:
             mailer: SMTP
             mailer_args:
@@ -74,11 +87,13 @@
         {
             template        => 'email/test.html.tt',
             content_type    => 'text/html',
+            charset         => 'utf-8',
             view            => 'TT', 
         },
         {
             template        => 'email/test.plain.mason',
             content_type    => 'text/plain',
+            charset         => 'utf-8',
             view            => 'Mason', 
         }
     ]
@@ -132,21 +147,51 @@
     my $template_prefix         = $self->{template_prefix};
     my $default_view            = $self->{default}->{view};
     my $default_content_type    = $self->{default}->{content_type};
+    my $default_charset         = $self->{default}->{charset};
 
-    my $view = $c->view($attrs->{view} || $default_view);
+    my $e_m_attrs = {};
+
+    my $view;
+    # use the view specified for the email part
+    if (exists $attrs->{view} && defined $attrs->{view} && $attrs->{view} ne '') {
+        $view = $c->view($attrs->{view});
+        $c->log->debug("C::V::Email::Template uses specified view $view for rendering.") if $c->debug;
+    }
+    # if none specified use the configured default view
+    elsif ($default_view) {
+        $view = $c->view($default_view);
+        $c->log->debug("C::V::Email::Template uses default view $view for rendering.") if $c->debug;;
+    }
+    # else fallback to Catalysts default view
+    else {
+        $view = $c->view;
+        $c->log->debug("C::V::Email::Template uses back to catalysts default view $view for rendering.") if $c->debug;;
+    }
+
     # validate the per template view
     $self->_validate_view($view);
-#    $c->log->debug("VIEW: $view");
     
     # prefix with template_prefix if configured
     my $template = $template_prefix ne '' ? join('/', $template_prefix, $attrs->{template}) : $attrs->{template};
-            
-    my $content_type = $attrs->{content_type} || $default_content_type;
     
+    if (exists $attrs->{content_type} && defined $attrs->{content_type} && $attrs->{content_type} ne '') {
+        $e_m_attrs->{content_type} = $attrs->{content_type};
+    }
+    elsif (defined $default_content_type && $default_content_type ne '') {
+        $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;
+    }
+
     # render the email part
     my $output = $view->render( $c, $template, { 
-        content_type    => "$content_type",
-        stash_key       => $self->stash_key,
+        content_type    => $e_m_attrs->{content_type},
+        stash_key       => $self->{stash_key},
 	%{$c->stash},
     });
 				
@@ -155,9 +200,7 @@
     }
 	
     return Email::MIME->create(
-        attributes => {
-	    content_type => "$content_type",
-	},
+        attributes => $e_m_attrs,
 	body => $output,
     );
 }
@@ -165,21 +208,13 @@
 sub process {
     my ( $self, $c ) = @_;
 
-    # validate here so _generate_part doesn't have to do it for every part
-    my $stash_key = $self->{stash_key};
-    croak "Email::Template's stash_key isn't defined!"
-        if ($stash_key eq '');
-    
     # don't validate template_prefix
 
-    # the default view is validated on use later anyways...
-    # but just to be sure even if not used
-#    $self->_validate_view($c->view($self->{default}->{view}));
+    # the default view is validated if used
 
-#    $c->log->debug("SELF: $self");
-#    $c->log->debug('DEFAULT VIEW: ' . $self->{default}->{view});
-
     # the content type should be validated by Email::MIME::Creator
+    
+    my $stash_key = $self->{stash_key};
 
     croak "No template specified for rendering"
         unless $c->stash->{$stash_key}->{template}
@@ -202,6 +237,7 @@
                 view            => $part->{view},
                 template        => $part->{template},
                 content_type    => $part->{content_type},
+                charset         => $part->{charset},
             });
 	}
     }

Modified: Catalyst-View-Email/0.07/lib/Catalyst/View/Email.pm
===================================================================
--- Catalyst-View-Email/0.07/lib/Catalyst/View/Email.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/lib/Catalyst/View/Email.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -9,12 +9,11 @@
 use Email::Send;
 use Email::MIME::Creator;
 
-use base qw|Catalyst::View|;
+use base qw/ Catalyst::View /;
 
-our $VERSION = '0.07';
+our $VERSION = '0.09999_01';
 
-#__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
-__PACKAGE__->mk_accessors(qw(stash_key content_type mailer));
+__PACKAGE__->mk_accessors(qw/ mailer /);
 
 =head1 NAME
 
@@ -27,11 +26,23 @@
 
 =head1 CONFIGURATION
 
+Use the helper to create your View:
+    
+    $ script/myapp_create.pl view Email Email
+
 In your app configuration (example in L<YAML>):
 
     View::Email:
+        # Where to look in the stash for the email information.
+        # 'email' is the default, so you don't have to specify it.
         stash_key: email
-        content_type: text/plain 
+        # Define the defaults for the mail
+        default:
+            # Defines the default content type (mime type).
+            # mandatory
+            content_type: text/plain
+        # Setup how to send the email
+        # all those options are passed directly to Email::Send
         sender:
             mailer: SMTP
             # mailer_args is passed directly into Email::Send 
@@ -63,6 +74,8 @@
         my ( $self, $c ) = @_;
         $c->stash->{email} = {
             to      => q{catalyst at rocksyoursocks.com},
+            cc      => q{foo at bar.com},
+            bcc     => q{hidden at secret.com},
             from    => q{no-reply at socksthatarerocked.com},
             subject => qq{Your Subject Here},
             body    => qq{Body Body Body}
@@ -105,24 +118,23 @@
         $c->res->body('Email sent A-OK! (At least as far as we can tell)');
     }
 
-=head1 OTHER MAILERS
+=head1 USING TEMPLATES FOR EMAIL
 
-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:
+Now, it's no fun to just send out email using plain strings.
+Take a look at L<Catalyst::View::Email::Template> to see how you can use your
+favourite template engine to render the mail body.
 
-    View::Email:
-        default:
-            view: TT
 
-Then, Catalyst::View::Email will forward to your View::TT by default.
-
 =cut
 
 sub new {
     my $self = shift->next::method(@_);
 
     my ( $c, $arguments ) = @_;
+    
+    my $stash_key = $self->{stash_key};
+    croak "$self stash_key isn't defined!"
+        if ($stash_key eq '');
 
     my $sender = Email::Send->new;
 
@@ -159,17 +171,21 @@
     croak "Unable to send mail, bad mail configuration"
         unless $self->mailer;
 
-    my $email  = $c->stash->{$self->stash_key};
+    my $email  = $c->stash->{$self->{stash_key}};
     croak "Can't send email without a valid email structure"
         unless $email;
     
-    if ( $self->content_type ) {
-        $email->{content_type} ||= $self->content_type;
+    if ( exists $self->{content_type} ) {
+        $email->{content_type} ||= $self->{content_type};
     }
 
     my $header  = $email->{header} || [];
         push @$header, ('To' => delete $email->{to})
             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, ('Subject' => delete $email->{subject})
@@ -222,10 +238,12 @@
 
 Daniel Westermann-Clark
 
-Simon Elliott <cpan at browsing.co.uk> - ::Template
+Simon Elliott <cpan at browsing.co.uk>
 
 Roman Filippov
 
+Alexander Hartmaier <alex_hartmaier at hotmail.com>
+
 =head1 LICENSE
 
 This library is free software, you can redistribute it and/or modify it under

Modified: Catalyst-View-Email/0.07/t/lib/TestApp/Controller/Root.pm
===================================================================
--- Catalyst-View-Email/0.07/t/lib/TestApp/Controller/Root.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/t/lib/TestApp/Controller/Root.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -62,10 +62,8 @@
         to      => 'test-email at example.com',
         from    => 'no-reply at example.com',
         subject => 'Just a test',
-#        content_type => 'multipart/alternative',
         templates => [
             {
-                view            => 'TT',
                 template        => 'text_plain/test.tt',
                 content_type    => 'text/plain',
             },
@@ -96,13 +94,13 @@
         to      => 'test-email at example.com',
         from    => 'no-reply at example.com',
         subject => 'Just a test',
-#        content_type => 'multipart/alternative',
         templates => [
             {
                 template        => 'text_plain/test.tt',
                 content_type    => 'text/plain',
             },
             {
+                view            => 'TT',
                 template        => 'text_html/test.tt',
                 content_type    => 'text/html',
             },
@@ -128,13 +126,13 @@
         to      => 'test-email at example.com',
         from    => 'no-reply at example.com',
         subject => 'Just a test',
-        content_type => 'multipart/alternative',
         templates => [
             {
                 template        => 'text_plain/test.tt',
                 content_type    => 'text/plain',
             },
             {
+                view            => 'Mason',
                 template        => 'text_html/test.tt',
                 content_type    => 'text/html',
             },

Modified: Catalyst-View-Email/0.07/t/lib/TestApp.pm
===================================================================
--- Catalyst-View-Email/0.07/t/lib/TestApp.pm	2007-10-22 06:14:52 UTC (rev 7040)
+++ Catalyst-View-Email/0.07/t/lib/TestApp.pm	2007-10-22 16:52:53 UTC (rev 7041)
@@ -5,7 +5,8 @@
 use FindBin;
 
 TestApp->config(
-    root => "$FindBin::Bin/root",
+    root            => "$FindBin::Bin/root",
+    default_view    => 'TT',
     'View::Email::AppConfig' => {
         sender => {
             mailer => 'Test',




More information about the Catalyst-commits mailing list