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

filter: Statementstmt 44.0% 26 / 59 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 8 / 12 Podpod 25.0% 1 / 4 totaltotal 32.7% 35 / 107 CC 14 SCAR 43.3
CC
14
Cov
30%
new
156.0
encode_subject
6.0
encode_body
6.0
CRAP 76.2
↑ Summary
1 package MIME::Lite::TT::HTML;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MIME::Words qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTML::FormatText::WithLinks;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 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<MIME::Lite> object with html formatted mail.
41  
42 =head1 METHODS
43  
44 =over 4
45  
46 =item new
47  
48 return L<MIME::Lite> 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<Template::Toolkit> 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<Template::Toolkit> 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<Encode> to make this happy.
100  
101 =cut
102  
103 0 ▶sub new {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
104 0 my $class = shift;
105 0 ▶ my $options = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
106  
107 0 my $template = delete $options->{ Template };
108 0 ▶ return croak "html template not defined" unless $template->{html};
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{"html"} 0 0 0
109  
110 0 ▶ my $time_zone = delete $options->{ TimeZone } || 'UTC';
Condition0%0/2
delete $options->{'TimeZone'} || 'UTC'
l!l
00
MC/DC0%0/1
delete $options->{'TimeZone'} || 'UTC'
delete $options->{'TimeZone'}
Truth table0%0/2
A
A= delete $options->{'TimeZone'}
Aresult
11
00
111 0 my $tmpl_params = delete $options->{ TmplParams };
112 0 ▶ my $encoding = delete $options->{ Encoding } || '7bit';
Condition0%0/2
delete $options->{'Encoding'} || '7bit'
l!l
00
MC/DC0%0/1
delete $options->{'Encoding'} || '7bit'
delete $options->{'Encoding'}
Truth table0%0/2
A
A= delete $options->{'Encoding'}
Aresult
11
00
113 0 my $charset_option = delete $options->{ Charset };
114 0 ▶ my $charset = ref $charset_option eq 'ARRAY' ? [ @{$charset_option} ] : [ $charset_option ];
Branch: 1 branch
BranchTrueFalseTotal
ref $charset_option eq 'ARRAY' ? : 0 0 0
115 0 ▶ $charset = [ $charset ] unless ref $charset eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $charset eq "ARRAY" 0 0 0
116 0 ▶ my $charset_input = shift @$charset || 'US-ASCII';
Condition0%0/2
shift @$charset || 'US-ASCII'
l!l
00
MC/DC0%0/1
shift @$charset || 'US-ASCII'
shift @$charset
Truth table0%0/2
A
A= shift @$charset
Aresult
11
00
117 0 ▶ my $charset_output = shift @$charset || $charset_input;
Condition0%0/3
shift @$charset || $charset_input
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @$charset || $charset_input
shift @$charset $charset_input
Truth table0%0/3
A || B
A= shift @$charsetB= $charset_input
ABresult
1X1
011
000
118
119 0 my $tt = Template->new( delete $options->{ TmplOptions } );
120  
121 my $msg = MIME::Lite->new(
122 0 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 0 my ( $text, $html );
129 0 ▶ $tt->process( $template->{html}, $tmpl_params, \$html ) or croak $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template->{"html"}, $tmpl_params, \$html) or croak($tt->error) 0 0 0
130 0 ▶ if ( $template->{text} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{'text'}) { } 0 0 0
131 0 ▶ $tt->process( $template->{text}, $tmpl_params, \$text ) or croak $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template->{"text"}, $tmpl_params, \$text) or croak($tt->error) 0 0 0
132 }else{
133 0 my $f2 = HTML::FormatText::WithLinks->new(
134 before_link => '',
135 after_link => '',
136 footnote => ''
137 );
138 0 $text = $f2->parse($html);
139 }
140  
141 0 $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 0 $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 0 $msg;
154 }
155  
156 0 ▶sub encode_subject {
Subroutine encode_subject: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my ( $subject, $charset_input, $charset_output ) = @_;
158 0 my $string = remove_utf8_flag( $subject );
159 0 ▶ Encode::from_to( $string, $charset_input, $charset_output )
Branch: 1 branch
BranchTrueFalseTotal
if $charset_input ne $charset_output 0 0 0
160 if $charset_input ne $charset_output;
161 0 encode_mimeword( $string, 'b', $charset_output );
162 }
163  
164 0 ▶sub encode_body {
Subroutine encode_body: not called CC=2 SCAR=17.9
Pod undocumented
165 0 my ( $body, $charset_input, $charset_output ) = @_;
166 0 my $string = remove_utf8_flag( $body );
167 0 ▶ Encode::from_to( $string, $charset_input, $charset_output )
Branch: 1 branch
BranchTrueFalseTotal
if $charset_input ne $charset_output 0 0 0
168 if $charset_input ne $charset_output;
169 0 $string;
170 }
171  
172 0 ▶sub remove_utf8_flag {
Subroutine remove_utf8_flag: not called CC=1 SCAR=6.9
Pod undocumented
173 0 pack 'C0A*', shift;
174 }
175  
176 =back
177  
178 =head1 AUTHOR
179  
180 Sheng Chun E<lt>chunzi@cpan.orgE<gt>
181  
182 =head1 SEE ALSO
183  
184 L<MIME::Lite::TT> L<MIME::Lite::TT::Japanese> L<MIME::Lite::TT::HTML::Japanese>
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 1;
↑ Summary