From: J. Shirley Date: Tue, 30 Oct 2007 16:07:41 +0000 (+0000) Subject: Minor refactor of the way generate_part works, having the default attribute merging... X-Git-Tag: v0.14~19 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=43090696b9cc6002e258a76a3f9004daa48631fe;p=catagits%2FCatalyst-View-Email.git Minor refactor of the way generate_part works, having the default attribute merging in a separate method in ::Email. --- diff --git a/lib/Catalyst/View/Email.pm b/lib/Catalyst/View/Email.pm index 773d343..e274daa 100644 --- a/lib/Catalyst/View/Email.pm +++ b/lib/Catalyst/View/Email.pm @@ -41,6 +41,14 @@ In your app configuration (example in L): # 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: @@ -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_part( \%mime ); - my $message = Email::MIME->create(%mime); + #my $message = Email::MIME->create(%mime); if ( $message ) { my $return = $self->mailer->send($message); @@ -218,6 +234,39 @@ 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 '') { + $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; + } + + return $e_m_attrs; +} + +sub generate_part { + my ( $self, $attr ) = @_; + + # setup the attributes (merge with defaults) + $attr->{attributes} = $self->setup_attributes($attr->{attributes}); + return Email::MIME->create(%$attr); +} + =head1 SEE ALSO =head2 L - Send fancy template emails with Cat diff --git a/lib/Catalyst/View/Email/Template.pm b/lib/Catalyst/View/Email/Template.pm index fa5faee..3ab0e54 100644 --- a/lib/Catalyst/View/Email/Template.pm +++ b/lib/Catalyst/View/Email/Template.pm @@ -141,7 +141,7 @@ sub _validate_view { unless ($view->can('render')); } -sub _generate_part { +sub generate_part { my ($self, $c, $attrs) = @_; my $template_prefix = $self->{template_prefix}; @@ -149,8 +149,6 @@ sub _generate_part { my $default_content_type = $self->{default}->{content_type}; my $default_charset = $self->{default}->{charset}; - my $e_m_attrs = {}; - my $view; # use the view specified for the email part if (exists $attrs->{view} && defined $attrs->{view} && $attrs->{view} ne '') { @@ -173,35 +171,24 @@ sub _generate_part { # prefix with template_prefix if configured my $template = $template_prefix ne '' ? join('/', $template_prefix, $attrs->{template}) : $attrs->{template}; - - 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; - } + # setup the attributes (merge with defaults) + my $e_m_attrs = $self->setup_attributes($attrs); # render the email part my $output = $view->render( $c, $template, { content_type => $e_m_attrs->{content_type}, stash_key => $self->{stash_key}, - %{$c->stash}, + %{$c->stash}, }); - - if (ref $output) { - croak $output->can('as_string') ? $output->as_string : $output; + + if ( ref $output ) { + croak $output->can('as_string') ? $output->as_string : $output; } - + return Email::MIME->create( attributes => $e_m_attrs, - body => $output, + body => $output, ); } @@ -233,17 +220,17 @@ sub process { && ref $c->stash->{$stash_key}->{templates}[0] eq 'HASH') { # loop through all parts of the mail foreach my $part (@{$c->stash->{$stash_key}->{templates}}) { - push @parts, $self->_generate_part($c, { + push @parts, $self->generate_part($c, { view => $part->{view}, template => $part->{template}, content_type => $part->{content_type}, charset => $part->{charset}, }); - } + } } # single part api elsif($c->stash->{$stash_key}->{template}) { - push @parts, $self->_generate_part($c, { + push @parts, $self->generate_part($c, { template => $c->stash->{$stash_key}->{template}, }); } diff --git a/t/05template.t b/t/05template.t index b8c6936..ca488e4 100644 --- a/t/05template.t +++ b/t/05template.t @@ -29,6 +29,7 @@ cmp_ok(@parts, '==', 2, 'got parts'); is($parts[0]->content_type, 'text/plain; charset="us-ascii"', 'text/plain ok'); like($parts[0]->body, qr/test-email\@example.com on $time/, 'got content back'); + is($parts[1]->content_type, 'text/html; charset="us-ascii"', 'text/html ok'); like($parts[1]->body, qr{test-email\@example.com on $time}, 'got content back'); #like($emails[0]->body, qr/$time/, 'Got our email'); diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index ac9cee6..4e95693 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -126,6 +126,7 @@ sub mason_email : Global('mason_email') { to => 'test-email@example.com', from => 'no-reply@example.com', subject => 'Just a test', + view => 'Mason', templates => [ { template => 'text_plain/test.m',