Builder.pm

filter: Statementstmt 87.2% 191 / 219 Branchbran 83.3% 70 / 84 Conditioncond 61.1% 44 / 72 MC/DCmcdc 39.0% 16 / 41 Subroutinesub 93.1% 27 / 29 Podpod 75.0% 3 / 4 totaltotal 78.1% 351 / 449 CC 74 SCAR 34.3
CC
74
Cov
81%
_convert_text
63.9
purge_cache
20.0
build_message
14.0
CRAP 31.0
↑ Summary Builder/Role/File.pm »
1 # ============================================================================
2 package Mail::Builder;
3 # ============================================================================
4  
5 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our $VERSION = '2.13';
9 8 our $AUTHORITY = 'cpan:MAROS';
10  
11 8 ▶use Mail::Builder::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Mail::Builder::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use Email::MessageID;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Email::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use Mail::Builder::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Mail::Builder::Attachment;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Mail::Builder::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use Mail::Builder::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 has 'plaintext' => (
28 is => 'rw',
29 isa => 'Str',
30 predicate => 'has_plaintext',
31 clearer => 'clear_plaintext',
32 trigger => \&_set_autotext_plaintext,
33 );
34  
35 8 has 'htmltext' => (
36 is => 'rw',
37 isa => 'Str',
38 predicate => 'has_htmltext',
39 clearer => 'clear_htmltext',
40 trigger => \&_generate_plaintext,
41 );
42  
43 8 has 'date' => (
44 is => 'rw',
45 isa => 'Mail::Builder::Type::Date',
46 lazy_build => 1,
47 clearer => 'clear_date',
48 );
49  
50 8 has 'subject' => (
51 is => 'rw',
52 isa => 'Str',
53 #required => 1,
54 predicate => 'has_subject',
55 );
56  
57 8 has 'organization' => (
58 is => 'rw',
59 isa => 'Str',
60 predicate => 'has_organization',
61 clearer => 'clear_organization',
62 );
63  
64 8 has 'priority' => (
65 is => 'rw',
66 isa => 'Mail::Builder::Type::Priority',
67 default => 3,
68 );
69  
70 8 has 'language' => (
71 is => 'rw',
72 isa => 'Str',
73 clearer => 'clear_language',
74 predicate => 'has_language',
75 );
76  
77 8 has 'mailer' => (
78 is => 'rw',
79 isa => 'Str',
80 required => 1,
81 default => "Mail::Builder v$VERSION",
82 );
83  
84 8 has 'autotext' => (
85 is => 'rw',
86 isa => 'Bool',
87 default => 1,
88 );
89  
90 8 has 'messageid' => (
91 is => 'ro',
92 isa => 'Email::MessageID',
93 clearer => 'clear_messageid',
94 predicate => 'has_messageid',
95 );
96  
97 8 has '_plaintext_autotext' => (
98 is => 'rw',
99 isa => 'Bool',
100 default => 0,
101 );
102  
103 8 has '_boundary' => (
104 is => 'rw',
105 isa => 'Int',
106 default => 1,
107 );
108  
109 8 foreach my $field (qw(from reply returnpath sender)) {
110 32 has $field => (
111 is => 'rw',
112 isa => 'Mail::Builder::Type::Address',
113 coerce => 1,
114 predicate => 'has_'.$field,
115 clearer => 'clear'.$field,
116 );
117 32 around $field => \&_address_accessor;
118 }
119  
120 has [qw(to cc bcc)] => (
121 is => 'rw',
122 isa => 'Mail::Builder::Type::AddressList',
123 required => 1,
124 coerce => 1,
125 13 ▶ default => sub { return Mail::Builder::List->new( type => 'Mail::Builder::Address' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 8 );
127  
128 has 'attachment' => (
129 is => 'rw',
130 isa => 'Mail::Builder::Type::AttachmentList',
131 coerce => 1,
132 required => 1,
133 6 ▶ default => sub { Mail::Builder::List->new( type => 'Mail::Builder::Attachment' ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
134 8 );
135  
136 has 'image' => (
137 is => 'rw',
138 isa => 'Mail::Builder::Type::ImageList',
139 coerce => 1,
140 required => 1,
141 6 ▶ default => sub { Mail::Builder::List->new( type => 'Mail::Builder::Image' ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
142 8 );
143  
144 34 ▶sub _address_accessor {
Subroutine :around: called CC=5 SCAR=16.1
145 34 my ($method,$self,@params) = @_;
146  
147 34 ▶ if (scalar @params) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params) { } 8 26 34
148 8 ▶ if (scalar @params == 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 1 and builtin::blessed($params[0]) and $params[0]->isa('Mail::Builder::Address')) { } 1 7 8
Condition100%6/6
scalar @params == 1 and builtin::blessed($params[0]) and $params[0]->isa("Mail::Builder::Address")
!ll&&!rl&&r
611
scalar @params == 1 and builtin::blessed($params[0])
!ll&&!rl&&r
152
MC/DC100%3/3
scalar @params == 1 and builtin::blessed($params[0]) and $params[0]->isa("Mail::Builder::Address")
scalar @params == 1 builtin::blessed($params[0]) $params[0]->isa("Mail::Builder::Address")
Truth table100%4/4
A and B and C
A= scalar @params == 1B= builtin::blessed($params[0])C= $params[0]->isa("Mail::Builder::Address")
ABCresult
0XX0
10X0
1100
1111
149 && blessed($params[0])
150 && $params[0]->isa('Mail::Builder::Address')) {
151 1 return $method->($self,$params[0]);
152 } else {
153 7 return $method->($self,Mail::Builder::Address->new(@params));
154 }
155 } else {
156 26 return $method->($self);
157 }
158 }
159  
160 5 ▶sub _set_raw {
Subroutine _set_raw: called CC=1 SCAR=0.0
161 5 my ($self,$key,$value) = @_;
162 5 my $attr = __PACKAGE__->meta->get_attribute($key);
163 5 $attr->set_raw_value($self,$value);
164 5 return;
165 }
166  
167 1 ▶sub _generate_plaintext {
Subroutine _generate_plaintext: called CC=5 SCAR=17.8
168 1 my ($self,$htmltext) = @_;
169 1 ▶ $htmltext ||= $self->htmltext;
Condition33%1/3
$htmltext ||= $self->htmltext
l!l&&r!l&&!r
100
MC/DC0%0/2
$htmltext ||= $self->htmltext
$htmltext $self->htmltext
Truth table33%1/3
A ||= B
A= $htmltextB= $self->htmltext
ABresult
1X1
011
000
170  
171 1 ▶ if ($self->autotext
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autotext and $self->_plaintext_autotext == 1 || !$self->has_plaintext) 1 0 1
Condition33%2/6
$self->autotext and $self->_plaintext_autotext == 1 || !$self->has_plaintext
!ll&&!rl&&r
001
$self->_plaintext_autotext == 1 or not $self->has_plaintext
l!l&&r!l&&!r
010
MC/DC0%0/3
$self->autotext and $self->_plaintext_autotext == 1 || !$self->has_plaintext
$self->autotext $self->_plaintext_autotext == 1 not $self->has_plaintext
Truth table25%1/4
A and B or not(C)
A= $self->autotextB= $self->_plaintext_autotext == 1C= not $self->has_plaintext
ABCresult
0XX0
1000
11X1
1011
172 && ($self->_plaintext_autotext == 1 || ! $self->has_plaintext)) {
173 1 Class::Load::load_class('HTML::TreeBuilder');
174  
175 1 my $html_tree = HTML::TreeBuilder->new_from_content($self->htmltext);
176 # Only use the body
177 1 my $html_body = $html_tree->find('body');
178 # And now convert all elements
179 1 my $plaintext = $self->_convert_text($html_body);
180 # Store in plaintext accessor and set _plaintext_autotext flag
181 1 $self->_set_raw('plaintext',$plaintext);
182 1 $self->_plaintext_autotext(1);
183 }
184 1 return;
185 }
186  
187 2 ▶sub _set_autotext_plaintext {
Subroutine _set_autotext_plaintext: called CC=1 SCAR=0.0
188 2 my ($self,$plaintext) = @_;
189 2 $self->_plaintext_autotext(0);
190 2 return;
191 }
192  
193 26 ▶sub _convert_text {
Subroutine _convert_text: called CC=43 SCAR=41.6
194 26 my ($self,$html_element,$params) = @_;
195  
196 26 my $plain_text = q[];
197 26 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
215
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
198  
199 # Loop all children of the HTML element
200 26 foreach my $html_content ($html_element->content_list) {
201 # HTML element
202 44 ▶ if (ref($html_content)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html_content and $html_content->isa('HTML::Element')) { } 26 18 44
Condition66%2/3
ref $html_content and $html_content->isa("HTML::Element")
!ll&&!rl&&r
18026
MC/DC50%1/2
ref $html_content and $html_content->isa("HTML::Element")
ref $html_content $html_content->isa("HTML::Element")
Truth table66%2/3
A and B
A= ref $html_contentB= $html_content->isa("HTML::Element")
ABresult
0X0
100
111
203 && $html_content->isa('HTML::Element')) {
204 26 my $html_tagname = $html_content->tag;
205 26 ▶ if ($html_tagname eq 'i' || $html_tagname eq 'em') {
Branch: 15 branches
BranchTrueFalseTotal
if ($html_tagname eq 'i' or $html_tagname eq 'em') { } 1 25 26
elsif ($html_tagname =~ /^h\d$/) { } 1 24 25
elsif ($html_tagname eq 'strong' or $html_tagname eq 'b') { } 1 23 24
elsif ($html_tagname eq 'hr') { } 0 23 23
elsif ($html_tagname eq 'br') { } 0 23 23
elsif ($html_tagname eq 'ul' or $html_tagname eq 'ol') { } 2 21 23
elsif ($html_tagname eq 'div' or $html_tagname eq 'p') { } 2 19 21
elsif ($html_tagname eq 'table') { } 1 18 19
elsif ($html_tagname eq 'tr' and defined $params->{'table'}) { } 3 15 18
elsif ($html_tagname eq 'td' || $html_tagname eq 'th' and $params->{'tablerow'}) { } 8 7 15
elsif ($html_tagname eq 'img' and $html_content->attr('alt')) { } 1 6 7
elsif ($html_tagname eq 'a' and $html_content->attr('href')) { } 1 5 6
elsif ($html_tagname eq 'li') { } 4 1 5
elsif ($html_tagname eq 'pre') { } 0 1 1
elsif ($html_tagname eq 'head' or $html_tagname eq 'script' or $html_tagname eq 'frameset' or $html_tagname eq 'style') { } 0 1 1
Condition66%2/3
$html_tagname eq "i" or $html_tagname eq "em"
l!l&&r!l&&!r
0125
MC/DC50%1/2
$html_tagname eq "i" or $html_tagname eq "em"
$html_tagname eq "i" $html_tagname eq "em"
Truth table66%2/3
A or B
A= $html_tagname eq "i"B= $html_tagname eq "em"
ABresult
1X1
011
000
206 1 $plain_text .= '_'.$self->_convert_text($html_content,$params).'_';
207 } elsif ($html_tagname =~ m/^h\d$/) {
208 1 $plain_text .= '=='.$self->_convert_text($html_content,$params).qq[\n];
209 ▶ } elsif ($html_tagname eq 'strong' || $html_tagname eq 'b') {
Condition66%2/3
$html_tagname eq "strong" or $html_tagname eq "b"
l!l&&r!l&&!r
1023
MC/DC50%1/2
$html_tagname eq "strong" or $html_tagname eq "b"
$html_tagname eq "strong" $html_tagname eq "b"
Truth table66%2/3
A or B
A= $html_tagname eq "strong"B= $html_tagname eq "b"
ABresult
1X1
011
000
210 1 $plain_text .= '*'.$self->_convert_text($html_content,$params).'*';
211 } elsif ($html_tagname eq 'hr') {
212 0 $plain_text .= qq[\n---------------------------------------------------------\n];
213 } elsif ($html_tagname eq 'br') {
214 0 $plain_text .= qq[\n];
215 ▶ } elsif ($html_tagname eq 'ul' || $html_tagname eq 'ol') {
Condition100%3/3
$html_tagname eq "ul" or $html_tagname eq "ol"
l!l&&r!l&&!r
1121
MC/DC100%2/2
$html_tagname eq "ul" or $html_tagname eq "ol"
$html_tagname eq "ul" $html_tagname eq "ol"
Truth table100%3/3
A or B
A= $html_tagname eq "ul"B= $html_tagname eq "ol"
ABresult
1X1
011
000
216 2 my $count_old = $params->{count};
217 2 ▶ $params->{count} = ($html_tagname eq 'ol') ? 1:'*';
Branch: 1 branch
BranchTrueFalseTotal
$html_tagname eq 'ol' ? : 1 1 2
218 2 $plain_text .= qq[\n].$self->_convert_text($html_content,$params).qq[\n\n];
219 2 ▶ if (defined $count_old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count_old) { } 0 2 2
220 0 $params->{count} = $count_old;
221 } else {
222 2 delete $params->{count};
223 }
224 ▶ } elsif ($html_tagname eq 'div' || $html_tagname eq 'p') {
Condition66%2/3
$html_tagname eq "div" or $html_tagname eq "p"
l!l&&r!l&&!r
0219
MC/DC50%1/2
$html_tagname eq "div" or $html_tagname eq "p"
$html_tagname eq "div" $html_tagname eq "p"
Truth table66%2/3
A or B
A= $html_tagname eq "div"B= $html_tagname eq "p"
ABresult
1X1
011
000
225 2 $plain_text .= $self->_convert_text($html_content,$params).qq[\n\n];
226 } elsif ($html_tagname eq 'table') {
227 1 require Text::Table; # Load Text::Table lazily
228  
229 1 my $table_old = $params->{table};
230 1 $params->{table} = Text::Table->new();
231 1 $self->_convert_text($html_content,$params);
232 1 $params->{table}->body_rule('-','+');
233 1 $params->{table}->rule('-','+');
234 1 $plain_text .= qq[\n].$params->{table}->rule('-').$params->{table}.$params->{table}->rule('-').qq[\n];
235 1 ▶ if (defined $table_old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table_old) { } 0 1 1
236 0 $params->{table} = $table_old;
237 } else {
238 1 delete $params->{table};
239 }
240 ▶ } elsif ($html_tagname eq 'tr'
Condition66%2/3
$html_tagname eq "tr" and defined $params->{"table"}
!ll&&!rl&&r
1503
MC/DC50%1/2
$html_tagname eq "tr" and defined $params->{"table"}
$html_tagname eq "tr" defined $params->{"table"}
Truth table66%2/3
A and B
A= $html_tagname eq "tr"B= defined $params->{"table"}
ABresult
0X0
100
111
241 && defined $params->{table}) {
242 3 my $tablerow_old = $params->{tablerow};
243 3 $params->{tablerow} = [];
244 3 $self->_convert_text($html_content,$params);
245 3 $params->{table}->add(@{$params->{tablerow}});
246 3 ▶ if (defined $tablerow_old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tablerow_old) { } 0 3 3
247 0 $params->{tablerow} = $tablerow_old;
248 } else {
249 3 delete $params->{tablerow};
250 }
251 ▶ } elsif (($html_tagname eq 'td' || $html_tagname eq 'th') && $params->{tablerow}) {
Condition66%4/6
$html_tagname eq "td" || $html_tagname eq "th" and $params->{"tablerow"}
!ll&&!rl&&r
708
$html_tagname eq "td" or $html_tagname eq "th"
l!l&&r!l&&!r
807
MC/DC33%1/3
$html_tagname eq "td" || $html_tagname eq "th" and $params->{"tablerow"}
$html_tagname eq "td" $html_tagname eq "th" $params->{"tablerow"}
Truth table40%2/5
A or B and C
A= $html_tagname eq "td"B= $html_tagname eq "th"C= $params->{"tablerow"}
ABCresult
00X0
1X00
0100
1X11
0111
252 8 push @{$params->{tablerow}},$self->_convert_text($html_content,$params);
253 8 ▶ if ($html_content->attr('colspan')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html_content->attr("colspan")) 1 7 8
254 1 ▶ my $colspan = $html_content->attr('colspan') || 1;
Condition50%1/2
$html_content->attr('colspan') || 1
l!l
10
MC/DC0%0/1
$html_content->attr('colspan') || 1
$html_content->attr('colspan')
Truth table50%1/2
A
A= $html_content->attr('colspan')
Aresult
11
00
255 1 $colspan --;
256 1 push @{$params->{tablerow}},''
257 1 for (1..$colspan);
258 }
259 ▶ } elsif ($html_tagname eq 'img' && $html_content->attr('alt')) {
Condition66%2/3
$html_tagname eq "img" and $html_content->attr("alt")
!ll&&!rl&&r
601
MC/DC50%1/2
$html_tagname eq "img" and $html_content->attr("alt")
$html_tagname eq "img" $html_content->attr("alt")
Truth table66%2/3
A and B
A= $html_tagname eq "img"B= $html_content->attr("alt")
ABresult
0X0
100
111
260 1 $plain_text .= '['.$html_content->attr('alt').']';
261 ▶ } elsif ($html_tagname eq 'a' && $html_content->attr('href')) {
Condition66%2/3
$html_tagname eq "a" and $html_content->attr("href")
!ll&&!rl&&r
501
MC/DC50%1/2
$html_tagname eq "a" and $html_content->attr("href")
$html_tagname eq "a" $html_content->attr("href")
Truth table66%2/3
A and B
A= $html_tagname eq "a"B= $html_content->attr("href")
ABresult
0X0
100
111
262 1 $plain_text .= '['.$html_content->attr('href').' '.$self->_convert_text($html_content,$params).']';
263 } elsif ($html_tagname eq 'li') {
264 4 $plain_text .= qq[\n\t];
265 4 ▶ $params->{count} ||= '*';
Condition50%1/2
$params->{"count"} ||= "*"
l!l
40
MC/DC0%0/1
$params->{"count"} ||= "*"
$params->{"count"}
Truth table50%1/2
A
A= $params->{"count"}
Aresult
11
00
266 4 ▶ if ($params->{count} eq '*') {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'count'} eq '*') { } 2 2 4
elsif ($params->{'count'} =~ /^\d+$/) { } 2 0 2
267 2 $plain_text .= '*';
268 } elsif ($params->{count} =~ /^\d+$/) {
269 2 $plain_text .= $params->{count}.'.';
270 2 $params->{count} ++;
271 }
272 4 $plain_text .= q[ ].$self->_convert_text($html_content);
273 } elsif ($html_tagname eq 'pre') {
274 0 $params->{pre} = 1;
275 0 $plain_text .= qq[\n].$self->_convert_text($html_content,$params).qq[\n\n];
276 0 delete $params->{pre};
277 ▶ } elsif ($html_tagname eq 'head'
Condition33%3/9
$html_tagname eq "head" or $html_tagname eq "script" or $html_tagname eq "frameset" or $html_tagname eq "style"
l!l&&r!l&&!r
001
$html_tagname eq "head" or $html_tagname eq "script" or $html_tagname eq "frameset"
l!l&&r!l&&!r
001
$html_tagname eq "head" or $html_tagname eq "script"
l!l&&r!l&&!r
001
MC/DC0%0/4
$html_tagname eq "head" or $html_tagname eq "script" or $html_tagname eq "frameset" or $html_tagname eq "style"
$html_tagname eq "head" $html_tagname eq "script" $html_tagname eq "frameset" $html_tagname eq "style"
Truth table20%1/5
A or B or C or D
A= $html_tagname eq "head"B= $html_tagname eq "script"C= $html_tagname eq "frameset"D= $html_tagname eq "style"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
278 || $html_tagname eq 'script'
279 || $html_tagname eq 'frameset'
280 || $html_tagname eq 'style') {
281 0 next;
282 } else {
283 1 $plain_text .= $self->_convert_text($html_content,$params);
284 }
285 # CDATA
286 } else {
287 18 ▶ unless ($params->{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"pre"}) 18 0 18
288 18 $html_element =~ s/(\n|\n)//g;
289 18 $html_element =~ s/(\t|\n)/ /g;
290 }
291 18 $plain_text .= $html_content;
292 }
293 }
294  
295 26 return $plain_text;
296 }
297  
298 3 ▶sub _get_boundary {
Subroutine _get_boundary: called CC=1 SCAR=0.0
299 3 my ($self) = @_;
300 3 my $boundary = $self->_boundary( $self->_boundary + 1 );
301 3 return sprintf('----_=_NextPart_%04i_%lx',$boundary,time);
302 }
303  
304 0 ▶sub charset {
Subroutine charset: not called CC=1 SCAR=6.9
Pod undocumented
305 0 warn('DEPRECATED: The charset accessor has been removed.');
306 0 return;
307 }
308  
309 0 ▶sub purge_cache {
Subroutine purge_cache: not called CC=4 SCAR=30.0
Pod documented
310 0 my ($self) = @_;
311  
312 0 $self->clear_messageid;
313 0 $self->clear_date;
314  
315 # Remove plaintext if it has been derived form htmltext
316 0 ▶ if ($self->_plaintext_autotext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_plaintext_autotext) 0 0 0
317 0 $self->clear_plaintext;
318 }
319  
320 # Empty attachment images
321 0 foreach my $list (qw(attachment image)) {
322 0 foreach my $element ($self->$list->list) {
323 0 $element->clear_cache;
324 }
325 }
326 0 return 1;
327 }
328  
329 4 ▶sub _build_text {
Subroutine _build_text: called CC=6 SCAR=20.1
330 4 my ($self,%mime_params) = @_;
331  
332 # Build plaintext message from HTML
333 4 ▶ if ($self->has_htmltext
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_htmltext and not $self->has_plaintext and $self->autotext) 0 4 4
Condition50%3/6
$self->has_htmltext and not $self->has_plaintext and $self->autotext
!ll&&!rl&&r
400
$self->has_htmltext and not $self->has_plaintext
!ll&&!rl&&r
220
MC/DC0%0/3
$self->has_htmltext and not $self->has_plaintext and $self->autotext
$self->has_htmltext not $self->has_plaintext $self->autotext
Truth table50%2/4
A and not(B) and C
A= $self->has_htmltextB= not $self->has_plaintextC= $self->autotext
ABCresult
0XX0
10X0
1100
1111
334 && ! $self->has_plaintext
335 && $self->autotext) {
336 # Parse HTML tree. Load HTML::TreeBuilder lazily
337 0 require HTML::TreeBuilder;
338  
339 0 my $html_tree = HTML::TreeBuilder->new_from_content($self->html_text);
340 # Only use the body
341 0 my $html_body = $html_tree->find('body');
342 # And now convert all elements
343 0 $self->plaintext(_convert_text($html_body));
344 }
345  
346 4 my $mime_part;
347  
348 # We have HTML and plaintext
349 4 ▶ if ($self->has_htmltext
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_htmltext and $self->has_plaintext) { } 2 2 4
Condition66%2/3
$self->has_htmltext and $self->has_plaintext
!ll&&!rl&&r
202
MC/DC50%1/2
$self->has_htmltext and $self->has_plaintext
$self->has_htmltext $self->has_plaintext
Truth table66%2/3
A and B
A= $self->has_htmltextB= $self->has_plaintext
ABresult
0X0
100
111
350 && $self->has_plaintext) {
351  
352 # Build multipart/alternative envelope for HTML and plaintext
353 2 $mime_part = MIME::Entity->build(
354 %mime_params,
355 Type => q[multipart/alternative],
356 Boundary => $self->_get_boundary(),
357 Encoding => 'binary',
358 );
359  
360 # Add the plaintext entity first
361 2 $mime_part->add_part(MIME::Entity->build(
362 Top => 0,
363 Type => qq[text/plain; charset="utf-8"],
364 Data => $self->plaintext,
365 Encoding => 'quoted-printable',
366 ));
367  
368 # Add the html entity (the last entity is prefered in multipart/alternative context)
369 2 $mime_part->add_part($self->_build_html(Top => 0));
370 # We only have plaintext
371 } else {
372 2 $mime_part = MIME::Entity->build(
373 %mime_params,
374 Type => qq[text/plain; charset="utf-8"],
375 Data => $self->plaintext,
376 Encoding => 'quoted-printable',
377 );
378 }
379  
380 4 return $mime_part;
381 }
382  
383 2 ▶sub _build_html {
Subroutine _build_html: called CC=3 SCAR=13.5
384 2 my ($self,%mime_params) = @_;
385  
386 2 my $mime_part;
387  
388 # We have inline images
389 2 ▶ if ($self->image->length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->image->length) { } 0 2 2
390 # So we need a multipart/related envelope first
391 0 $mime_part = MIME::Entity->build(
392 %mime_params,
393 Type => q[multipart/related],
394 Boundary => $self->_get_boundary(),
395 Encoding => 'binary',
396 );
397 # Add the html body
398 0 $mime_part->add_part(MIME::Entity->build(
399 Top => 0,
400 Type => qq[text/html; charset="utf-8"],
401 Data => $self->htmltext,
402 Encoding => 'quoted-printable',
403 ));
404 # And now all the inline images
405 0 foreach ($self->image->list) {
406 0 $mime_part->add_part($_->serialize);
407 }
408 # We don't have any inline images
409 } else {
410 2 $mime_part = MIME::Entity->build(
411 %mime_params,
412 Type => qq[text/html; charset="utf-8"],
413 Data => $self->htmltext,
414 Encoding => 'quoted-printable',
415 );
416 }
417 2 return $mime_part;
418 }
419  
420 2 ▶sub _build_date {
Subroutine _build_date: called CC=1 SCAR=0.0
421 2 my ($self) = @_;
422 2 Class::Load::load_class('Email::Date::Format');
423 2 return Email::Date::Format::email_date();
424 }
425  
426  
427 8 ▶sub build_message {
Subroutine build_message: called CC=14 SCAR=26.4
Pod documented
428 8 my ($self) = @_;
429  
430 8 ▶ croak(q[Recipient address missing])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->to->length 1 7 8
431 unless ($self->to->length());
432 7 ▶ croak(q[From address missing])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_from 1 6 7
433 unless ($self->has_from);
434 6 ▶ croak(q[e-mail subject missing])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_subject 1 5 6
435 unless ($self->has_subject);
436 5 ▶ croak(q[e-mail content missing])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_plaintext or $self->has_htmltext 1 4 5
Condition66%2/3
$self->has_plaintext or $self->has_htmltext
l!l&&r!l&&!r
401
MC/DC50%1/2
$self->has_plaintext or $self->has_htmltext
$self->has_plaintext $self->has_htmltext
Truth table66%2/3
A or B
A= $self->has_plaintextB= $self->has_htmltext
ABresult
1X1
011
000
437 unless ($self->has_plaintext || $self->has_htmltext);
438  
439 4 my $messageid = Email::MessageID->new;
440 4 $self->_set_raw('messageid',$messageid);
441  
442 # Set header fields
443 4 my %email_header = (
444 'Top' => 1,
445 'From' => $self->from->serialize,
446 'To' => $self->to->join(','),
447 'Cc' => $self->cc->join(','),
448 'Bcc' => $self->bcc->join(','),
449 'Date' => $self->date,
450 'Subject' => Mail::Builder::Utils::encode_mime($self->subject),
451 'Message-ID' => $messageid->in_brackets(),
452 'X-Priority' => $self->priority,
453 'X-Mailer' => Mail::Builder::Utils::encode_mime($self->mailer),
454 );
455  
456 # Set reply address
457 4 ▶ if ($self->has_reply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reply) 3 1 4
458 3 $email_header{'Reply-To'} = $self->reply->serialize;
459 }
460  
461 # Set sender address
462 4 ▶ if ($self->has_sender) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sender) 3 1 4
463 3 $email_header{'Sender'} = $self->sender->serialize;
464 }
465  
466 # Set language
467 4 ▶ if ($self->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_language) 3 1 4
468 3 $email_header{'Content-language'} = $self->language;
469 }
470  
471 # Set return path
472 4 ▶ if ($self->has_returnpath) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_returnpath) { } 2 2 4
elsif ($self->has_reply) { } 1 1 2
473 2 $email_header{'Return-Path'} = $self->returnpath->address();
474 } elsif ($self->has_reply) {
475 1 $email_header{'Return-Path'} = $self->reply->address();
476 } else {
477 1 $email_header{'Return-Path'} = $self->from->address();
478 }
479  
480 # Set organizsation
481 4 ▶ if ($self->has_organization) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_organization) 3 1 4
482 3 $email_header{'Organization'} = Mail::Builder::Utils::encode_mime($self->organization);
483 }
484  
485 # Build e-mail entity ...
486 4 my $mime_entity;
487  
488 # ... with attachments
489 4 ▶ if ($self->attachment->length()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->attachment->length) { } 1 3 4
490 1 $mime_entity = MIME::Entity->build(
491 %email_header,
492 Type => 'multipart/mixed',
493 Boundary => $self->_get_boundary(),
494 Encoding => 'binary',
495 );
496 1 $mime_entity->add_part($self->_build_text(Top => 0));
497 1 foreach my $attachment ($self->attachment->list()) {
498 1 $mime_entity->add_part($attachment->serialize());
499 }
500 # ... without attachments
501 } else {
502 3 $mime_entity = $self->_build_text(%email_header);
503 }
504  
505 4 return $mime_entity;
506 }
507  
508 1 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
509 1 my $obj = shift;
510 1 return $obj->build_message->stringify;
511 }
512  
513 8 __PACKAGE__->meta->make_immutable;
514  
515 =encoding utf8
516  
517 =head1 NAME
518  
519 Mail::Builder - Easily create plaintext/html e-mail messages with attachments
520 and inline images
521  
522 =head1 SYNOPSIS
523  
524 use Mail::Builder;
525
526 my $mail = Mail::Builder->new({
527 subject => 'Party at Sam\'s place',
528 from => 'mightypirate@meele-island.mq',
529 htmltext => '<h1>Party invitation</h1> ... ',
530 attachment=> '/path/to/direction_samandmax.pdf',
531 });
532
533 # Alter from name
534 $mail->from->name('Guybrush Threepwood');
535 # Set recipent
536 $mail->to('manuel.calavera@dod.mx','Manuel Calavera');
537 # Add one more recipient
538 $mail->to->add('glotis@dod.mx');
539
540 # Send it with your favourite module (e.g. Email::Send)
541 my $mailer = Email::Send->new({mailer => 'Sendmail'})->send($mail->stringify);
542
543 # Or mess with MIME::Entity objects
544 my $mime = $mail->build_message;
545 $mime-> ....
546  
547 =head1 DESCRIPTION
548  
549 This module helps you to build correct e-mails with attachments, inline
550 images, multiple recipients, ... without having to worry about the underlying
551 MIME and encoding issues. Mail::Builder relies heavily on the L<MIME::Entity>
552 module from the L<MIME::Tools> distribution.
553  
554 The module will create the correct MIME bodies, headers and containers
555 (multipart/mixed, multipart/related, multipart/alternative) depending on if
556 you use attachments, HTML text and inline images.
557  
558 Furthermore it will encode non-ascii header data and autogenerate plaintext
559 messages (if you don't provide it yourself or disable the L<autotext> option)
560 from html content.
561  
562 Addresses, attachments and inline images are handled as objects by helper
563 classes:
564  
565 =over
566  
567 =item * L<Mail::Builder::Address>
568  
569 Stores an e-mail address and a display name.
570  
571 =item * Attachments: L<Mail::Builder::Attachment>
572  
573 This class manages attachments which can be created either from files in the
574 filesystem, filehandles or from data in memory.
575  
576 =item * Inline images:L<Mail::Builder::Image>
577  
578 The Mail::Builder::Image class manages images that should be displayed in the
579 html e-mail body. (E<lt>img src="cid:imageid" /E<gt>)
580  
581 =item * L<Mail::Builder::List>
582  
583 Helper class for handling list of varoius items (i.e. recipient lists,
584 attachment lists and image lists)
585  
586 =back
587  
588 =head1 METHODS
589  
590 =head2 Constructors
591  
592 =head3 new
593  
594 This is a simple constructor. It accepts all defined acccessors as a Hash or
595 HashRef.
596  
597 =head2 Public methods
598  
599 =head3 stringify
600  
601 Returns the e-mail message as a string. This string can be passed to modules
602 like L<Email::Send>.
603  
604 This method is just a shortcut to C<$mb-E<gt>build_message-E<gt>stringify>
605  
606 =head3 build_message
607  
608 my $entity = $mb->build_message();
609
610 # Print the entire message:
611 $entity->print(\*STDOUT);
612
613 # Stringify the entire message:
614 print $entity->stringify;
615  
616 Returns the e-mail message as a L<MIME::Entity> object. You can mess around
617 with the object, change parts, ... as you wish.
618  
619 Every time you call build_message the MIME::Entity object will be created,
620 which can take some time if you are sending bulk e-mails. In
621 order to increase the processing speed L<Mail::Builder::Attachment> and
622 L<Mail::Builder::Image> entities will be cached and only rebuilt if something
623 has changed.
624  
625 Each call to this method also changes the C<messageid>.
626  
627 =head3 purge_cache
628  
629 Emptys the attachment and image cache and removes the plaintext text if it has
630 been autogenerated from html text. Also resets the Message ID and date header.
631  
632 =head2 Accessors
633  
634 =head3 from, returnpath, reply, sender
635  
636 These accessors set/return the from, sender and reply address as well as the
637 returnpath for bounced messages.
638  
639 $obj->from(EMAIL[,NAME[,COMMENT]])
640 OR
641 $obj->from(Mail::Builder::Address)
642 OR
643 $obj->from(Email::Address)
644 OR
645 $obj->from({
646 email => EMAIL,
647 [name => NAME,]
648 [comment => COMMENT,]
649 });
650  
651 This accessor always returns a Mail::Builder::Address object.
652  
653 To change the attribute value you can either supply
654  
655 =over
656  
657 =item * a L<Mail::Builder::Address> object
658  
659 =item * an L<Email::Address> object
660  
661 =item * a list containing an e-mail address and optionally name and comment
662  
663 =item * a HashRef that will be passed to the L<Mail::Builder::Address>
664 constructor (Keys: email, name and comment)
665  
666 =back
667  
668 The presence of a value can be checked with the C<has_from>, C<has_reply>,
669 C<has_reply> and C<has_sender> methods.
670  
671 Values can be cleared with the C<clear_from>, C<clear_reply>,
672 C<clear_reply> and C<clear_sender> methods.
673  
674 L<Email::Valid> options may be changed by setting the appropriate values
675 in the %Mail::Builder::TypeConstraints::EMAILVALID hash.
676  
677 Eg. if you want to disable the check for valid TLDs you can set the 'tldcheck'
678 option (without dashes 'tldcheck' and not '-tldcheck'):
679  
680 $Mail::Builder::TypeConstraints::EMAILVALID{tldcheck} = 0;
681  
682 =head3 to, cc, bcc
683  
684 $obj->to(Mail::Builder::List)
685 OR
686 $obj->to(Mail::Builder::Address)
687 OR
688 $obj->to(Email::Address)
689 OR
690 $obj->to(EMAIL[,NAME[,COMMENT]])
691 OR
692 $obj->to({
693 email => EMAIL,
694 [name => NAME,]
695 [comment => COMMENT,]
696 })
697 OR
698 $obj->to([
699 Mail::Builder::Address | Email::Address | HashRef | EMAIL
700 ])
701  
702 This accessor always returns a L<Mail::Builder::List> object containing
703 L<Mail::Builder::Address> objects.
704  
705 To alter the values you can either
706  
707 =over
708  
709 =item * Manipulate the L<Mail::Builder::List> object (add, remove, ...)
710  
711 =item * Supply a L<Mail::Builder::Address> object. This will reset the current
712 list and add the object to the list.
713  
714 =item * Supply a L<Mail::Builder::List> object. The list object replaces the
715 old one if the list types matches.
716  
717 =item * Scalar and HashRef values will be passed to
718 C<Mail::Builder::Address-E<gt>new>. The returned object will be added to the
719 object list.
720  
721 =item * Supply a L<Email::Address> object. This will reset the current
722 list, generate a L<Mail::Builder::Address> object and add it to the list.
723  
724 =back
725  
726 The L<Mail::Builder::List> package provides some basic methods for
727 manipulating the list of recipients. e.g.
728  
729 $obj->to->add(EMAIL[,NAME[,COMMENT]])
730 OR
731 $obj->to->add(Mail::Builder::Address)
732 OR
733 $obj->to->add(Email::Address)
734  
735 =head3 date
736  
737 e-mail date header. Accepts/returns a RFC2822 date string. Also accepts a
738 DateTime object or epoch integer. Will be autogenerated if not provided.
739 C<clear_date> can be used to reset the date accessor.
740  
741 =head3 language
742  
743 e-mail text language. Additionally the C<has_language> and C<clear_language>
744 methods can be used to check respectively clear the value.
745  
746 =head3 messageid
747  
748 Message ID of the e-mail as a L<Email::MessageID> object. Read only and
749 available only after the C<build_message> or C<stringify> methods have been
750 called. Each call to C<build_message> or C<stringify> changes the message ID.
751 The C<has_messageid> and C<clear_messageid> methods can be used to check
752 respectively clear the value.
753  
754 =head3 organization
755  
756 Accessor for the name of the sender's organisation. This header field is not
757 part of the RFC 4021, however supported by many mailer applications.
758 Additionally the C<has_organization> and C<clear_organization> methods can be
759 used to check or clear the value.
760  
761 =head3 priority
762  
763 Priority accessor. Accepts values from 1 to 5. The default priority is 3.
764  
765 =head3 subject
766  
767 e-mail subject accessor. Must be specified.
768  
769 =head3 htmltext
770  
771 HTML mail body accessor. Additionally the C<has_htmltext> and
772 C<clear_htmltext> methods can be used to check or clear the value.
773  
774 =head3 mailer
775  
776 Mailer name.
777  
778 =head3 plaintext
779  
780 Plaintext mail body accessor. The C<clear_plaintext> and C<has_plaintext>
781 methods can be used to check or clear the value.
782  
783 This text will be autogenerated from htmltext if not provided by the user
784 and the C<autotext> option is not turned off. Simple formating (e.g. <strong>,
785 <em>, ...) will be converted to pseudo formating.
786  
787 If you want to disable the autogeneration of plaintext parts set the
788 L<autotext> accessor to a false value. However be aware that most anti-spam
789 enginges tag html e-mail messages without a plaintext part as spam.
790  
791 The following html tags will be transformed to simple markup:
792  
793 =over
794  
795 =item * I, EM
796  
797 Italic text will be surrounded by underscores. (_italic text_)
798  
799 =item * H1, H2, H3, ...
800  
801 Two equal signs are prepended to headlines (== Headline)
802  
803 =item * STRONG, B
804  
805 Bold text will be marked by stars (*bold text*)
806  
807 =item * HR
808  
809 A horizontal rule is replaced with 60 dashes.
810  
811 =item * BR
812  
813 Single linebreak
814  
815 =item * P, DIV
816  
817 Two linebreaks
818  
819 =item * IMG
820  
821 Prints the alt text of the image (if any).
822  
823 =item * A
824  
825 Prints the link url surrounded by brackets ([http://myurl.com text])
826  
827 =item * UL, OL
828  
829 All list items will be indented with a tab and prefixed with a start
830 (*) or an index number.
831  
832 =item * TABLE, TR, TD, TH
833  
834 Tables are converted into text using L<Text::Table>.
835  
836 =back
837  
838 =head3 autotext
839  
840 Enables the autogeneration of plaintext parts from HTML.
841  
842 Default TRUE.
843  
844 =head3 attachment
845  
846 $obj->attachment(Mail::Builder::List)
847 OR
848 $obj->attachment(Mail::Builder::Attachment)
849 OR
850 $obj->attachment(PATH | Path::Class::File | FH | IO::File | SCALARREF)
851 OR
852 $obj->attachment({
853 file => PATH | Path::Class::File | FH | IO::File | SCALARREF,
854 [name => NAME,]
855 [mimetype => MIME,]
856 })
857 OR
858 $obj->attachment([
859 HashRef | Mail::Builder::Attachment | PATH | Path::Class::File | FH | IO::File | SCALARREF
860 ]);
861  
862 This accessor always returns a Mail::Builder::List object. If you supply
863 a L<Mail::Builder::List> the list will be replaced.
864  
865 If you pass a Mail::Builder::Attachment object, a filehandle, an IO::File
866 object, a L<Path::Class::File> object, a path or a scalar reference
867 or a scalar path the current list will be reset and the new
868 attachment will be added.
869  
870 The L<Mail::Builder::List> package provides some basic methods for
871 manipulating the list of attachments.
872  
873 If you want to append an additional attachment to the list use
874  
875 $obj->attachment->add(PATH | Path::Class::File | FH | IO::File | ScalarRef)
876 OR
877 $obj->attachment->add({
878 file => PATH | Path::Class::File | FH | IO::File | ScalarRef,
879 [name => NAME,]
880 [mimetype => MIME,]
881 })
882 OR
883 $obj->attachment->add(Mail::Builder::Attachment)
884  
885 =head3 image
886  
887 $obj->image(Mail::Builder::List)
888 OR
889 $obj->image(Mail::Builder::Image)
890 OR
891 $obj->image(PATH | Path::Class::File | FH | IO::File | ScalarRef)
892 OR
893 $obj->image({
894 file => PATH | Path::Class::File | FH | IO::File | ScalarRef,
895 [id => ID,]
896 [mimetype => MIME,]
897 })
898 OR
899 $obj->image([
900 HashRef | Mail::Builder::Image | PATH | Path::Class::File | FH | IO::File | ScalarRef
901 ]);
902  
903 This accessor always returns a Mail::Builder::List object. If you supply
904 a L<Mail::Builder::List> the list will be replaced.
905  
906 If you pass a Mail::Builder::Image object or a scalar path (with an
907 optional id) the current list will be reset and the new image will be added.
908  
909 The L<Mail::Builder::List> package provides some basic methods for
910 manipulating the list of inline images.
911  
912 If you want to append an additional attachment to the list use
913  
914 $obj->image->add(PATH | Path::Class::File | FH | IO::File | ScalarRef)
915 OR
916 $obj->image->add(Mail::Builder::Image)
917  
918 You can embed the image into the html mail body code by referencing the ID. If
919 you don't provide an ID the lowercase filename without the file extension will
920 be used as the ID.
921  
922 <img src="cid:logo"/>
923  
924 Only jpg, gif and png images may be added as inline images.
925  
926 =head1 EXAMPLE
927  
928 If you want to send multiple e-mail messages from one Mail::Builder object
929 (e.g. a solicited mailing to multiple recipients) you have to pay special
930 attention, or else you might end up with growing recipients lists.
931  
932 # Example for a mass mailing
933 foreach my $recipient (@recipients) {
934 $mb->to->reset; # Remove all recipients
935 $mb->to->add($recipient); # Add current recipient
936
937 # Alternatively you could use $mb->to($recipient); which has the
938 # same effect as the two previous commands. Same applies to 'cc' and 'bcc'
939
940 # Autogenerated Plaintext will be reset
941 $mb->htmltext(qq[<h1>Hello $recipient!</h1> Text, yadda yadda! ]);
942
943 # Plaintext is autogenerated
944 my $mail = $mb->stringify();
945
946 # Send $mail ...
947 }
948  
949 =head1 IMPORTANT CHANGES
950  
951 From 1.10 on Mail::Builder only supports utf-8 charsets for mails. Supporting
952 multiple encodings turned out to be error prone and not necessary since all
953 modern mail clients support utf-8.
954  
955 Starting with Mail::Builder 2.0 the Mail::Builder::Attachment::* and
956 Mail::Builder::Image::* classes have been deprecated. Use the base classes
957 Mail::Builder::Attachment and Mail::Builder::Image instead.
958  
959 =head1 CAVEATS
960  
961 Watch out when sending Mail::Builder generated mails with
962 L<Email::Send::SMTP>: The 'Return-Path' headers are ignored by the MTA
963 since L<Email::Send::SMTP> uses the 'From' header for SMTP handshake. Postfix
964 (any maybe some other MTAs) overwrites the 'Return-Path' field in the data
965 with the e-mail used in the handshake ('From'). The behaviour of
966 L<Email::Send::SMTP> may however be modified by replacing the
967 C<get_env_sender> and C<get_env_recipients> methods. See L<Email::Send::SMTP>
968 for more details.
969  
970 =head1 SUPPORT
971  
972 Please report any bugs or feature requests to
973 C<bug-mail-builder@rt.cpan.org>, or through the web interface at
974 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Mail::Builder>.
975 I will be notified, and then you'll automatically be notified of progress on
976 your report as I make changes.
977  
978 =head1 AUTHOR
979  
980 Maroš Kollár
981 CPAN ID: MAROS
982 maros [at] k-1.com
983 http://www.k-1.com
984  
985 =head1 COPYRIGHT
986  
987 Mail::Builder is Copyright (c) 2007-2010 Maroš Kollár.
988  
989 This program is free software; you can redistribute it and/or modify it under
990 the same terms as Perl itself as long it is not used for sending
991 unsolicited mail (SPAM):
992  
993 "Thou shalt not send SPAM with this module."
994  
995 The full text of the license can be found in the
996 LICENSE file included with this module.
997  
998 =head1 SEE ALSO
999  
1000 The L<Mime::Entity> module in the L<Mime::Tools> distribution.
1001  
1002 L<Mail::Builder::Simple> provides a wrapper around this module and itegrates
1003 it with L<Email::Sender::Simple>
1004  
1005 Furthermore these modules are being used for various tasks:
1006  
1007 =over
1008  
1009 =item * L<Email::Valid> for validating e-mail addresses
1010  
1011 =item * L<Email::MessageID> for generating unique message IDs
1012  
1013 =item * L<HTML::TreeBuilder> for parsing html and generating plaintext
1014  
1015 =item * L<MIME::Types> for guessing attachment mime types
1016  
1017 =back
1018  
1019 =cut
1020  
1021 8 1;
↑ Summary Builder/Role/File.pm »