line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIME::Lite::HT::HTML;
|
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
77214
|
use 5.006001;
|
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
937
|
|
4
|
3
|
|
|
3
|
|
25
|
use strict;
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
124
|
|
5
|
3
|
|
|
3
|
|
18
|
use warnings;
|
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
345
|
|
6
|
3
|
|
|
3
|
|
16
|
use strict;
|
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
92
|
|
7
|
3
|
|
|
3
|
|
8267
|
use MIME::Lite;
|
|
3
|
|
|
|
|
198394
|
|
|
3
|
|
|
|
|
131
|
|
8
|
3
|
|
|
3
|
|
3043
|
use MIME::Words qw(:all);
|
|
3
|
|
|
|
|
4678
|
|
|
3
|
|
|
|
|
931
|
|
9
|
3
|
|
|
3
|
|
4346
|
use Encode;
|
|
3
|
|
|
|
|
51022
|
|
|
3
|
|
|
|
|
357
|
|
10
|
3
|
|
|
3
|
|
7725
|
use HTML::Template;
|
|
3
|
|
|
|
|
76122
|
|
|
3
|
|
|
|
|
162
|
|
11
|
3
|
|
|
3
|
|
5967
|
use HTML::FormatText::WithLinks;
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use DateTime::Format::Mail;
|
13
|
|
|
|
|
|
|
use Carp;
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.05';
|
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 object with HTML
|
45
|
|
|
|
|
|
|
formatted mail.
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over 4
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item new
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return L object with HTML mail format.
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=back
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 ADITIONAL 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
|
|
|
|
|
|
|
arument of the filename option. See L for more information.
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 TmplParams
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The parameters of a template are set to this options.
|
70
|
|
|
|
|
|
|
This parameter must be the reference of hash.
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 TmplOptions
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Configuration of L is set to this option (e.g.
|
75
|
|
|
|
|
|
|
die_on_bad_params or path).
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 TimeZone
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
You can specified 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 tranfer.
|
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 specified 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 giving the orignal 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 to make this happy.
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub new {
|
115
|
|
|
|
|
|
|
my $class = shift;
|
116
|
|
|
|
|
|
|
my $options = @_ > 1 ? {@_} : $_[0];
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $template = delete $options->{ Template };
|
119
|
|
|
|
|
|
|
return croak "html template not defined" unless $template->{html};
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $time_zone = delete $options->{ TimeZone } || 'UTC';
|
122
|
|
|
|
|
|
|
my $tmpl_params = delete $options->{ TmplParams };
|
123
|
|
|
|
|
|
|
my $encoding = delete $options->{ Encoding } || '7bit';
|
124
|
|
|
|
|
|
|
my $charset_option = delete $options->{ Charset };
|
125
|
|
|
|
|
|
|
my $charset = ref $charset_option eq 'ARRAY' ? [ @{$charset_option} ] : [ $charset_option ];
|
126
|
|
|
|
|
|
|
$charset = [ $charset ] unless ref $charset eq 'ARRAY';
|
127
|
|
|
|
|
|
|
my $charset_input = shift @$charset || 'US-ASCII';
|
128
|
|
|
|
|
|
|
my $charset_output = shift @$charset || $charset_input;
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my %tmpl_options = ();
|
131
|
|
|
|
|
|
|
%tmpl_options = %{delete $options->{ TmplOptions }} if $options->{ TmplOptions };
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $msg = MIME::Lite->new(
|
134
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $t_html = HTML::Template->new( filename => $template->{html}, %tmpl_options );
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# -- fill in params
|
144
|
|
|
|
|
|
|
$t_html->param($tmpl_params) if $tmpl_params;
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# -- generate output
|
147
|
|
|
|
|
|
|
my $html = $t_html->output();
|
148
|
|
|
|
|
|
|
my $text = undef;
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
if( $template->{text} ) {
|
151
|
|
|
|
|
|
|
my $t_text = HTML::Template->new( filename => $template->{text}, %tmpl_options );
|
152
|
|
|
|
|
|
|
$t_text->param($tmpl_params) if $tmpl_params;
|
153
|
|
|
|
|
|
|
$text = $t_text->output();
|
154
|
|
|
|
|
|
|
}else{
|
155
|
|
|
|
|
|
|
my $f2 = HTML::FormatText::WithLinks->new(
|
156
|
|
|
|
|
|
|
before_link => '',
|
157
|
|
|
|
|
|
|
after_link => '',
|
158
|
|
|
|
|
|
|
footnote => '',
|
159
|
|
|
|
|
|
|
);
|
160
|
|
|
|
|
|
|
$text = $f2->parse($html);
|
161
|
|
|
|
|
|
|
}
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
return $msg;
|
176
|
|
|
|
|
|
|
} # /new
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub encode_subject {
|
179
|
|
|
|
|
|
|
my ( $subject, $charset_input, $charset_output ) = @_;
|
180
|
|
|
|
|
|
|
my $string = remove_utf8_flag( $subject );
|
181
|
|
|
|
|
|
|
Encode::from_to( $string, $charset_input, $charset_output )
|
182
|
|
|
|
|
|
|
if $charset_input ne $charset_output;
|
183
|
|
|
|
|
|
|
encode_mimeword( $string, 'b', $charset_output );
|
184
|
|
|
|
|
|
|
} # /encode_subject
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub encode_body {
|
187
|
|
|
|
|
|
|
my ( $body, $charset_input, $charset_output ) = @_;
|
188
|
|
|
|
|
|
|
my $string = remove_utf8_flag( $body );
|
189
|
|
|
|
|
|
|
Encode::from_to( $string, $charset_input, $charset_output )
|
190
|
|
|
|
|
|
|
if $charset_input ne $charset_output;
|
191
|
|
|
|
|
|
|
$string;
|
192
|
|
|
|
|
|
|
} # /encode_body
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub remove_utf8_flag {
|
195
|
|
|
|
|
|
|
pack 'C0A*', shift;
|
196
|
|
|
|
|
|
|
} # /remove_utf8_flag
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 AUTHOR
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Alexander Becker Ec a p f a n _a.t_ g m x . d eE
|
202
|
|
|
|
|
|
|
But all I did was c&p from L
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 SEE ALSO
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L, L, L, L
|
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; |