Transport/SMTP.pm

filter: Statementstmt 21.0% 21 / 100 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 54.5% 6 / 11 Podpod 60.0% 3 / 5 totaltotal 15.1% 30 / 198 CC 36 SCAR 55.8
CC
36
Cov
12%
trySend
506.0
init
90.0
contactAnyServer
42.0
CRAP 265.5
↑ Summary Transport/Send.pm »
1 # This code is part of Perl distribution Mail-Transport version 4.01.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Transport::SMTP;{
13 1 our $VERSION = '4.01';
14 }
15  
16 1 ▶use parent 'Mail::Transport::Send';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'mail-transport', import => [ qw/__x error notice trace warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Net::SMTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use constant CMD_OK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 sub init($)
30 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=9 SCAR=45.0
Pod undocumented
31 0 ▶ $args->{via} ||= 'smtp';
Condition0%0/2
$args->{"via"} ||= "smtp"
l!l
00
MC/DC0%0/1
$args->{"via"} ||= "smtp"
$args->{"via"}
Truth table0%0/2
A
A= $args->{"via"}
Aresult
11
00
32 0 ▶ $args->{port} ||= '25';
Condition0%0/2
$args->{"port"} ||= 25
l!l
00
MC/DC0%0/1
$args->{"port"} ||= 25
$args->{"port"}
Truth table0%0/2
A
A= $args->{"port"}
Aresult
11
00
33  
34 0 my $hosts = $args->{hostname};
35 0 ▶ unless($hosts)
Branch: 1 branch
BranchTrueFalseTotal
unless ($hosts) 0 0 0
36 0 { require Net::Config;
37 0 $hosts = $Net::Config::NetConfig{smtp_hosts};
38 0 ▶ undef $hosts unless @$hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hosts 0 0 0
39 0 $args->{hostname} = $hosts;
40 }
41  
42 0 ▶ $self->SUPER::init($args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($args) or return 0 0 0
43  
44 my $helo = $args->{helo}
45 0 || eval { require Net::Config; $Net::Config::NetConfig{inet_domain} }
46 0 ▶ || eval { require Net::Domain; Net::Domain::hostfqdn() };
Condition0%0/6
$args->{'helo'} || eval { do { require Net::Config; $Net::Config::NetConfig{'inet_domain'} } } || eval { do { require Net::Domain; Net::Domain::hostfqdn() } }
l!l&&r!l&&!r
000
$args->{"helo"} or eval { do { require Net::Config; $Net::Config::NetConfig{"inet_domain"} } }
l!l&&r!l&&!r
000
MC/DC0%0/3
$args->{'helo'} || eval { do { require Net::Config; $Net::Config::NetConfig{'inet_domain'} } } || eval { do { require Net::Domain; Net::Domain::hostfqdn() } }
$args->{"helo"} eval { do { require Net::Config; $Net::Config::NetConfig{"inet_domain"} } } eval { do { require Net::Domain; Net::Domain::hostfqdn() } }
Truth table0%0/4
A or B || C
A= $args->{"helo"}B= eval { do { require Net::Config; $Net::Config::NetConfig{"inet_domain"} } }C= eval { do { require Net::Domain; Net::Domain::hostfqdn() } }
ABCresult
1XX1
01X1
0011
0000
47  
48 0 ▶ $self->{MTS_net_smtp_opts} = +{ Hello => $helo, Debug => ($args->{smtp_debug} || 0) };
Condition0%0/2
$args->{'smtp_debug'} || 0
l!l
00
MC/DC0%0/1
$args->{'smtp_debug'} || 0
$args->{'smtp_debug'}
Truth table0%0/2
A
A= $args->{'smtp_debug'}
Aresult
11
00
49 0 $self->{MTS_esmtp_options} = $args->{esmtp_options};
50 0 $self->{MTS_from} = $args->{from};
51 0 $self;
52 }
53  
54  
55 sub trySend($@)
56 0 ▶{ my ($self, $message, %args) = @_;
Subroutine trySend: not called CC=22 SCAR=62.3
Pod documented
57 0 ▶ my %send_options = ( %{$self->{MTS_esmtp_options} || {}}, %{$args{esmtp_options} || {}} );
Condition0%0/4
$self->{'MTS_esmtp_options'} || {}
l!l
00
$args{'esmtp_options'} || {}
l!l
00
MC/DC0%0/1
$self->{'MTS_esmtp_options'} || {}
$self->{'MTS_esmtp_options'}
MC/DC0%0/1
$args{'esmtp_options'} || {}
$args{'esmtp_options'}
Truth table0%0/2
A
A= $self->{'MTS_esmtp_options'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'esmtp_options'}
Aresult
11
00
58  
59 # From whom is this message.
60 0 ▶ my $from = $args{from} || $self->{MTS_from} || $message->sender || '<>';
Condition0%0/8
$args{'from'} || $self->{'MTS_from'} || $message->sender || '<>'
l!l
00
$args{"from"} or $self->{"MTS_from"} or $message->sender
l!l&&r!l&&!r
000
$args{"from"} or $self->{"MTS_from"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{'from'} || $self->{'MTS_from'} || $message->sender || '<>'
$args{"from"} $self->{"MTS_from"} $message->sender
Truth table0%0/4
A or B or C
A= $args{"from"}B= $self->{"MTS_from"}C= $message->sender
ABCresult
1XX1
01X1
0011
0000
61 0 ▶ $from = $from->address if ref $from && $from->isa('Mail::Address');
Branch: 1 branch
BranchTrueFalseTotal
if ref $from and $from->isa("Mail::Address") 0 0 0
Condition0%0/3
ref $from and $from->isa("Mail::Address")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $from and $from->isa("Mail::Address")
ref $from $from->isa("Mail::Address")
Truth table0%0/3
A and B
A= ref $fromB= $from->isa("Mail::Address")
ABresult
0X0
100
111
62  
63 # Which are the destinations.
64 ! defined $args{To}
65 0 ▶ or warning __x"use option `to' to overrule the destination: `To' refers to a field.";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"To"} and &warning(&__x("use option `to' to overrule the destination: `To' refers to a field.")) 0 0 0
66  
67 0 my @to = map $_->address, $self->destinations($message, $args{to});
68 0 ▶ @to or notice(__x"no addresses found to send the message to, no connection made."), return 1;
Branch: 1 branch
BranchTrueFalseTotal
@to or &notice(&__x("no addresses found to send the message to, no connection made.")), (return 1) 0 0 0
69  
70 #### Prepare the message.
71  
72 0 my $out = '';
73 0 open my $fh, '>:raw', \$out;
74 0 $self->putContent($message, $fh, undisclosed => 0);
75 0 ▶ $out =~ m![\r\n]\z! or $out .= "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
$out =~ /[\r\n]\z/ or $out .= "\r\n" 0 0 0
76 0 close $fh;
77  
78 #### Send
79  
80 0 my $server;
81 0 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
82 { # In LIST context
83 0 ▶ $server = $self->contactAnyServer
Branch: 1 branch
BranchTrueFalseTotal
$server = $self->contactAnyServer or return 0, 500, "Connection Failed", "CONNECT", 0 0 0 0
84 or return (0, 500, "Connection Failed", "CONNECT", 0);
85  
86 0 ▶ $server->mail($from, %send_options)
Branch: 1 branch
BranchTrueFalseTotal
$server->mail($from, %send_options) or return 0, $server->code, $server->message, "FROM", $server->quit 0 0 0
87 or return (0, $server->code, $server->message, 'FROM', $server->quit);
88  
89 0 foreach (@to)
90 0 ▶ { next if $server->to($_);
Branch: 1 branch
BranchTrueFalseTotal
if $server->to($_) 0 0 0
91 #??? must we be able to disable this? f.i:
92 #??? next if $args{ignore_erroneous_destinations}
93 0 return (0, $server->code, $server->message, "To $_", $server->quit);
94 }
95  
96 0 my $bodydata = $message->body->file;
97  
98 0 ▶ $server->datafast(\$out) #!! destroys $out
Branch: 1 branch
BranchTrueFalseTotal
$server->datafast(\$out) or return 0, $server->code, $server->message, "DATA", $server->quit 0 0 0
99 or return (0, $server->code, $server->message, 'DATA', $server->quit);
100  
101 0 my $accept = ($server->message)[-1];
102 0 chomp $accept;
103  
104 0 my $rc = $server->quit;
105 0 return ($rc, $server->code, $server->message, 'QUIT', $rc, $accept);
106 }
107  
108 # in SCALAR context
109 0 ▶ $server = $self->contactAnyServer
Branch: 1 branch
BranchTrueFalseTotal
$server = $self->contactAnyServer or return 0 0 0 0
110 or return 0;
111  
112 0 ▶ $server->mail($from, %send_options)
Branch: 1 branch
BranchTrueFalseTotal
$server->mail($from, %send_options) or $server->quit, (return 0) 0 0 0
113 or ($server->quit, return 0);
114  
115 0 foreach (@to)
116 0 ▶ { next if $server->to($_);
Branch: 1 branch
BranchTrueFalseTotal
if $server->to($_) 0 0 0
117 0 $server->quit;
118 0 return 0;
119 }
120  
121 0 ▶ $server->datafast(\$out) #!! destroys $out
Branch: 1 branch
BranchTrueFalseTotal
$server->datafast(\$out) or $server->quit, (return 0) 0 0 0
122 or ($server->quit, return 0);
123  
124 0 $server->quit;
125 }
126  
127 # Improvement on Net::CMD::datasend(), mainly bulk adding dots and avoiding copying
128 # About 79% performance gain on huge messages.
129 # Be warned: this method destructs the content of $data!
130 sub Net::SMTP::datafast($)
131 0 ▶{ my ($self, $data) = @_;
Subroutine datafast: not called CC=2 SCAR=17.9
Pod undocumented
132 0 ▶ $self->_DATA or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_DATA or return 0 0 0 0
133  
134 0 $$data =~ tr/\r\n/\015\012/ if "\r" ne "\015"; # mac
135 0 $$data =~ s/(?<!\015)\012/\015\012/g; # \n -> crlf as sep. Needed?
136 0 $$data =~ s/^\./../; # data starts with a dot, escape it
137 0 $$data =~ s/\012\./\012../g; # other lines which start with a dot
138  
139 0 $self->_syswrite_with_timeout($$data . ".\015\012");
140 0 $self->response == CMD_OK;
141 }
142  
143 #--------------------
144  
145 sub contactAnyServer()
146 0 ▶{ my $self = shift;
Subroutine contactAnyServer: not called CC=6 SCAR=37.4
Pod documented
147  
148 0 my ($enterval, $count, $timeout) = $self->retry;
149 0 my ($host, $port, $username, $password) = $self->remoteHost;
150 0 ▶ my @hosts = ref $host ? @$host : $host;
Branch: 1 branch
BranchTrueFalseTotal
ref $host ? : 0 0 0
151 0 my $opts = $self->{MTS_net_smtp_opts};
152  
153 0 foreach my $host (@hosts)
154 0 ▶ { my $server = $self->tryConnectTo($host, Port => $port, %$opts, Timeout => $timeout)
Branch: 1 branch
BranchTrueFalseTotal
my $server = $self->tryConnectTo($host, "Port", $port, %$opts, "Timeout", $timeout) or next 0 0 0
155 or next;
156  
157 0 trace "opened SMTP connection to $host.";
158  
159 0 ▶ if(defined $username)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username) 0 0 0
160 0 ▶ { unless($server->auth($username, $password))
Branch: 1 branch
BranchTrueFalseTotal
unless ($server->auth($username, $password)) 0 0 0
161 0 { error __x"authentication for {host} failed.", host => $host;
162 0 return undef;
163 }
164 0 trace "$host: Authentication succeeded.";
165 }
166  
167 0 return $server;
168 }
169  
170 0 undef;
171 }
172  
173  
174 sub tryConnectTo($@)
175 0 ▶{ my ($self, $host) = (shift, shift);
Subroutine tryConnectTo: not called CC=1 SCAR=6.9
Pod documented
176 0 Net::SMTP->new($host, @_);
177 }
178  
179 1 1;
↑ Summary Transport/Send.pm »