line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Email::Template; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
32990
|
use warnings; |
|
3
|
|
|
|
|
79
|
|
|
3
|
|
|
|
|
100
|
|
4
|
3
|
|
|
3
|
|
15
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
107
|
|
5
|
3
|
|
|
3
|
|
16
|
use Carp 'croak'; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
198
|
|
6
|
3
|
|
|
3
|
|
3055
|
use Encode qw( encode ); |
|
3
|
|
|
|
|
244086
|
|
|
3
|
|
|
|
|
1055
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Email::Template - Send "multipart/alternative" (text & html) email from a Template |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.02 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Email::Template; |
23
|
|
|
|
|
|
|
Email::Template->send( '/path/to/email/template.tt2', |
24
|
|
|
|
|
|
|
{ |
25
|
|
|
|
|
|
|
From => 'sender@domain.tld', |
26
|
|
|
|
|
|
|
To => 'recipient@domain.tld', |
27
|
|
|
|
|
|
|
Subject => 'Email::Template is easy to use', |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
tt_vars => { key => $value, ... }, |
30
|
|
|
|
|
|
|
convert => { rightmargin => 80, no_rowspacing => 1, ... } |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
) or warn "could not send the email"; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This is a fairly simple interface to generate "multipart/alternative" emails with both |
37
|
|
|
|
|
|
|
"text/html" and "text/plain" components using a single HTML based Template Toolkit template. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The HTML, once processed by Template Toolkit, is converted to text using |
40
|
|
|
|
|
|
|
HTML::FormatText::WithLinks::AndTables. Both HTML and text versions are then attached |
41
|
|
|
|
|
|
|
and sent via MIME::Lite. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Be sure to validate your sender and recipient addresses first (ie. Email::Valid->rfc822 ). |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 A NOTE ABOUT CHARACTER SETS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
If your template files are non-ASCII, be sure to pass { ENCODING => 'utf-8' } (or whatever) |
48
|
|
|
|
|
|
|
in the tt_new argument; otherwise, you will get gibberish in the emails. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The text/html and text/plain parts are encoded using utf-8 by default, or pass in 'charset' |
51
|
|
|
|
|
|
|
to choose a different one (e.g. iso-8859-1). |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 EXPORTS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
None by default. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 METHODS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 send |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The first argument to send() is the path to the Template file. Absolute paths are allowed. |
62
|
|
|
|
|
|
|
If the path is relative, it works the same as when using Template Toolkit. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
The second argument to send() is a hash reference containing the following possible options. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head3 MIME::Lite |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# REQUIRED |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
From => 'sender@domain.tld', |
71
|
|
|
|
|
|
|
To => 'recipient@domain.tld', |
72
|
|
|
|
|
|
|
Subject => 'a subject for your email', |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# OPTIONAL |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# charset to use for the text and html MIME parts (default utf-8) |
77
|
|
|
|
|
|
|
charset => 'utf-8', |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# arguments to be passed to MIME::Lite send() |
80
|
|
|
|
|
|
|
mime_lite_send => ['smtp', 'some.host', Debug=>1 ], |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# additional attachments to add via MIME::Lite attach() |
83
|
|
|
|
|
|
|
mime_lite_attach => [ {Type=>...,Data=>...}, ... ], |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# do not send(), just return the prepared MIME::Lite object |
86
|
|
|
|
|
|
|
return_mime_lite => 1, |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head3 Template Toolkit |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# OPTIONAL |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# configuration options passed into Template->new() |
93
|
|
|
|
|
|
|
tt_new => { INCLUDE_PATH => '/path/to/templates', ... }, |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# variables to interpolate via Template->process() |
96
|
|
|
|
|
|
|
tt_vars => { key => $value, ... }, |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head3 HTML::FormatText::WithLinks::AndTables |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# OPTIONAL |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# configuration options passed into convert() |
103
|
|
|
|
|
|
|
convert => { rm => 80, no_rowspacing => 1, ... } |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
NOTE: all additional arguments not explicitely mentioned above will be passed into |
106
|
|
|
|
|
|
|
MIME::Lite->new() |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Assuming "return_mime_lite => 1" was not passed in the arguments list, on success |
109
|
|
|
|
|
|
|
send() returns the value of MIME::Lite->as_string(), or on failure returns nothing. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
3
|
|
|
3
|
|
139551
|
use Template; |
|
3
|
|
|
|
|
102776
|
|
|
3
|
|
|
|
|
104
|
|
114
|
3
|
|
|
3
|
|
4668
|
use MIME::Lite; |
|
3
|
|
|
|
|
135720
|
|
|
3
|
|
|
|
|
143
|
|
115
|
3
|
|
|
3
|
|
3069
|
use HTML::FormatText::WithLinks::AndTables; |
|
3
|
|
|
|
|
485512
|
|
|
3
|
|
|
|
|
1780
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub send { |
118
|
2
|
50
|
33
|
2
|
1
|
850
|
shift if $_[0] eq __PACKAGE__ or ref $_[0]; |
119
|
|
|
|
|
|
|
|
120
|
2
|
|
|
|
|
6
|
my ($path_to_tt2, $args) = @_; |
121
|
2
|
50
|
|
|
|
16
|
if ($path_to_tt2 =~ /^\//) { |
122
|
2
|
50
|
33
|
|
|
90
|
croak "Invalid path to template ($path_to_tt2)\n" |
123
|
|
|
|
|
|
|
if not -e $path_to_tt2 and -T _; # make sure template exists |
124
|
2
|
|
50
|
|
|
16
|
$args->{tt_new}->{ABSOLUTE} ||= 1; |
125
|
|
|
|
|
|
|
} |
126
|
2
|
50
|
|
|
|
8
|
croak "Invalid or missing arguments to Email::Template->send()\n" |
127
|
|
|
|
|
|
|
if not _required_args($args); # make sure required args are present |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# process the template |
130
|
2
|
|
|
|
|
40
|
my $tt = Template->new($args->{tt_new}); |
131
|
2
|
50
|
|
|
|
53032
|
$tt->process($path_to_tt2, $args->{tt_vars}, \my $html) or croak $tt->error, "\n"; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# convert html to text |
134
|
2
|
|
|
|
|
623024
|
my $text = HTML::FormatText::WithLinks::AndTables->convert($html, $args->{convert}); |
135
|
|
|
|
|
|
|
|
136
|
8
|
|
|
|
|
28
|
my %mime_lite_args = map { $_ => $args->{$_} } |
|
12
|
|
|
|
|
34
|
|
137
|
2
|
|
|
|
|
28420
|
grep { ! /^(?: |
138
|
|
|
|
|
|
|
tt_vars | tt_args | mime_lite_send | |
139
|
|
|
|
|
|
|
mime_lite_attach | return_mime_lite | |
140
|
|
|
|
|
|
|
convert |
141
|
|
|
|
|
|
|
)\z/x } keys %$args; |
142
|
|
|
|
|
|
|
|
143
|
2
|
|
50
|
|
|
24
|
my $charset = $args->{charset} || 'utf-8'; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# send the email |
146
|
2
|
|
|
|
|
28
|
my $email = MIME::Lite->new( %mime_lite_args, Type => 'multipart/alternative' ); |
147
|
2
|
|
|
|
|
2188308
|
$email->attach( Type => "text/plain; charset=${charset}", Data => encode( $charset, $text ) ); |
148
|
2
|
|
|
|
|
708
|
$email->attach( Type => "text/html; charset=${charset}", Data => encode( $charset, $html ) ); |
149
|
2
|
50
|
33
|
|
|
286
|
if ( $args->{mime_lite_attach} and ref $args->{mime_lite_attach} eq 'ARRAY' ) { |
150
|
0
|
|
|
|
|
0
|
for (@{ $args->{mime_lite_attach} }) { |
|
0
|
|
|
|
|
0
|
|
151
|
0
|
0
|
|
|
|
0
|
next if not ref $_ eq 'HASH'; |
152
|
0
|
|
|
|
|
0
|
$email->attach(%$_); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
2
|
50
|
|
|
|
10
|
return $email if $args->{return_mime_lite}; |
156
|
|
|
|
|
|
|
|
157
|
2
|
|
|
|
|
10
|
my $raw_email = $email->as_string; |
158
|
2
|
50
|
|
|
|
1026
|
my @send_args = $args->{mime_lite_send} ? @{ $args->{mime_lite_send} } : (); |
|
0
|
|
|
|
|
0
|
|
159
|
2
|
|
|
|
|
12
|
$email->send(@send_args); |
160
|
0
|
0
|
|
|
|
0
|
return if not $email->last_send_successful; # requires $MIME::Lite::VERSION >= 3.01_04 |
161
|
0
|
|
|
|
|
0
|
return $raw_email; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub _required_args { |
165
|
2
|
|
|
2
|
|
10
|
my $args = shift; |
166
|
2
|
50
|
|
|
|
10
|
return if not ref $args eq 'HASH'; |
167
|
2
|
|
|
|
|
4
|
for (qw( To From Subject )) { |
168
|
6
|
50
|
|
|
|
20
|
return if not $args->{$_}; |
169
|
|
|
|
|
|
|
} |
170
|
2
|
|
|
|
|
8
|
return 1; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 SEE ALSO |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Template |
176
|
|
|
|
|
|
|
MIME::Lite |
177
|
|
|
|
|
|
|
HTML::FormatText::WithLinks::AndTables |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 AUTHOR |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Shaun Fryer, C<< >> |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Ryan D Johnson, C<< >>, charset support |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 BUGS |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
190
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
191
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SUPPORT |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
perldoc Email::Template |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
You can also look for information at: |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=over 4 |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
L |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
L |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item * CPAN Ratings |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * Search CPAN |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=back |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Everybody. :) |
229
|
|
|
|
|
|
|
L |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Copyright 2008 Shaun Fryer, all rights reserved. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
236
|
|
|
|
|
|
|
under the same terms as Perl itself. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=cut |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1; # End of Email::Template |