# 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:
} 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);
}
}
+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<Catalyst::View::Email::Template> - Send fancy template emails with Cat
unless ($view->can('render'));
}
-sub _generate_part {
+sub generate_part {
my ($self, $c, $attrs) = @_;
my $template_prefix = $self->{template_prefix};
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 '') {
# 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,
);
}
&& 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},
});
}
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');