line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIME::Lite::HT::HTML; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
20641
|
use 5.006001; |
|
2
|
|
|
|
|
22
|
|
4
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
41
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
6
|
2
|
|
|
2
|
|
8
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
30
|
|
7
|
2
|
|
|
2
|
|
1532
|
use MIME::Lite; |
|
2
|
|
|
|
|
66002
|
|
|
2
|
|
|
|
|
82
|
|
8
|
2
|
|
|
2
|
|
864
|
use MIME::Words qw(:all); |
|
2
|
|
|
|
|
2578
|
|
|
2
|
|
|
|
|
1751
|
|
9
|
2
|
|
|
2
|
|
993
|
use Encode; |
|
2
|
|
|
|
|
28450
|
|
|
2
|
|
|
|
|
168
|
|
10
|
2
|
|
|
2
|
|
2138
|
use HTML::Template; |
|
2
|
|
|
|
|
26539
|
|
|
2
|
|
|
|
|
87
|
|
11
|
2
|
|
|
2
|
|
1037
|
use HTML::FormatText::WithLinks; |
|
2
|
|
|
|
|
90596
|
|
|
2
|
|
|
|
|
74
|
|
12
|
2
|
|
|
2
|
|
1076
|
use DateTime::Format::Mail; |
|
2
|
|
|
|
|
966297
|
|
|
2
|
|
|
|
|
84
|
|
13
|
2
|
|
|
2
|
|
16
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1175
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
MIME::Lite::HT::HTML - Create HTML mail with MIME::Lite and HTML::Template |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use MIME::Lite::HT::HTML; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $msg = MIME::Lite::HT::HTML->new( |
26
|
|
|
|
|
|
|
From => 'from@example.com', |
27
|
|
|
|
|
|
|
To => 'to@example.com', |
28
|
|
|
|
|
|
|
Subject => 'Subject', |
29
|
|
|
|
|
|
|
TimeZone => 'Europe/Berlin', |
30
|
|
|
|
|
|
|
Encoding => 'quoted-printable', |
31
|
|
|
|
|
|
|
Template => { |
32
|
|
|
|
|
|
|
html => 'mail.html', |
33
|
|
|
|
|
|
|
text => 'mail.txt', |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
Charset => 'utf8', |
36
|
|
|
|
|
|
|
TmplOptions => \%options, |
37
|
|
|
|
|
|
|
TmplParams => \%params, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$msg->send; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
This module provide easy interface to make L<MIME::Lite> object with HTML |
45
|
|
|
|
|
|
|
formatted mail. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over 4 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item new |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return L<MIME::Lite> object with HTML mail format. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=back |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 ADDITIONAL OPTIONS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 Template |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This is a mapping of filenames to the two variants of templates (HTML or text). |
62
|
|
|
|
|
|
|
You define, which file will be used for the HTML-part and the plain/text part. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
The filenames will be passed to the constructor of HTML::Template, as |
65
|
|
|
|
|
|
|
argument of the filename option. See L<HTML::Template> for more information. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 TmplParams |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The parameters of a template are set to these options. |
70
|
|
|
|
|
|
|
This parameter must be the reference of hash. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 TmplOptions |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Configuration of L<HTML::Template> is set to this option (e.g. |
75
|
|
|
|
|
|
|
die_on_bad_params or path). |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 TimeZone |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
You can specify the time zone of the mail date: |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
TimeZone => 'Asia/Shanghai', |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
default using 'UTC' if not defined. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 Encoding |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Mail body will be encoded for transfer. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Use encoding: | If your message contains: |
90
|
|
|
|
|
|
|
------------------------------------------------------------ |
91
|
|
|
|
|
|
|
7bit | Only 7-bit text, all lines <1000 characters |
92
|
|
|
|
|
|
|
8bit | 8-bit text, all lines <1000 characters |
93
|
|
|
|
|
|
|
quoted-printable | 8-bit text or long lines (more reliable than "8bit") |
94
|
|
|
|
|
|
|
base64 | Largely non-textual data: a GIF, a tar file, etc. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
default using '7bit' if not defined. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 Charset |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
You can specify the charset of your mail, both subject and body will using the charset |
101
|
|
|
|
|
|
|
to make mail reader's client satisfied. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Charset => 'big5', |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
And, if you are giving the original words as UTF8 and attempt to mail them as GB2312 charset, |
106
|
|
|
|
|
|
|
you can define the charset like: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Charset => [ 'utf8' => 'gb2312' ], |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
We will using L<Encode> to make this happy. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub new { |
115
|
2
|
|
|
2
|
1
|
335
|
my $class = shift; |
116
|
2
|
50
|
|
|
|
24
|
my $options = @_ > 1 ? {@_} : $_[0]; |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
16
|
my $template = delete $options->{ Template }; |
119
|
2
|
50
|
|
|
|
10
|
return croak "html template not defined" unless $template->{html}; |
120
|
|
|
|
|
|
|
|
121
|
2
|
|
50
|
|
|
8
|
my $time_zone = delete $options->{ TimeZone } || 'UTC'; |
122
|
2
|
|
|
|
|
5
|
my $tmpl_params = delete $options->{ TmplParams }; |
123
|
2
|
|
50
|
|
|
8
|
my $encoding = delete $options->{ Encoding } || '7bit'; |
124
|
2
|
|
|
|
|
6
|
my $charset_option = delete $options->{ Charset }; |
125
|
2
|
50
|
|
|
|
7
|
my $charset = ref $charset_option eq 'ARRAY' ? [ @{$charset_option} ] : [ $charset_option ]; |
|
0
|
|
|
|
|
0
|
|
126
|
2
|
50
|
|
|
|
8
|
$charset = [ $charset ] unless ref $charset eq 'ARRAY'; |
127
|
2
|
|
50
|
|
|
8
|
my $charset_input = shift @$charset || 'US-ASCII'; |
128
|
2
|
|
33
|
|
|
13
|
my $charset_output = shift @$charset || $charset_input; |
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
6
|
my %tmpl_options = (); |
131
|
2
|
100
|
|
|
|
9
|
%tmpl_options = %{delete $options->{ TmplOptions }} if $options->{ TmplOptions }; |
|
1
|
|
|
|
|
4
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $msg = MIME::Lite->new( |
134
|
2
|
|
|
|
|
10
|
Subject => encode_subject( delete $options->{ Subject }, $charset_input, $charset_output ), |
135
|
|
|
|
|
|
|
Type => 'multipart/alternative', |
136
|
|
|
|
|
|
|
Date => DateTime::Format::Mail->format_datetime( DateTime->now->set_time_zone($time_zone) ), |
137
|
|
|
|
|
|
|
%$options, |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# -- Create templates. |
141
|
2
|
|
|
|
|
68342
|
my $t_html = HTML::Template->new( filename => $template->{html}, %tmpl_options ); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# -- fill in params |
144
|
2
|
50
|
|
|
|
1266
|
$t_html->param($tmpl_params) if $tmpl_params; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# -- generate output |
147
|
2
|
|
|
|
|
9
|
my $html = $t_html->output(); |
148
|
2
|
|
|
|
|
120
|
my $text = undef; |
149
|
|
|
|
|
|
|
|
150
|
2
|
50
|
|
|
|
6
|
if( $template->{text} ) { |
151
|
2
|
|
|
|
|
9
|
my $t_text = HTML::Template->new( filename => $template->{text}, %tmpl_options ); |
152
|
2
|
50
|
|
|
|
978
|
$t_text->param($tmpl_params) if $tmpl_params; |
153
|
2
|
|
|
|
|
7
|
$text = $t_text->output(); |
154
|
|
|
|
|
|
|
}else{ |
155
|
0
|
|
|
|
|
0
|
my $f2 = HTML::FormatText::WithLinks->new( |
156
|
|
|
|
|
|
|
before_link => '', |
157
|
|
|
|
|
|
|
after_link => '', |
158
|
|
|
|
|
|
|
footnote => '', |
159
|
|
|
|
|
|
|
); |
160
|
0
|
|
|
|
|
0
|
$text = $f2->parse($html); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
2
|
|
|
|
|
126
|
$msg->attach( |
164
|
|
|
|
|
|
|
Type => sprintf( 'text/plain; charset=%s', $charset_output ), |
165
|
|
|
|
|
|
|
Data => encode_body( $text, $charset_input, $charset_output ), |
166
|
|
|
|
|
|
|
Encoding => $encoding, |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
347
|
$msg->attach( |
170
|
|
|
|
|
|
|
Type => sprintf( 'text/html; charset=%s', $charset_output ), |
171
|
|
|
|
|
|
|
Data => encode_body( $html, $charset_input, $charset_output ), |
172
|
|
|
|
|
|
|
Encoding => $encoding, |
173
|
|
|
|
|
|
|
); |
174
|
|
|
|
|
|
|
|
175
|
2
|
|
|
|
|
292
|
return $msg; |
176
|
|
|
|
|
|
|
} # /new |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub encode_subject { |
179
|
2
|
|
|
2
|
0
|
6
|
my ( $subject, $charset_input, $charset_output ) = @_; |
180
|
2
|
|
|
|
|
7
|
my $string = remove_utf8_flag( $subject ); |
181
|
2
|
50
|
|
|
|
6
|
Encode::from_to( $string, $charset_input, $charset_output ) |
182
|
|
|
|
|
|
|
if $charset_input ne $charset_output; |
183
|
2
|
|
|
|
|
12
|
encode_mimeword( $string, 'b', $charset_output ); |
184
|
|
|
|
|
|
|
} # /encode_subject |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub encode_body { |
187
|
4
|
|
|
4
|
0
|
10
|
my ( $body, $charset_input, $charset_output ) = @_; |
188
|
4
|
|
|
|
|
10
|
my $string = remove_utf8_flag( $body ); |
189
|
4
|
50
|
|
|
|
15
|
Encode::from_to( $string, $charset_input, $charset_output ) |
190
|
|
|
|
|
|
|
if $charset_input ne $charset_output; |
191
|
4
|
|
|
|
|
17
|
$string; |
192
|
|
|
|
|
|
|
} # /encode_body |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub remove_utf8_flag { |
195
|
6
|
|
|
6
|
0
|
30
|
pack 'C0A*', shift; |
196
|
|
|
|
|
|
|
} # /remove_utf8_flag |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 AUTHOR |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Alexander Becker E<lt>asb@cpan.orgE<gt> |
202
|
|
|
|
|
|
|
But all I did was c&p from L<MIME::Lite::TT::HTML> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 SEE ALSO |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L<HTML::Template>, L<MIME::Lite>, L<MIME::Lite::TT>, L<MIME::Lite::TT::HTML> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 LICENSE |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under |
211
|
|
|
|
|
|
|
the same terms as Perl itself. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
1; |