lib/Petal/Mail.pm

filter: Statementstmt 99.3% 148 / 149 Branchbran 90.0% 36 / 40 Conditioncond 83.3% 25 / 30 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 92.6% 238 / 257 CC 32 SCAR 13.2
CC
32
Cov
95%
send
7.1
__Body_Process
6.0
_soft_wrap
5.0
CRAP 3.8
↑ Summary fake_sendmail.pl »
1 =head1 NAME
2  
3 Petal::Mail - Format text e-mail using Petal
4  
5  
6 =head1 SYNOPSIS
7  
8 use Petal::Mail;
9 use Petal::Mail;
10 use Petal::Mail;
11 use Petal::Mail;
12 my $petal_mail = new Petal::Mail ('email.xml');
13 my $text_mail = $petal_mail->process (%args);
14  
15  
16 =head1 SUMMARY
17  
18 L<Petal::Mail> processes a Petal XML template, and then turns the resulting XML
19 into a text email which can be sent through sendmail or other. The XML has to
20 follow a certain syntax which is defined in this documentation.
21  
22 Since L<Petal::Mail>'s is a subclass of Petal, its API is the same. Which
23 means you need to read about L<Petal> before you can use L<Petal::Mail>.
24  
25 =cut
26 package Petal::Mail;
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use MKDoc::XML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use MKDoc::XML::Decode;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use base qw /Petal/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 =head1 GLOBAL / LOCAL VARIABLES
36  
37 =head2 local $Petal::Mail::Line_Wrap = 68;
38  
39 Amounts of characters allowed before text-flowed wrapping.
40  
41 =head2 local $Petal::Mail::Indent = 4;
42  
43 Amounts of whitespace when indenting <dd> tags
44  
45 =head2 local $Petal::Mail::Sendmail = '/usr/sbin/sendmail -t';
46  
47 If you set this variable to your sendmail executable, and make sure the
48 $ENV{SERVER_ADMIN} is set to a proper email address for processing bounces,
49 then you can use the send() method instead of the process() method and
50 Petal::Mail will send the email once it's been created.
51  
52 =cut
53 3 our $Line_Wrap = 68;
54 3 our $DD_Indent = 4;
55 3 our $Sendmail = '/usr/sbin/sendmail -t';
56  
57  
58 # can't touch those
59 3 our $Decode = new MKDoc::XML::Decode qw/xml xhtml numeric/;
60  
61 =head1 FUNCTIONS
62  
63 =head2 process
64  
65 This function processes a template. It takes a hash or hashref which is used
66 to fill out any elements in the template. It returns the processed template as
67 a string. See L<Petal> for further details.
68  
69 =cut
70 3 our $VERSION = 0.31;
71  
72 =head1 FUNCTIONS
73  
74 =head2 process
75  
76 This function processes a template. It takes a hash or hashref which is used
77 to fill out any elements in the template. It returns the processed template as
78 a string. See L<Petal> for further details.
79 =head2 send
80  
81 This function processes a template and sends an email message according to the
82 headers in the template. It takes the same parameters as process in addition
83 to the email address of the authorized sender. The authorized sender may also
84 be set with the environment parameter 'SERVER_ADMIN'. Returns null on success
85 or dies on failure.
86  
87 $petal_mail->send(AUTH_SENDER => 'lewis@carroll.net', %args);
88  
89 =cut
90  
91 =cut
92  
93 =head1 FUNCTIONS
94  
95 =head2 process
96  
97 This function processes a template. It takes a hash or hashref which is used
98 to fill out any elements in the template. It returns the processed template as
99 a string. See L<Petal> for further details.
100 =head2 send
101  
102 This function processes and sends an email message according to a template.
103 It takes the same parameters as process. Returns null(?) on success or dies on
104 failure.
105  
106 =cut
107  
108 =head1 FUNCTIONS
109  
110 =head2 process
111  
112 This function processes a template. It takes a hash or hashref which is used
113 to fill out any elements in the template. It returns the processed template as
114 a string. See L<Petal> for further details.
115 =head2 send
116  
117 This function processes and sends an email message according to a template.
118 It takes the same parameters as process. Returns null(?) on success or dies on
119 failure.
120  
121 =cut
122  
123 sub process
124 8 ▶{
Subroutine process: called CC=1 SCAR=0.0
Pod documented
125 8 my $self = shift;
126 8 my $xml = $self->SUPER::process (@_);
127 8 return $self->_xml_to_text ($xml);
128 }
129  
130  
131 =head2 send
132  
133 This function processes and sends an email message according to a template.
134 It takes the same parameters as process. Returns null(?) on success or dies on
135 failure.
136  
137 =cut
138 sub send
139 3 ▶{
Subroutine send: called CC=7 SCAR=19.6
Pod documented
140 3 my $self = shift;
141 3 my %args = @_;
142 3 ▶ my $authorized_sender = $args{'AUTH_SENDER'} || $ENV{SERVER_ADMIN} || '';
Condition100%5/5
$args{'AUTH_SENDER'} || $ENV{'SERVER_ADMIN'} || ''
l!l
21
$args{"AUTH_SENDER"} or $ENV{"SERVER_ADMIN"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$args{'AUTH_SENDER'} || $ENV{'SERVER_ADMIN'} || ''
$args{"AUTH_SENDER"} $ENV{"SERVER_ADMIN"}
Truth table100%3/3
A or B
A= $args{"AUTH_SENDER"}B= $ENV{"SERVER_ADMIN"}
ABresult
1X1
011
000
143 3 delete $args{'AUTH_SENDER'};
144 3 ▶ $authorized_sender || die 'No authorized sender defined and $ENV{SERVER_ADMIN} not set';
Branch: 1 branch
BranchTrueFalseTotal
$authorized_sender or die "No authorized sender defined and \$ENV{SERVER_ADMIN} not set" 2 1 3
145
146 2 ▶ my $mail = $self->process (%args) || die '$self->process (\@_) returned undef';
Condition50%1/2
$self->process(%args) || die('$self->process (\\@_) returned undef')
l!l
20
MC/DC0%0/1
$self->process(%args) || die('$self->process (\\@_) returned undef')
$self->process(%args)
Truth table50%1/2
A
A= $self->process(%args)
Aresult
11
00
147 2 ▶ open (SENDMAIL, "| $Sendmail -f $authorized_sender") || die "error opening sendmail [$Sendmail]: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SENDMAIL, "| $Petal::Mail::Sendmail -f $authorized_sender" or die "error opening sendmail [$Petal::Mail::Sendmail]: $!" 2 0 2
148 2 binmode (SENDMAIL, ":utf8");
149 2 ▶ print SENDMAIL $mail || die "error writing to sendmail [$Sendmail]: $!";
Condition50%1/2
$mail || die("error writing to sendmail [$Petal::Mail::Sendmail]: $!")
l!l
20
MC/DC0%0/1
$mail || die("error writing to sendmail [$Petal::Mail::Sendmail]: $!")
$mail
Truth table50%1/2
A
A= $mail
Aresult
11
00
150 2 close SENDMAIL;
151 }
152  
153  
154 sub _xml_to_text
155 8 ▶{
Subroutine _xml_to_text: called CC=3 SCAR=11.6
156 8 my $self = shift;
157 8 my $xml = shift;
158
159 8 my @nodes = MKDoc::XML::TreeBuilder->process_data ($xml);
160 my @result = map {
161 8 ▶ ((ref $_) and ($_->{_tag} eq 'Message')) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->{'_tag'} eq 'Message' ? : 8 0 8
Condition33%1/3
ref $_ and $_->{"_tag"} eq "Message"
!ll&&!rl&&r
008
MC/DC0%0/2
ref $_ and $_->{"_tag"} eq "Message"
ref $_ $_->{"_tag"} eq "Message"
Truth table33%1/3
A and B
A= ref $_B= $_->{"_tag"} eq "Message"
ABresult
0X0
100
111
162 $self->__Message ($_) : ()
163 } @nodes;
164
165 8 return join "\n", @result;
166 }
167  
168  
169 sub __Message
170 8 ▶{
Subroutine __Message: called CC=1 SCAR=0.0
171 8 my $self = shift;
172 8 my $node = shift;
173  
174 8 my $headers = $self->__Headers ($node);
175 8 my $body = $self->__Body ($node);
176 8 return join "\n\n", ($headers, $body);
177 }
178  
179  
180 sub __Headers
181 8 ▶{
Subroutine __Headers: called CC=4 SCAR=13.9
182 8 my $self = shift;
183 8 my $node = shift;
184 my @res = map {
185 196 ▶ (ref $_ and $_->{_tag} !~ /^body$/i) ? do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && !($_->{'_tag'} =~ /^body$/i) ? : 86 110 196
Condition100%3/3
ref $_ and not $_->{"_tag"} =~ /^body$/i
!ll&&!rl&&r
102886
MC/DC100%2/2
ref $_ and not $_->{"_tag"} =~ /^body$/i
ref $_ not $_->{"_tag"} =~ /^body$/i
Truth table100%3/3
A and not(B)
A= ref $_B= not $_->{"_tag"} =~ /^body$/i
ABresult
0X0
100
111
186 86 my $text = $self->__Content_To_Text ($_);
187 86 $text = $Decode->process ($text);
188 86 $text =~ s/\n/ /gsm;
189 86 $text = encode ('MIME-Header', $text);
190
191 86 my $tag = $_->{_tag};
192 86 ▶ ($tag =~ /^from$/i) ?
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /^from$/i ? : 8 78 86
193 ("$tag: $text", $self->__Headers_message_id ($text)) :
194 ("$tag: $text");
195 } : ()
196 8 } @{$node->{_content}};
197
198 8 my $res = join "\n", @res;
199 8 return $res;
200 }
201  
202  
203 sub __Headers_message_id
204 8 ▶{
Subroutine __Headers_message_id: called CC=1 SCAR=0.0
205 8 my $self = shift;
206 8 my $id = shift;
207 8 my $time = time();
208 8 my $rand = join '', map { chr (ord ('A') + int rand (26)) } 1..5;
209 8 $id =~ s/^.*<//;
210 8 $id =~ s/>.*$//;
211 8 $id =~ s/^\s+//;
212 8 $id =~ s/\s+$//;
213 8 $id =~ s/^.*\@//;
214 8 $id = "$time.$rand\@$id";
215 8 return qq |Message-ID: <$id>|;
216 }
217  
218  
219 sub __Content_To_Text
220 202 ▶{
Subroutine __Content_To_Text: called CC=2 SCAR=6.9
221 202 my $self = shift;
222 202 my $node = shift;
223 my @res = map {
224 204 ▶ (ref $_) ? ( $self->__Content_To_Text ($_) ) : ( $Decode->process ($_) )
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 203 204
225 202 } @{$node->{_content}};
226 202 return join '', @res;
227 }
228  
229  
230 sub __Body
231 8 ▶{
Subroutine __Body: called CC=3 SCAR=11.0
232 8 my $self = shift;
233 8 my $node = shift;
234 my @res = map {
235 196 ▶ (ref $_ and $_->{_tag} =~ /^body$/i) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->{'_tag'} =~ /^body$/i ? : 8 188 196
Condition100%3/3
ref $_ and $_->{"_tag"} =~ /^body$/i
!ll&&!rl&&r
102868
MC/DC100%2/2
ref $_ and $_->{"_tag"} =~ /^body$/i
ref $_ $_->{"_tag"} =~ /^body$/i
Truth table100%3/3
A and B
A= ref $_B= $_->{"_tag"} =~ /^body$/i
ABresult
0X0
100
111
236 $self->__Body_Process ($_) : ()
237 8 } @{$node->{_content}};
238  
239 8 my $res = join "\n", @res;
240 8 $res =~ s/^\n+//gs;
241 8 return $res;
242 }
243  
244  
245 sub __Body_Process
246 15 ▶{
Subroutine __Body_Process: called CC=6 SCAR=17.9
247 15 my $self = shift;
248 15 my $node = shift;
249
250 my @res = map {
251 259 my $node = $_;
252 $self->__Body_P ($node) ||
253 $self->__Body_DT ($node) ||
254 $self->__Body_DD ($node) ||
255 $self->__Body_PRE ($node) ||
256 259 ▶ do { ref ($_) ? ( $self->__Body_Process ($node) ) : () }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 7 137 144
Condition91%11/12
$self->__Body_P($node) || $self->__Body_DT($node) || $self->__Body_DD($node) || $self->__Body_PRE($node) || do { ref $_ ? $self->__Body_Process($node) : () }
l!l&&r!l&&!r
1151440
$self->__Body_P($node) or $self->__Body_DT($node) or $self->__Body_DD($node) or $self->__Body_PRE($node)
l!l&&r!l&&!r
1078144
$self->__Body_P($node) or $self->__Body_DT($node) or $self->__Body_DD($node)
l!l&&r!l&&!r
7928152
$self->__Body_P($node) or $self->__Body_DT($node)
l!l&&r!l&&!r
5128180
MC/DC0%0/5
$self->__Body_P($node) || $self->__Body_DT($node) || $self->__Body_DD($node) || $self->__Body_PRE($node) || do { ref $_ ? $self->__Body_Process($node) : () }
$self->__Body_P($node) $self->__Body_DT($node) $self->__Body_DD($node) $self->__Body_PRE($node) do { ref $_ ? $self->__Body_Process($node) : () }
Truth table83%5/6
A or B or C or D || E
A= $self->__Body_P($node)B= $self->__Body_DT($node)C= $self->__Body_DD($node)D= $self->__Body_PRE($node)E= do { ref $_ ? $self->__Body_Process($node) : () }
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
257 15 } @{$node->{_content}};
258
259 15 my $res = join '', @res;
260 15 return $res;
261 }
262  
263  
264 sub __Body_PRE
265 152 ▶{
Subroutine __Body_PRE: called CC=3 SCAR=11.0
266 152 my $self = shift;
267 152 my $node = shift;
268
269 152 ▶ ref $node || return;
Branch: 1 branch
BranchTrueFalseTotal
ref $node or return 15 137 152
270 15 ▶ return unless ($node->{_tag} =~ /^pre$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"_tag"} =~ /^pre$/i 7 8 15
271
272 8 my $text = $self->__Content_To_Text ($node);
273 8 return "\n\n$text";
274 }
275  
276  
277 sub __Body_P
278 287 ▶{
Subroutine __Body_P: called CC=3 SCAR=11.0
279 287 my $self = shift;
280 287 my $node = shift;
281  
282 287 ▶ ref $node || return;
Branch: 1 branch
BranchTrueFalseTotal
ref $node or return 150 137 287
283 150 ▶ return unless ($node->{_tag} =~ /^p$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"_tag"} =~ /^p$/i 71 79 150
284
285 79 my $text = $self->__Content_To_Text ($node);
286 79 $text =~ s/\n/ /gsm;
287 79 $text =~ s/^\s+//;
288 79 $text =~ s/\s+$//;
289
290 79 my @tokens = split /\s+/, $text;
291 79 return "\n\n" . $self->_soft_wrap (@tokens);
292 }
293  
294  
295 sub __Body_DT
296 208 ▶{
Subroutine __Body_DT: called CC=3 SCAR=11.0
297 208 my $self = shift;
298 208 my $node = shift;
299
300 208 ▶ ref $node || return;
Branch: 1 branch
BranchTrueFalseTotal
ref $node or return 71 137 208
301 71 ▶ return unless ($node->{_tag} =~ /^dt$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"_tag"} =~ /^dt$/i 43 28 71
302
303 # treat a DT as a paragraph
304 28 $node->{_tag} = 'p';
305 28 return $self->__Body_P ($node);
306 }
307  
308  
309 sub __Body_DD
310 180 ▶{
Subroutine __Body_DD: called CC=3 SCAR=11.0
311 180 my $self = shift;
312 180 my $node = shift;
313
314 180 ▶ ref $node || return;
Branch: 1 branch
BranchTrueFalseTotal
ref $node or return 43 137 180
315 43 ▶ return unless ($node->{_tag} =~ /^dd$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"_tag"} =~ /^dd$/i 15 28 43
316
317 28 my $text = $self->__Content_To_Text ($node);
318 28 $text =~ s/\n/ /gsm;
319 28 $text =~ s/^\s+//;
320 28 $text =~ s/\s+$//;
321
322 28 my @tokens = split /\s+/, $text;
323
324 28 local ($Line_Wrap) = $Line_Wrap - length ($DD_Indent);
325 28 my $res = $self->_soft_wrap (@tokens);
326  
327 28 my $indent = $self->_text_indent();
328 28 $res =~ s/\n/\n$indent/g;
329 28 return "\n$indent$res";
330 }
331  
332  
333 sub _text_indent
334 28 ▶{
Subroutine _text_indent: called CC=1 SCAR=0.0
335 28 return " " x $DD_Indent;
336 }
337  
338  
339 sub _soft_wrap
340 107 ▶{
Subroutine _soft_wrap: called CC=5 SCAR=16.2
341 107 my $self = shift;
342 107 my @tokens = @_;
343
344 107 my @res = ();
345 107 my @next_line = ();
346 107 ▶ while (@tokens)
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 1610 107 1717
347 {
348 1610 my $next_token = shift (@tokens);
349 1610 my $potential_line = join " ", (@next_line, $next_token);
350
351 1610 ▶ if (length ($potential_line) > $Line_Wrap)
Branch: 1 branch
BranchTrueFalseTotal
if (length $potential_line > $Petal::Mail::Line_Wrap) { } 92 1518 1610
352 {
353 92 ▶ if (@next_line == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@next_line == 0) { } 0 92 92
354 {
355 0 push @res, $next_token;
356 }
357 else
358 {
359 92 unshift (@tokens, $next_token);
360 92 push @res, join " ", @next_line;
361 92 @next_line = ();
362 }
363 }
364 else
365 {
366 1518 push @next_line, $next_token;
367 }
368 }
369
370 107 ▶ push @res, join " ", @next_line unless (@next_line == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @next_line == 0 107 0 107
371 107 return join " \n", @res;
372 }
373  
374  
375 3 1;
376  
377  
378 __END__
↑ Summary fake_sendmail.pl »