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
=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
my ( $self, $c ) = @_;
$c->stash->{email} = {
to => q{catalyst@rocksyoursocks.com},
+ cc => q{foo@bar.com},
+ bcc => q{hidden@secret.com},
from => q{no-reply@socksthatarerocked.com},
subject => qq{Your Subject Here},
body => qq{Body Body Body}
$c->res->body('Email sent A-OK! (At least as far as we can tell)');
}
-=head1 OTHER MAILERS
-
-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:
+=head1 USING TEMPLATES FOR EMAIL
- View::Email:
- default:
- view: TT
+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.
-Then, Catalyst::View::Email will forward to your View::TT by default.
=cut
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;
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})
Daniel Westermann-Clark
-Simon Elliott <cpan@browsing.co.uk> - ::Template
+Simon Elliott <cpan@browsing.co.uk>
Roman Filippov
+Alexander Hartmaier <alex_hartmaier@hotmail.com>
+
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
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
=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:
{
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',
}
]
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 $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;;
+ }
- my $view = $c->view($attrs->{view} || $default_view);
# 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},
});
}
return Email::MIME->create(
- attributes => {
- content_type => "$content_type",
- },
+ attributes => $e_m_attrs,
body => $output,
);
}
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}));
-
-# $c->log->debug("SELF: $self");
-# $c->log->debug('DEFAULT VIEW: ' . $self->{default}->{view});
+ # the default view is validated if used
# 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}
view => $part->{view},
template => $part->{template},
content_type => $part->{content_type},
+ charset => $part->{charset},
});
}
}
to => 'test-email@example.com',
from => 'no-reply@example.com',
subject => 'Just a test',
-# content_type => 'multipart/alternative',
templates => [
{
- view => 'TT',
template => 'text_plain/test.tt',
content_type => 'text/plain',
},
to => 'test-email@example.com',
from => 'no-reply@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',
},
to => 'test-email@example.com',
from => 'no-reply@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',
},