lib/Serge/Mail.pm

filter: Statementstmt 48.0% 37 / 77 Branchbran 8.0% 4 / 50 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 2 totaltotal 33.9% 54 / 159 SCAR 52.1
CC
23
Cov
33%
_send
380.0
sendmessage
12.0
send_html_message
12.0
CRAP 183.1
« lib/Serge/Engine/Plugin/keys_language.pm ↑ Summary lib/Serge/Engine/Plugin/parse_js.pm »
1 package Serge::Mail;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $debug = $ENV{SMTP_DEBUG} == 1;
11 1 our $disable = $ENV{SMTP_DISABLE} == 1;
12 1 our $force_to = $ENV{SMTP_FORCE_RECIPIENTS};
13  
14 1 ▶our $host = $ENV{SMTP_HOST} || 'localhost';
Condition50%1/2
$ENV{'SMTP_HOST'} || 'localhost'
l!l
01
MC/DC0%0/1
$ENV{'SMTP_HOST'} || 'localhost'
$ENV{'SMTP_HOST'}
Truth table50%1/2
A
A= $ENV{'SMTP_HOST'}
Aresult
11
00
15 1 ▶our $port = $ENV{SMTP_PORT} || 25;
Condition50%1/2
$ENV{'SMTP_PORT'} || 25
l!l
01
MC/DC0%0/1
$ENV{'SMTP_PORT'} || 25
$ENV{'SMTP_PORT'}
Truth table50%1/2
A
A= $ENV{'SMTP_PORT'}
Aresult
11
00
16 1 our $user = $ENV{SMTP_USERNAME};
17 1 our $pass = $ENV{SMTP_PASSWORD};
18  
19 1 ▶if ($pass && !$user || $user && !$pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::Mail::pass and not $Serge::Mail::user or $Serge::Mail::user and not $Serge::Mail::pass) 0 1 1
Condition33%3/9
$Serge::Mail::pass and not $Serge::Mail::user or $Serge::Mail::user and not $Serge::Mail::pass
l!l&&r!l&&!r
001
$Serge::Mail::pass and not $Serge::Mail::user
!ll&&!rl&&r
100
$Serge::Mail::user and not $Serge::Mail::pass
!ll&&!rl&&r
100
MC/DC0%0/4
$Serge::Mail::pass and not $Serge::Mail::user or $Serge::Mail::user and not $Serge::Mail::pass
$Serge::Mail::pass not $Serge::Mail::user $Serge::Mail::user not $Serge::Mail::pass
Truth table0%0/7
A and not(B) or C and not(D)
A= $Serge::Mail::passB= not $Serge::Mail::userC= $Serge::Mail::userD= not $Serge::Mail::pass
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
20 0 die "Both SMTP_PASSWORD and SMTP_USERNAME should be provided (or neither one)";
21 }
22  
23 1 our $use_ssl = ($port == 465); # autodetect
24 1 ▶$use_ssl = ($ENV{SMTP_USE_SSL} == 1) if exists $ENV{SMTP_USE_SSL};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"SMTP_USE_SSL"} 0 1 1
25 1 ▶$use_ssl = undef if exists $ENV{SMTP_NO_SSL};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"SMTP_NO_SSL"} 0 1 1
26  
27 1 ▶if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::Mail::debug) 0 1 1
28 0 print "SMTP Host: $host\n";
29 0 print "SMTP Port: $port\n";
30 0 ▶ print "SMTP SSL : ".($use_ssl ? 'YES' : 'NO')."\n";
Branch: 1 branch
BranchTrueFalseTotal
$Serge::Mail::use_ssl ? : 0 0 0
31 0 ▶ print "SMTP User: $user\n" if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $Serge::Mail::user 0 0 0
32 }
33  
34 sub _send {
35 0 ▶ my ($from, $toref, $ccref, $bccref, $data) = @_;
Subroutine _send: not called CC=19 SCAR=59.4
36  
37 0 ▶ if ($disable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::Mail::disable) 0 0 0
38 0 print "Warning: email will not be sent because of SMTP_DISABLE environment variable\n";
39 0 return;
40 }
41  
42 0 my $smtp = Net::SMTP->new($host, Port => $port, SSL => $use_ssl, Debug => $debug);
43 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
44 0 warn "Failed to send the following email:\n$data\n";
45 0 warn $@;
46 }
47  
48 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::Mail::user) 0 0 0
49 0 ▶ $smtp->auth($user, $pass) or die "auth() call failed for user '$user'";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->auth($Serge::Mail::user, $Serge::Mail::pass) or die "auth() call failed for user '$Serge::Mail::user'" 0 0 0
50 }
51 0 ▶ $smtp->mail("$from\n") or die "mail(from) call failed; from='$from'";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->mail("$from\n") or die "mail(from) call failed; from='${from}'" 0 0 0
52  
53 0 my @to = @$toref;
54 0 ▶ if ($force_to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Serge::Mail::force_to) 0 0 0
55 0 @to = split(',', $force_to);
56 0 print "Warning: the list of email recipients is overridden by SMTP_FORCE_RECIPIENTS environment variable\n";
57 0 print "Email will be sent to: ", join(', ', sort @to), "\n";
58 }
59 0 foreach my $recpt (@to) {
60 0 ▶ $smtp->to($recpt) or die "to(rcpt) call failed; rcpt='$recpt'";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->to($recpt) or die "to(rcpt) call failed; rcpt='${recpt}'" 0 0 0
61 }
62 0 ▶ if (defined($ccref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ccref) 0 0 0
63 0 foreach my $recpt (@$ccref) {
64 0 ▶ $smtp->cc($recpt) or die "cc(rcpt) call failed; rcpt='$recpt'";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->cc($recpt) or die "cc(rcpt) call failed; rcpt='${recpt}'" 0 0 0
65 }
66 }
67 0 ▶ if (defined($bccref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bccref) 0 0 0
68 0 foreach my $recpt (@$bccref) {
69 0 ▶ $smtp->bcc($recpt) or die "bcc(rcpt) call failed; rcpt='$recpt'";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->bcc($recpt) or die "bcc(rcpt) call failed; rcpt='${recpt}'" 0 0 0
70 }
71 }
72 0 ▶ $smtp->data() or die "data() call failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->data or die "data() call failed" 0 0 0
73 0 ▶ $smtp->datasend($data) or die "datasend() call failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend($data) or die "datasend() call failed" 0 0 0
74 0 ▶ $smtp->dataend() or die "dataend() call failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->dataend or die "dataend() call failed" 0 0 0
75 0 ▶ $smtp->quit() or die "quit() call failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->quit or die "quit() call failed" 0 0 0
76 }
77  
78 sub sendmessage {
79 0 ▶ my ($from, $toref, $subject, $body, $ccref, $bccref) = @_;
Subroutine sendmessage: not called CC=3 SCAR=24.8
Pod undocumented
80  
81 0 ▶ $body =
Branch: 2 branches
BranchTrueFalseTotal
defined $ccref ? : 0 0 0
defined $bccref ? : 0 0 0
82 "From: $from\n".
83 "To: ".join(', ', @$toref)."\n".
84 "Cc: ". (defined($ccref) ? join(', ', @$ccref) : '') ."\n".
85 "Bcc: ". (defined($bccref) ? join(', ', @$bccref) : '') ."\n".
86 "Subject: $subject\n".
87 "\n".
88 "$body\n";
89  
90 0 _send($from, $toref, $ccref, $bccref, $body);
91 }
92  
93 sub send_html_message {
94 0 ▶ my ($from, $toref, $subject, $body, $ccref, $bccref) = @_;
Subroutine send_html_message: not called CC=3 SCAR=24.8
Pod undocumented
95  
96 ### Create a new multipart message:
97 0 ▶ my $msg = MIME::Lite->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $ccref ? : 0 0 0
defined $bccref ? : 0 0 0
98 From => $from,
99 To => join(', ', @$toref),
100 Cc => defined($ccref) ? join(', ', @$ccref) : '',
101 Bcc => defined($bccref) ? join(', ', @$bccref) : '',
102 Subject => $subject,
103 Type => 'multipart/mixed'
104 );
105  
106 0 $msg->attach(
107 Type => 'text/html; charset=utf-8',
108 Data => $body
109 );
110  
111 0 _send($from, $toref, $ccref, $bccref, $msg->as_string);
112 }
113  
114 1 1;
« lib/Serge/Engine/Plugin/keys_language.pm ↑ Summary lib/Serge/Engine/Plugin/parse_js.pm »