blib/lib/MIME/Lite/HT/HTML.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 55.0% 11 / 20 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 15 / 15 Podpod 25.0% 1 / 4 totaltotal 80.1% 101 / 126 CC 15 SCAR 20.9
CC
15
Cov
83%
new
16.4
encode_subject
2.0
encode_body
2.0
CRAP 8.1
↑ Summary
1 package MIME::Lite::HT::HTML;
2
3 2 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MIME::Words qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::FormatText::WithLinks;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 2 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 2 ▶sub new {
Subroutine new: called CC=13 SCAR=28.0
Pod documented
115 2 my $class = shift;
116 2 ▶ my $options = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2 0 2
117
118 2 my $template = delete $options->{ Template };
119 2 ▶ return croak "html template not defined" unless $template->{html};
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{"html"} 0 2 2
120
121 2 ▶ my $time_zone = delete $options->{ TimeZone } || 'UTC';
Condition50%1/2
delete $options->{'TimeZone'} || 'UTC'
l!l
20
MC/DC0%0/1
delete $options->{'TimeZone'} || 'UTC'
delete $options->{'TimeZone'}
Truth table50%1/2
A
A= delete $options->{'TimeZone'}
Aresult
11
00
122 2 my $tmpl_params = delete $options->{ TmplParams };
123 2 ▶ my $encoding = delete $options->{ Encoding } || '7bit';
Condition50%1/2
delete $options->{'Encoding'} || '7bit'
l!l
20
MC/DC0%0/1
delete $options->{'Encoding'} || '7bit'
delete $options->{'Encoding'}
Truth table50%1/2
A
A= delete $options->{'Encoding'}
Aresult
11
00
124 2 my $charset_option = delete $options->{ Charset };
125 0 ▶ my $charset = ref $charset_option eq 'ARRAY' ? [ @{$charset_option} ] : [ $charset_option ];
Branch: 1 branch
BranchTrueFalseTotal
ref $charset_option eq 'ARRAY' ? : 0 2 2
126 2 ▶ $charset = [ $charset ] unless ref $charset eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $charset eq "ARRAY" 0 2 2
127 2 ▶ my $charset_input = shift @$charset || 'US-ASCII';
Condition50%1/2
shift @$charset || 'US-ASCII'
l!l
20
MC/DC0%0/1
shift @$charset || 'US-ASCII'
shift @$charset
Truth table50%1/2
A
A= shift @$charset
Aresult
11
00
128 2 ▶ my $charset_output = shift @$charset || $charset_input;
Condition33%1/3
shift @$charset || $charset_input
l!l&&r!l&&!r
020
MC/DC0%0/2
shift @$charset || $charset_input
shift @$charset $charset_input
Truth table33%1/3
A || B
A= shift @$charsetB= $charset_input
ABresult
1X1
011
000
129
130 2 my %tmpl_options = ();
131 2 ▶ %tmpl_options = %{delete $options->{ TmplOptions }} if $options->{ TmplOptions };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"TmplOptions"} 1 1 2
132
133 my $msg = MIME::Lite->new(
134 2 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 my $t_html = HTML::Template->new( filename => $template->{html}, %tmpl_options );
142
143 # -- fill in params
144 2 ▶ $t_html->param($tmpl_params) if $tmpl_params;
Branch: 1 branch
BranchTrueFalseTotal
if $tmpl_params 0 2 2
145
146 # -- generate output
147 2 my $html = $t_html->output();
148 2 my $text = undef;
149
150 2 ▶ if( $template->{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{'text'}) { } 2 0 2
151 2 my $t_text = HTML::Template->new( filename => $template->{text}, %tmpl_options );
152 2 ▶ $t_text->param($tmpl_params) if $tmpl_params;
Branch: 1 branch
BranchTrueFalseTotal
if $tmpl_params 0 2 2
153 2 $text = $t_text->output();
154 }else{
155 0 my $f2 = HTML::FormatText::WithLinks->new(
156 before_link => '',
157 after_link => '',
158 footnote => '',
159 );
160 0 $text = $f2->parse($html);
161 }
162
163 2 $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 $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 return $msg;
176 } # /new
177
178 2 ▶sub encode_subject {
Subroutine encode_subject: called CC=2 SCAR=7.0
Pod undocumented
179 2 my ( $subject, $charset_input, $charset_output ) = @_;
180 2 my $string = remove_utf8_flag( $subject );
181 2 ▶ Encode::from_to( $string, $charset_input, $charset_output )
Branch: 1 branch
BranchTrueFalseTotal
if $charset_input ne $charset_output 0 2 2
182 if $charset_input ne $charset_output;
183 2 encode_mimeword( $string, 'b', $charset_output );
184 } # /encode_subject
185
186 4 ▶sub encode_body {
Subroutine encode_body: called CC=2 SCAR=7.0
Pod undocumented
187 4 my ( $body, $charset_input, $charset_output ) = @_;
188 4 my $string = remove_utf8_flag( $body );
189 4 ▶ Encode::from_to( $string, $charset_input, $charset_output )
Branch: 1 branch
BranchTrueFalseTotal
if $charset_input ne $charset_output 0 4 4
190 if $charset_input ne $charset_output;
191 4 $string;
192 } # /encode_body
193
194 6 ▶sub remove_utf8_flag {
Subroutine remove_utf8_flag: called CC=1 SCAR=0.0
Pod undocumented
195 6 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 2 1;
↑ Summary