Commit | Line | Data |
529915ab |
1 | package Catalyst::View::Email::Template; |
2 | |
3 | use warnings; |
4 | use strict; |
5 | |
6 | use Class::C3; |
7 | use Carp; |
ea115f9b |
8 | use Scalar::Util qw/ blessed /; |
529915ab |
9 | |
10 | use Email::MIME::Creator; |
11 | |
ea115f9b |
12 | use base qw/ Catalyst::View::Email /; |
529915ab |
13 | |
ea115f9b |
14 | our $VERSION = '0.09999_01'; |
529915ab |
15 | |
16 | =head1 NAME |
17 | |
18 | Catalyst::View::Email::Template - Send Templated Email from Catalyst |
19 | |
20 | =head1 SYNOPSIS |
21 | |
ea115f9b |
22 | Sends Templated mail, based upon your default view. It captures the output |
529915ab |
23 | of the rendering path, slurps in based on mime-types and assembles a multi-part |
ea115f9b |
24 | email using Email::MIME::Creator and sends it out. |
529915ab |
25 | |
ea115f9b |
26 | =head1 CONFIGURATION |
27 | |
28 | Use the helper to create your View: |
29 | |
30 | $ script/myapp_create.pl view Email::Template Email::Template |
31 | |
32 | In your app configuration (example in L<YAML>): |
529915ab |
33 | |
34 | View::Email::Template: |
529915ab |
35 | # Optional prefix to look somewhere under the existing configured |
ea115f9b |
36 | # template paths. |
37 | # Default: none |
529915ab |
38 | template_prefix: email |
06afcdbc |
39 | # Where to look in the stash for the email information. |
ea115f9b |
40 | # Default: email |
529915ab |
41 | stash_key: email |
06afcdbc |
42 | # Define the defaults for the mail |
43 | default: |
ea115f9b |
44 | # Defines the default content type (mime type). |
45 | # Mandatory |
06afcdbc |
46 | content_type: text/html |
47 | # Defines the default charset for every MIME part with the content |
ea115f9b |
48 | # type text. |
49 | # According to RFC2049 a MIME part without a charset should |
06afcdbc |
50 | # be treated as US-ASCII by the mail client. |
51 | # If the charset is not set it won't be set for all MIME parts |
52 | # without an overridden one. |
ea115f9b |
53 | # Default: none |
06afcdbc |
54 | charset: utf-8 |
ea115f9b |
55 | # Defines the default view used to render the templates. |
56 | # If none is specified neither here nor in the stash |
57 | # Catalysts default view is used. |
58 | # Warning: if you don't tell Catalyst explicit which of your views should |
59 | # be its default one, C::V::Email::Template may choose the wrong one! |
06afcdbc |
60 | view: TT |
529915ab |
61 | # Setup how to send the email |
ea115f9b |
62 | # All those options are passed directly to Email::Send, |
63 | # for all available options look at its docs. |
529915ab |
64 | sender: |
06afcdbc |
65 | mailer: SMTP |
66 | mailer_args: |
67 | Host: smtp.example.com # defaults to localhost |
68 | username: username |
69 | password: password |
529915ab |
70 | |
71 | =head1 SENDING EMAIL |
72 | |
06afcdbc |
73 | Sending email is just setting up your defaults, the stash key and forwarding to the view. |
529915ab |
74 | |
75 | $c->stash->{email} = { |
76 | to => 'jshirley@gmail.com', |
77 | from => 'no-reply@foobar.com', |
78 | subject => 'I am a Catalyst generated email', |
06afcdbc |
79 | template => 'test.tt', |
529915ab |
80 | }; |
81 | $c->forward('View::Email::Template'); |
82 | |
06afcdbc |
83 | Alternatively if you want more control over your templates you can use the following idiom |
84 | to override the defaults: |
12c85b56 |
85 | |
86 | templates => [ |
06afcdbc |
87 | { |
88 | template => 'email/test.html.tt', |
89 | content_type => 'text/html', |
ea115f9b |
90 | charset => 'utf-8', |
06afcdbc |
91 | view => 'TT', |
92 | }, |
93 | { |
94 | template => 'email/test.plain.mason', |
95 | content_type => 'text/plain', |
ea115f9b |
96 | charset => 'utf-8', |
06afcdbc |
97 | view => 'Mason', |
98 | } |
12c85b56 |
99 | ] |
100 | |
101 | |
529915ab |
102 | If it fails $c->error will have the error message. |
103 | |
104 | =cut |
105 | |
06afcdbc |
106 | # here the defaults of Catalyst::View::Email are extended by the additional |
107 | # ones Template.pm needs. |
108 | |
529915ab |
109 | __PACKAGE__->config( |
110 | template_prefix => '', |
111 | ); |
112 | |
113 | |
114 | # This view hitches into your default view and will call the render function |
115 | # on the templates provided. This means that you have a layer of abstraction |
116 | # and you aren't required to modify your templates based on your desired engine |
117 | # (Template Toolkit or Mason, for example). As long as the view adequately |
118 | # supports ->render, all things are good. Mason, and others, are not good. |
119 | |
120 | # |
121 | # The path here is to check configuration for the template root, and then |
122 | # proceed to call render on the subsequent templates and stuff each one |
123 | # into an Email::MIME container. The mime-type will be stupidly guessed with |
124 | # the subdir on the template. |
125 | # |
529915ab |
126 | |
06afcdbc |
127 | # Set it up so if you have multiple parts, they're alternatives. |
128 | # This is on the top-level message, not the individual parts. |
129 | #multipart/alternative |
529915ab |
130 | |
06afcdbc |
131 | sub _validate_view { |
132 | my ($self, $view) = @_; |
133 | |
134 | croak "Email::Template's configured view '$view' isn't an object!" |
135 | unless (blessed($view)); |
136 | |
137 | croak "Email::Template's configured view '$view' isn't an Catalyst::View!" |
138 | unless ($view->isa('Catalyst::View')); |
139 | |
140 | croak "Email::Template's configured view '$view' doesn't have a render method!" |
141 | unless ($view->can('render')); |
142 | } |
529915ab |
143 | |
43090696 |
144 | sub generate_part { |
06afcdbc |
145 | my ($self, $c, $attrs) = @_; |
146 | |
147 | my $template_prefix = $self->{template_prefix}; |
148 | my $default_view = $self->{default}->{view}; |
149 | my $default_content_type = $self->{default}->{content_type}; |
ea115f9b |
150 | my $default_charset = $self->{default}->{charset}; |
151 | |
ea115f9b |
152 | my $view; |
153 | # use the view specified for the email part |
154 | if (exists $attrs->{view} && defined $attrs->{view} && $attrs->{view} ne '') { |
155 | $view = $c->view($attrs->{view}); |
156 | $c->log->debug("C::V::Email::Template uses specified view $view for rendering.") if $c->debug; |
157 | } |
158 | # if none specified use the configured default view |
159 | elsif ($default_view) { |
160 | $view = $c->view($default_view); |
161 | $c->log->debug("C::V::Email::Template uses default view $view for rendering.") if $c->debug;; |
162 | } |
163 | # else fallback to Catalysts default view |
164 | else { |
165 | $view = $c->view; |
c649c40e |
166 | $c->log->debug("C::V::Email::Template uses Catalysts default view $view for rendering.") if $c->debug;; |
ea115f9b |
167 | } |
06afcdbc |
168 | |
06afcdbc |
169 | # validate the per template view |
170 | $self->_validate_view($view); |
06afcdbc |
171 | |
172 | # prefix with template_prefix if configured |
173 | my $template = $template_prefix ne '' ? join('/', $template_prefix, $attrs->{template}) : $attrs->{template}; |
ea115f9b |
174 | |
43090696 |
175 | # setup the attributes (merge with defaults) |
176 | my $e_m_attrs = $self->setup_attributes($attrs); |
ea115f9b |
177 | |
06afcdbc |
178 | # render the email part |
179 | my $output = $view->render( $c, $template, { |
ea115f9b |
180 | content_type => $e_m_attrs->{content_type}, |
181 | stash_key => $self->{stash_key}, |
43090696 |
182 | %{$c->stash}, |
06afcdbc |
183 | }); |
43090696 |
184 | |
185 | if ( ref $output ) { |
186 | croak $output->can('as_string') ? $output->as_string : $output; |
529915ab |
187 | } |
43090696 |
188 | |
06afcdbc |
189 | return Email::MIME->create( |
ea115f9b |
190 | attributes => $e_m_attrs, |
43090696 |
191 | body => $output, |
06afcdbc |
192 | ); |
193 | } |
529915ab |
194 | |
06afcdbc |
195 | sub process { |
196 | my ( $self, $c ) = @_; |
529915ab |
197 | |
06afcdbc |
198 | # don't validate template_prefix |
8b10ee55 |
199 | |
ea115f9b |
200 | # the default view is validated if used |
12c85b56 |
201 | |
06afcdbc |
202 | # the content type should be validated by Email::MIME::Creator |
ea115f9b |
203 | |
204 | my $stash_key = $self->{stash_key}; |
12c85b56 |
205 | |
06afcdbc |
206 | croak "No template specified for rendering" |
207 | unless $c->stash->{$stash_key}->{template} |
208 | or $c->stash->{$stash_key}->{templates}; |
209 | |
210 | # this array holds the Email::MIME objects |
211 | # in case of the simple api only one |
212 | my @parts = (); |
12c85b56 |
213 | |
06afcdbc |
214 | # now find out if the single or multipart api was used |
215 | # prefer the multipart one |
216 | |
217 | # multipart api |
218 | if ($c->stash->{$stash_key}->{templates} |
219 | && ref $c->stash->{$stash_key}->{templates} eq 'ARRAY' |
220 | && ref $c->stash->{$stash_key}->{templates}[0] eq 'HASH') { |
221 | # loop through all parts of the mail |
222 | foreach my $part (@{$c->stash->{$stash_key}->{templates}}) { |
43090696 |
223 | push @parts, $self->generate_part($c, { |
06afcdbc |
224 | view => $part->{view}, |
225 | template => $part->{template}, |
226 | content_type => $part->{content_type}, |
ea115f9b |
227 | charset => $part->{charset}, |
06afcdbc |
228 | }); |
43090696 |
229 | } |
06afcdbc |
230 | } |
231 | # single part api |
232 | elsif($c->stash->{$stash_key}->{template}) { |
43090696 |
233 | push @parts, $self->generate_part($c, { |
06afcdbc |
234 | template => $c->stash->{$stash_key}->{template}, |
235 | }); |
236 | } |
237 | |
8b10ee55 |
238 | delete $c->stash->{$stash_key}->{body}; |
239 | $c->stash->{$stash_key}->{parts} ||= []; |
240 | push @{$c->stash->{$stash_key}->{parts}}, @parts; |
529915ab |
241 | |
242 | # Let C::V::Email do the actual sending. We just assemble the tasty bits. |
243 | return $self->next::method($c); |
244 | } |
245 | |
246 | =head1 TODO |
247 | |
248 | =head2 ATTACHMENTS |
249 | |
250 | There needs to be a method to support attachments. What I am thinking is |
251 | something along these lines: |
25650747 |
252 | |
529915ab |
253 | attachments => [ |
254 | # Set the body to a file handle object, specify content_type and |
255 | # the file name. (name is what it is sent at, not the file) |
256 | { body => $fh, name => "foo.pdf", content_type => "application/pdf" }, |
257 | # Or, specify a filename that is added, and hey, encoding! |
258 | { filename => "foo.gif", name => "foo.gif", content_type => "application/pdf", encoding => "quoted-printable" }, |
259 | # Or, just a path to a file, and do some guesswork for the content type |
260 | "/path/to/somefile.pdf", |
261 | ] |
262 | |
263 | =head1 SEE ALSO |
264 | |
265 | =head2 L<Catalyst::View::Email> - Send plain boring emails with Catalyst |
266 | |
267 | =head2 L<Catalyst::Manual> - The Catalyst Manual |
268 | |
269 | =head2 L<Catalyst::Manual::Cookbook> - The Catalyst Cookbook |
270 | |
271 | =head1 AUTHORS |
272 | |
273 | J. Shirley <jshirley@gmail.com> |
274 | |
12c85b56 |
275 | Simon Elliott <cpan@browsing.co.uk> |
276 | |
06afcdbc |
277 | Alexander Hartmaier <alex_hartmaier@hotmail.com> |
278 | |
529915ab |
279 | =head1 LICENSE |
280 | |
281 | This library is free software, you can redistribute it and/or modify it under |
282 | the same terms as Perl itself. |
283 | |
284 | =cut |
285 | |
286 | 1; |