Revision history for Perl extension Catalyst::View::Email.
+0.06
+ - Fixing some slight issues with configuration not being handled
+ appropriately (thanks dwc and mst)
+0.05
+ - Better support for configuration
+ - Adding Mason tests
+
0.04
- Fixing MANIFEST, distribution
t/04basic.t
t/05template.t
t/06config.t
+t/07mason.t
t/lib/TestApp.pm
t/lib/TestApp/Controller/Root.pm
t/lib/TestApp/View/Email.pm
t/lib/TestApp/View/Email/AppConfig.pm
t/lib/TestApp/View/Email/Template.pm
+t/lib/TestApp/View/Mason.pm
t/lib/TestApp/View/TT.pm
+t/root/text_html/test.m
t/root/text_html/test.tt
+t/root/text_plain/test.m
t/root/text_plain/test.tt
requires 'Email::MIME' => '1.859';
requires 'Email::MIME::Creator' => '1.453';
-build_requires 'Catalyst::View::TT';
+feature 'Template Toolkit Support',
+ -default => 1,
+ 'Catalyst::View::TT' => 0;
+
+feature 'Mason Support',
+ -default => 0,
+ 'Catalyst::View::Mason';
auto_install;
WriteAll;
use base qw|Catalyst::View|;
-our $VERSION = '0.05';
+our $VERSION = '0.06';
__PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
use base qw|Catalyst::View::Email|;
-our $VERSION = '0.03';
+our $VERSION = '0.06';
+
+__PACKAGE__->mk_accessors( qw(default_view template_prefix) );
=head1 NAME
sub process {
my ( $self, $c ) = @_;
- my $stash_key = $self->config->{stash_key} || 'email';
+ my $stash_key = $self->stash_key || 'email';
croak "No template specified for rendering"
unless $c->stash->{$stash_key}->{template} or
$c->stash->{$stash_key}->{templates};
# Where to look
- my $template_prefix = $self->config->{template_prefix};
+ my $template_prefix = $self->template_prefix;
my @templates = ();
if ( $c->stash->{$stash_key}->{templates} && !ref $c->stash->{$stash_key}->{templates}[0]) {
$c->stash->{$stash_key}->{template});
}
- my $default_view = $c->view( $self->config->{default_view} );
+ my $default_view = $c->view( $self->default_view );
unless ( $default_view->can('render') ) {
croak "Email::Template's configured view does not have a render method!";
} else {
$content_type = 'text/plain';
}
- my $output = $default_view->render( $c, $template,
- { content_type => $content_type, %{$c->stash} });
+
+ my $output = $default_view->render( $c, $template, {
+ content_type => $content_type,
+ stash_key => $self->stash_key,
+ %{$c->stash},
+ });
+
# Got a ref, not a scalar. An error!
if ( ref $output ) {
croak $output->can("as_string") ? $output->as_string : $output;
}
}
- delete $c->stash->{email}->{body};
- $c->stash->{email}->{parts} ||= [];
- push @{$c->stash->{email}->{parts}}, @parts;
+ delete $c->stash->{$stash_key}->{body};
+ $c->stash->{$stash_key}->{parts} ||= [];
+ push @{$c->stash->{$stash_key}->{parts}}, @parts;
# Let C::V::Email do the actual sending. We just assemble the tasty bits.
return $self->next::method($c);
use strict;
use warnings;
-use Test::More tests => 10;
+use Test::More;
use Email::Send::Test;
use FindBin;
use lib "$FindBin::Bin/lib";
+eval "use Catalyst::View::TT";
+if ( $@ ) {
+ plan skip_all => 'Catalyst::View::TT required for Template tests';
+ exit;
+}
+plan tests => 10;
+
use_ok('Catalyst::Test', 'TestApp');
my $response;
use strict;
use warnings;
-use Test::More tests => 5;
+use Test::More tests => 13;
use Email::Send::Test;
use FindBin;
use_ok('Catalyst::Test', 'TestApp');
my $response;
-my $time = time;
+my $time;
+my @emails;
+
+$time = time;
+
ok( ($response = request("/email_app_config?time=$time"))->is_success, 'request ok');
-my @emails = Email::Send::Test->emails;
+@emails = Email::Send::Test->emails;
is(@emails, 1, "got emails");
isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
like($emails[0]->body, qr/$time/, 'Got our email');
+
+Email::Send::Test->clear;
+
+$time = time;
+ok( ($response = request("/template_email_app_config?time=$time"))->is_success, 'request ok');
+
+@emails = Email::Send::Test->emails;
+
+is(@emails, 1, "got emails");
+isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
+my @parts = $emails[0]->parts;
+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{<em>test-email\@example.com</em> on $time}, 'got content back');
+
--- /dev/null
+use strict;
+use warnings;
+use Test::More;
+
+use Email::Send::Test;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+eval "use Catalyst::View::Mason";
+if ( $@ ) {
+ plan skip_all => 'Catalyst::View::Mason required for Mason tests';
+ exit;
+}
+plan tests => 10;
+
+use_ok('Catalyst::Test', 'TestApp');
+
+TestApp->config->{default_view} = 'mason';
+
+my $response;
+my $time = time;
+ok( ( $response = request("/mason_email?time=$time"))->is_success,
+ 'request ok' );
+like( $response->content, qr/Mason Email Ok/, 'controller says ok' );
+my @emails = Email::Send::Test->emails;
+
+cmp_ok(@emails, '==', 1, 'got emails');
+isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
+my @parts = $emails[0]->parts;
+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{<em>test-email\@example.com</em> on $time}, 'got content back');
+#like($emails[0]->body, qr/$time/, 'Got our email');
+
method => 'Test',
},
},
+ 'View::Email::Template::AppConfig' => {
+ stash_key => 'template_email',
+ sender => {
+ method => 'Test',
+ },
+ },
);
TestApp->setup;
}
}
+sub template_email_app_config : Global('template_email_app_config') {
+ my ($self, $c, @args) = @_;
+
+ $c->stash->{time} = $c->req->params->{time} || time;
+
+ $c->stash->{template_email} = {
+ to => 'test-email@example.com',
+ from => 'no-reply@example.com',
+ subject => 'Just a test',
+ content_type => 'multipart/alternative',
+ templates => [
+ qw{text_plain/test.tt},
+ qw{text_html/test.tt}
+ ]
+ };
+
+ $c->forward('TestApp::View::Email::Template::AppConfig');
+
+ if ( scalar( @{ $c->error } ) ) {
+ $c->res->status(500);
+ $c->res->body('Template Email Failed');
+ } else {
+ $c->res->body('Template Email Ok');
+ }
+}
+
+sub mason_email : Global('mason_email') {
+ my ($self, $c, @args) = @_;
+
+ $c->stash->{time} = $c->req->params->{time} || time;
+
+ $c->stash->{email} = {
+ to => 'test-email@example.com',
+ from => 'no-reply@example.com',
+ subject => 'Just a test',
+ content_type => 'multipart/alternative',
+ templates => [
+ qw{text_plain/test.m},
+ qw{text_html/test.m}
+ ]
+ };
+
+ $c->forward('TestApp::View::Email::Template');
+
+ if ( scalar( @{ $c->error } ) ) {
+ $c->res->status(500);
+ $c->res->body('Mason Email Failed');
+ } else {
+ $c->res->body('Mason Email Ok');
+ }
+}
+
+
1;
--- /dev/null
+package # Hide me.
+ TestApp::View::Mason;
+
+use strict;
+eval "use base 'Catalyst::View::Mason';";
+
+1;
TestApp::View::TT;
use strict;
-use base 'Catalyst::View::TT';
+eval "use base 'Catalyst::View::TT';";
1;
<html>
<body>
<h1>Look at my style</h1>
- <p>I was sent to <em>[% email.to %]</em> on [% time %]</p>
+ <p>I was sent to <em>[% $stash_key.to %]</em> on [% time %]</p>
</body>
</html>
I am plain text. I have no style.
-I was sent to [% email.to %] on [% time %]
+I was sent to [% $stash_key.to %] on [% time %]