line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIME::Lite::TT::HTML; |
2
|
1
|
|
|
1
|
|
27711
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
3
|
1
|
|
|
1
|
|
1441
|
use MIME::Lite; |
|
1
|
|
|
|
|
39164
|
|
|
1
|
|
|
|
|
35
|
|
4
|
1
|
|
|
1
|
|
794
|
use MIME::Words qw(:all); |
|
1
|
|
|
|
|
1126
|
|
|
1
|
|
|
|
|
125
|
|
5
|
1
|
|
|
1
|
|
935
|
use Encode; |
|
1
|
|
|
|
|
9618
|
|
|
1
|
|
|
|
|
80
|
|
6
|
1
|
|
|
1
|
|
676
|
use Template; |
|
1
|
|
|
|
|
146382
|
|
|
1
|
|
|
|
|
34
|
|
7
|
1
|
|
|
1
|
|
980
|
use DateTime::Format::Mail; |
|
1
|
|
|
|
|
256500
|
|
|
1
|
|
|
|
|
50
|
|
8
|
1
|
|
|
1
|
|
548
|
use HTML::FormatText::WithLinks; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Carp; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
MIME::Lite::TT::HTML - Create html mail with MIME::Lite and TT |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use MIME::Lite::TT::HTML; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $msg = MIME::Lite::TT::HTML->new( |
22
|
|
|
|
|
|
|
From => 'from@example.com', |
23
|
|
|
|
|
|
|
To => 'to@example.com', |
24
|
|
|
|
|
|
|
Subject => 'Subject', |
25
|
|
|
|
|
|
|
TimeZone => 'Asia/Shanghai', |
26
|
|
|
|
|
|
|
Encoding => 'quoted-printable', |
27
|
|
|
|
|
|
|
Template => { |
28
|
|
|
|
|
|
|
html => 'mail.html', |
29
|
|
|
|
|
|
|
text => 'mail.txt', |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
Charset => 'utf8', |
32
|
|
|
|
|
|
|
TmplOptions => \%options, |
33
|
|
|
|
|
|
|
TmplParams => \%params, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$msg->send; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
This module provide easy interface to make L object with html formatted mail. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=over 4 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item new |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
return L object with html mail format. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 ADITIONAL OPTIONS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 Template |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
The same value passed to the 1st argument of the process method of L is set to this option. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 TmplParams |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
The parameter of a template is set to this option. |
59
|
|
|
|
|
|
|
This parameter must be the reference of hash. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 TmplOptions |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
configuration of L is set to this option. |
64
|
|
|
|
|
|
|
ABSOLUTE and RELATIVE are set to 1 by the default. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 TimeZone |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
You can specified the time zone of the mail date: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
TimeZone => 'Asia/Shanghai', |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
default using 'UTC' if not defined. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 Encoding |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Mail body will be encoded for tranfer. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Use encoding: | If your message contains: |
79
|
|
|
|
|
|
|
------------------------------------------------------------ |
80
|
|
|
|
|
|
|
7bit | Only 7-bit text, all lines <1000 characters |
81
|
|
|
|
|
|
|
8bit | 8-bit text, all lines <1000 characters |
82
|
|
|
|
|
|
|
quoted-printable | 8-bit text or long lines (more reliable than "8bit") |
83
|
|
|
|
|
|
|
base64 | Largely non-textual data: a GIF, a tar file, etc. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
default using '7bit' if not defined. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 Charset |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
You can specified the charset of your mail, both subject and body will using the charset |
90
|
|
|
|
|
|
|
to make mail reader's client satisfied. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Charset => 'big5', |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
And, if you giving the orignal words as UTF8 and attempt to mail them as GB2312 charset, |
95
|
|
|
|
|
|
|
you can define the charset like: |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Charset => [ 'utf8' => 'gb2312' ], |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
We will using L to make this happy. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub new { |
104
|
|
|
|
|
|
|
my $class = shift; |
105
|
|
|
|
|
|
|
my $options = @_ > 1 ? {@_} : $_[0]; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $template = delete $options->{ Template }; |
108
|
|
|
|
|
|
|
return croak "html template not defined" unless $template->{html}; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $time_zone = delete $options->{ TimeZone } || 'UTC'; |
111
|
|
|
|
|
|
|
my $tmpl_params = delete $options->{ TmplParams }; |
112
|
|
|
|
|
|
|
my $encoding = delete $options->{ Encoding } || '7bit'; |
113
|
|
|
|
|
|
|
my $charset_option = delete $options->{ Charset }; |
114
|
|
|
|
|
|
|
my $charset = ref $charset_option eq 'ARRAY' ? [ @{$charset_option} ] : [ $charset_option ]; |
115
|
|
|
|
|
|
|
$charset = [ $charset ] unless ref $charset eq 'ARRAY'; |
116
|
|
|
|
|
|
|
my $charset_input = shift @$charset || 'US-ASCII'; |
117
|
|
|
|
|
|
|
my $charset_output = shift @$charset || $charset_input; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $tt = Template->new( delete $options->{ TmplOptions } ); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $msg = MIME::Lite->new( |
122
|
|
|
|
|
|
|
Subject => encode_subject( delete $options->{ Subject }, $charset_input, $charset_output ), |
123
|
|
|
|
|
|
|
Type => 'multipart/alternative', |
124
|
|
|
|
|
|
|
Date => DateTime::Format::Mail->format_datetime( DateTime->now->set_time_zone($time_zone) ), |
125
|
|
|
|
|
|
|
%$options, |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my ( $text, $html ); |
129
|
|
|
|
|
|
|
$tt->process( $template->{html}, $tmpl_params, \$html ) or croak $tt->error; |
130
|
|
|
|
|
|
|
if ( $template->{text} ){ |
131
|
|
|
|
|
|
|
$tt->process( $template->{text}, $tmpl_params, \$text ) or croak $tt->error; |
132
|
|
|
|
|
|
|
}else{ |
133
|
|
|
|
|
|
|
my $f2 = HTML::FormatText::WithLinks->new( |
134
|
|
|
|
|
|
|
before_link => '', |
135
|
|
|
|
|
|
|
after_link => '', |
136
|
|
|
|
|
|
|
footnote => '' |
137
|
|
|
|
|
|
|
); |
138
|
|
|
|
|
|
|
$text = $f2->parse($html); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$msg->attach( |
142
|
|
|
|
|
|
|
Type => sprintf( 'text/plain; charset=%s', $charset_output ), |
143
|
|
|
|
|
|
|
Data => encode_body( $text, $charset_input, $charset_output ), |
144
|
|
|
|
|
|
|
Encoding => $encoding, |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$msg->attach( |
148
|
|
|
|
|
|
|
Type => sprintf( 'text/html; charset=%s', $charset_output ), |
149
|
|
|
|
|
|
|
Data => encode_body( $html, $charset_input, $charset_output ), |
150
|
|
|
|
|
|
|
Encoding => $encoding, |
151
|
|
|
|
|
|
|
); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
$msg; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub encode_subject { |
157
|
|
|
|
|
|
|
my ( $subject, $charset_input, $charset_output ) = @_; |
158
|
|
|
|
|
|
|
my $string = remove_utf8_flag( $subject ); |
159
|
|
|
|
|
|
|
Encode::from_to( $string, $charset_input, $charset_output ) |
160
|
|
|
|
|
|
|
if $charset_input ne $charset_output; |
161
|
|
|
|
|
|
|
encode_mimeword( $string, 'b', $charset_output ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub encode_body { |
165
|
|
|
|
|
|
|
my ( $body, $charset_input, $charset_output ) = @_; |
166
|
|
|
|
|
|
|
my $string = remove_utf8_flag( $body ); |
167
|
|
|
|
|
|
|
Encode::from_to( $string, $charset_input, $charset_output ) |
168
|
|
|
|
|
|
|
if $charset_input ne $charset_output; |
169
|
|
|
|
|
|
|
$string; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub remove_utf8_flag { |
173
|
|
|
|
|
|
|
pack 'C0A*', shift; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 AUTHOR |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Sheng Chun Echunzi@cpan.orgE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SEE ALSO |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
L L L |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 LICENSE |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under |
189
|
|
|
|
|
|
|
the same terms as Perl itself. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |