1 package Catalyst::View::Email::Template;
9 use Email::MIME::Creator;
11 use base qw|Catalyst::View::Email|;
13 our $VERSION = '0.02';
17 Catalyst::View::Email::Template - Send Templated Email from Catalyst
21 Sends Templated mail, based upon your Default View. Will capture the output
22 of the rendering path, slurps in based on mime-types and assembles a multi-part
23 email and sends it out.
27 View::Email::Template:
28 # Set it up so if you have multiple parts, they're alternatives.
29 # This is on the top-level message, not the individual parts.
30 content_type: multipart/alternative
31 # Optional prefix to look somewhere under the existing configured
33 template_prefix: email
34 # Where to look in the stash for the email information
36 # Setup how to send the email
45 Sending email is just setting up your stash key, and forwarding to the view.
47 $c->stash->{email} = {
48 to => 'jshirley@gmail.com',
49 from => 'no-reply@foobar.com',
50 subject => 'I am a Catalyst generated email',
51 # Specify which templates to include
53 qw{text_plain/test.tt},
57 $c->forward('View::Email::Template');
59 Alternatively if you want more control over your templates you can use the following idiom :-
63 template => 'email/test.html.tt',
64 content_type => 'text/html'
67 template => 'email/test.plain.tt',
68 content_type => 'text/plain'
74 If it fails $c->error will have the error message.
79 template_prefix => '',
83 # This view hitches into your default view and will call the render function
84 # on the templates provided. This means that you have a layer of abstraction
85 # and you aren't required to modify your templates based on your desired engine
86 # (Template Toolkit or Mason, for example). As long as the view adequately
87 # supports ->render, all things are good. Mason, and others, are not good.
90 # The path here is to check configuration for the template root, and then
91 # proceed to call render on the subsequent templates and stuff each one
92 # into an Email::MIME container. The mime-type will be stupidly guessed with
93 # the subdir on the template.
95 # TODO: Make this unretarded.
98 my ( $self, $c ) = @_;
100 my $stash_key = $self->config->{stash_key} || 'email';
102 croak "No template specified for rendering"
103 unless $c->stash->{$stash_key}->{template} or
104 $c->stash->{$stash_key}->{templates};
106 my $template_prefix = $self->config->{template_prefix};
109 if ( $c->stash->{$stash_key}->{templates} && !ref $c->stash->{$stash_key}->{templates}[0]) {
110 push @templates, map {
111 join('/', $template_prefix, $_);
112 } @{$c->stash->{$stash_key}->{templates}};
114 } elsif($c->stash->{$stash_key}->{template}) {
115 push @templates, join('/', $template_prefix,
116 $c->stash->{$stash_key}->{template});
119 my $default_view = $c->view( $self->config->{default_view} );
121 unless ( $default_view->can('render') ) {
122 croak "Email::Template's configured view does not have a render method!";
125 #$c->log->_dump($default_view->config);
128 foreach my $template ( @templates ) {
129 $template =~ s#^/+##; # Make sure that we don't have an absolute path.
130 # This seems really stupid to me... argh. will give me nightmares!
131 my $template_path = $template;
132 $template_path =~ s#^$template_prefix/##;
133 my ( $content_type, $extra ) = split('/', $template_path);
135 $content_type ||= 'text/plain';
136 $content_type =~ s#_#/#;
138 $content_type = 'text/plain';
140 my $output = $default_view->render( $c, $template,
141 { content_type => $content_type, %{$c->stash} });
142 # Got a ref, not a scalar. An error!
144 croak $output->can("as_string") ? $output->as_string : $output;
146 push @parts, Email::MIME->create(
148 content_type => $content_type
155 if ( $c->stash->{$stash_key}->{'templates'} && ref $c->stash->{$stash_key}->{templates}[0] ) {
156 foreach my $part (@{$c->stash->{$stash_key}->{'templates'}}) {
157 my $view = $c->view($part->{'view'} || $self->config->{default_view});
159 my $content_type = $part->{'content_type'} || 'text/plain';
160 unless ( $view->can('render') ) {
161 croak "Part does not have valid render view";
164 my $output = $view->render( $c, $part->{'template'}, {
165 'content_type' => $content_type,
169 croak $output->can("as_string") ? $output->as_string : $output;
172 push @parts, Email::MIME->create(
174 content_type => $content_type
181 delete $c->stash->{email}->{body};
182 $c->stash->{email}->{parts} ||= [];
183 push @{$c->stash->{email}->{parts}}, @parts;
185 # Let C::V::Email do the actual sending. We just assemble the tasty bits.
186 return $self->next::method($c);
193 There needs to be a method to support attachments. What I am thinking is
194 something along these lines:
197 # Set the body to a file handle object, specify content_type and
198 # the file name. (name is what it is sent at, not the file)
199 { body => $fh, name => "foo.pdf", content_type => "application/pdf" },
200 # Or, specify a filename that is added, and hey, encoding!
201 { filename => "foo.gif", name => "foo.gif", content_type => "application/pdf", encoding => "quoted-printable" },
202 # Or, just a path to a file, and do some guesswork for the content type
203 "/path/to/somefile.pdf",
208 =head2 L<Catalyst::View::Email> - Send plain boring emails with Catalyst
210 =head2 L<Catalyst::Manual> - The Catalyst Manual
212 =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook
216 J. Shirley <jshirley@gmail.com>
218 Simon Elliott <cpan@browsing.co.uk>
222 This library is free software, you can redistribute it and/or modify it under
223 the same terms as Perl itself.