| 1 | package Mail::Sendmail; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | # Mail::Sendmail by Milivoj Ivkovic <mi\x40alma.ch> | ||||||||||||||||||||||||||||||||||
| 4 | # see embedded POD documentation after __END__ | ||||||||||||||||||||||||||||||||||
| 5 | # or http://alma.ch/perl/mail.html | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | Mail::Sendmail - Simple platform independent mailer | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 1 | require 5.006; | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = "0.80"; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | # *************** Configuration you may want to change ******************* | ||||||||||||||||||||||||||||||||||
| 23 | # You probably want to set your SMTP server here (unless you specify it in | ||||||||||||||||||||||||||||||||||
| 24 | # every script), and leave the rest as is. See pod documentation for details | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 1 | our %mailcfg = ( | |||||||||||||||||||||||||||||||||
| 27 | # List of SMTP servers: | ||||||||||||||||||||||||||||||||||
| 28 | 'smtp' => [ qw( localhost ) ], | ||||||||||||||||||||||||||||||||||
| 29 | #'smtp' => [ qw( mail.mydomain.com ) ], # example | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 'from' => '', # default sender e-mail, used when no From header in mail | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 'mime' => 1, # use MIME encoding by default | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 'retries' => 1, # number of retries on smtp connect failure | ||||||||||||||||||||||||||||||||||
| 36 | 'delay' => 1, # delay in seconds between retries | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 'tz' => '', # only to override automatic detection | ||||||||||||||||||||||||||||||||||
| 39 | 'port' => 25, # change it if you always use a non-standard port | ||||||||||||||||||||||||||||||||||
| 40 | 'debug' => 0 # prints stuff to STDERR | ||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | # ******************************************************************* | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 1 | our $address_rx; | |||||||||||||||||||||||||||||||||
| 46 | 1 | our $debug; | |||||||||||||||||||||||||||||||||
| 47 | 1 | our $log; | |||||||||||||||||||||||||||||||||
| 48 | 1 | our $error; | |||||||||||||||||||||||||||||||||
| 49 | 1 | our $retry_delay; | |||||||||||||||||||||||||||||||||
| 50 | 1 | our $connect_retries; | |||||||||||||||||||||||||||||||||
| 51 | 1 | our $auth_support; | |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use Time::Local; # for automatic time zone detection | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | use Sys::Hostname; # for use of hostname in HELO | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | #use Digest::HMAC_MD5 qw(hmac_md5 hmac_md5_hex); | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 1 | $auth_support = 'DIGEST-MD5 CRAM-MD5 PLAIN LOGIN'; | |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | # use MIME::QuotedPrint if available and configured in %mailcfg | ||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | eval("use MIME::QuotedPrint"); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | $mailcfg{'mime'} &&= (!$@); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Mail::Sendmail::mailcfg{"mime"} &&= !$@
MC/DC0%0/2
$Mail::Sendmail::mailcfg{"mime"} &&= !$@
$Mail::Sendmail::mailcfg{"mime"}
!$@
Truth table33%1/3
A &&= not(B)
A= $Mail::Sendmail::mailcfg{"mime"}B= !$@
| |||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 1 | our @EXPORT = qw(&sendmail); | |||||||||||||||||||||||||||||||||
| 66 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||
| 67 | %mailcfg | ||||||||||||||||||||||||||||||||||
| 68 | time_to_date | ||||||||||||||||||||||||||||||||||
| 69 | $address_rx | ||||||||||||||||||||||||||||||||||
| 70 | $debug | ||||||||||||||||||||||||||||||||||
| 71 | $log | ||||||||||||||||||||||||||||||||||
| 72 | $error | ||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | # regex for e-mail addresses where full=$1, user=$2, domain=$3 | ||||||||||||||||||||||||||||||||||
| 76 | # see pod documentation about this regex | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 1 | my $word_rx = '[\x21\x23-\x27\x2A-\x2B\x2D\x2F\w\x3D\x3F]+'; | |||||||||||||||||||||||||||||||||
| 79 | 1 | my $user_rx = $word_rx # valid chars | |||||||||||||||||||||||||||||||||
| 80 | .'(?:\.' . $word_rx . ')*' # possibly more words preceded by a dot | ||||||||||||||||||||||||||||||||||
| 81 | ; | ||||||||||||||||||||||||||||||||||
| 82 | 1 | my $dom_rx = '\w[-\w]*(?:\.\w[-\w]*)*'; # less valid chars in domain names | |||||||||||||||||||||||||||||||||
| 83 | 1 | my $ip_rx = '\[\d{1,3}(?:\.\d{1,3}){3}\]'; | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 1 | $address_rx = '((' . $user_rx . ')\@(' . $dom_rx . '|' . $ip_rx . '))'; | |||||||||||||||||||||||||||||||||
| 86 | ; # v. 0.61 | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | sub _require_md5 { | ||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | eval { require Digest::MD5; Digest::MD5->import(qw(md5 md5_hex)); }; | ||||||||||||||||||||||||||||||||
|
Subroutine
_require_md5: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $error .= $@ if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return ($@ ? undef : 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | sub _require_base64 { | ||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||||
|
Subroutine
_require_base64: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 96 | 0 | require MIME::Base64; MIME::Base64->import(qw(encode_base64 decode_base64)); | |||||||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $error .= $@ if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return ($@ ? undef : 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | sub _hmac_md5 { | ||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my ($pass, $ckey) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_hmac_md5: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | my $size = 64; | |||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $pass = md5($pass) if length($pass) > $size; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | my $ipad = $pass ^ (chr(0x36) x $size); | |||||||||||||||||||||||||||||||||
| 107 | 0 | my $opad = $pass ^ (chr(0x5c) x $size); | |||||||||||||||||||||||||||||||||
| 108 | 0 | return md5_hex($opad, md5($ipad, $ckey)); | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | sub _digest_md5 { | ||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my ($user, $pass, $challenge, $realm) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_digest_md5: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 0 | my %ckey = map { /^([^=]+)="?(.+?)"?$/ } split(/,/, $challenge); | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $realm ||= $ckey{realm}; #($user =~ s/\@(.+)$//o) ? $1 : $server; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$realm ||= $ckey{"realm"}
MC/DC0%0/2
$realm ||= $ckey{"realm"}
$realm
$ckey{"realm"}
Truth table0%0/3
A ||= B
A= $realmB= $ckey{"realm"}
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | my $nonce = $ckey{nonce}; | |||||||||||||||||||||||||||||||||
| 117 | 0 | my $cnonce = &make_cnonce; | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $uri = join('/', 'smtp', hostname()||'localhost', $ckey{realm}); | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
hostname() || 'localhost'
MC/DC0%0/1
hostname() || 'localhost'
hostname()
Truth table0%0/2
A
A= hostname()
| |||||||||||||||||||||||||||||||||||
| 119 | 0 | my $qop = 'auth'; | |||||||||||||||||||||||||||||||||
| 120 | 0 | my $nc = '00000001'; | |||||||||||||||||||||||||||||||||
| 121 | 0 | my($hv, $a1, $a2); | |||||||||||||||||||||||||||||||||
| 122 | 0 | $hv = md5("$user:$realm:$pass"); | |||||||||||||||||||||||||||||||||
| 123 | 0 | $a1 = md5_hex("$hv:$nonce:$cnonce"); | |||||||||||||||||||||||||||||||||
| 124 | 0 | $a2 = md5_hex("AUTHENTICATE:$uri"); | |||||||||||||||||||||||||||||||||
| 125 | 0 | $hv = md5_hex("$a1:$nonce:$nc:$cnonce:$qop:$a2"); | |||||||||||||||||||||||||||||||||
| 126 | 0 | return qq(username="$user",realm="$ckey{realm}",nonce="$nonce",nc=$nc,cnonce="$cnonce",digest-uri="$uri",response=$hv,qop=$qop); | |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | sub make_cnonce { | ||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $s = '' ; | ||||||||||||||||||||||||||||||||
|
Subroutine
make_cnonce: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 131 | 0 | for(1..16) { $s .= chr(rand 256) } | |||||||||||||||||||||||||||||||||
| 132 | 0 | $s = encode_base64($s, ""); | |||||||||||||||||||||||||||||||||
| 133 | 0 | $s =~ s/\W/X/go; | |||||||||||||||||||||||||||||||||
| 134 | 0 | return substr($s, 0, 16); | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | sub time_to_date { | ||||||||||||||||||||||||||||||||||
| 138 | # convert a time() value to a date-time string according to RFC 822 | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | 2 | ▶ | my $time = $_[0] || time(); # default to now if no argument | ||||||||||||||||||||||||||||||||
|
Subroutine
time_to_date: called CC=3 SCAR=11.2
Pod
documented
Condition33%1/3
$_[0] || time
MC/DC0%0/2
$_[0] || time
$_[0]
time
Truth table33%1/3
A || B
A= $_[0]B= time
| |||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 2 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||||||||||||||||
| 143 | 2 | my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 2 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) | |||||||||||||||||||||||||||||||||
| 146 | = localtime($time); | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 2 | my $TZ = $mailcfg{'tz'}; | |||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | if ( $TZ eq "" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 150 | # offset in hours | ||||||||||||||||||||||||||||||||||
| 151 | 2 | my $offset = sprintf "%.1f", (timegm(localtime) - time) / 3600; | |||||||||||||||||||||||||||||||||
| 152 | 2 | my $minutes = sprintf "%02d", abs( $offset - int($offset) ) * 60; | |||||||||||||||||||||||||||||||||
| 153 | 2 | $TZ = sprintf("%+03d", int($offset)) . $minutes; | |||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||
| 155 | 2 | return join(" ", | |||||||||||||||||||||||||||||||||
| 156 | ($wdays[$wday] . ','), | ||||||||||||||||||||||||||||||||||
| 157 | $mday, | ||||||||||||||||||||||||||||||||||
| 158 | $months[$mon], | ||||||||||||||||||||||||||||||||||
| 159 | $year+1900, | ||||||||||||||||||||||||||||||||||
| 160 | sprintf("%02d:%02d:%02d", $hour, $min, $sec), | ||||||||||||||||||||||||||||||||||
| 161 | $TZ | ||||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||||
| 163 | } # end sub time_to_date | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | sub sendmail { | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | $error = ''; | ||||||||||||||||||||||||||||||||
|
Subroutine
sendmail: called CC=102 SCAR=74.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 168 | 1 | $log = "Mail::Sendmail v. $VERSION - " . scalar(localtime()) . "\n"; | |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | 1 | my $CRLF = "\015\012"; | |||||||||||||||||||||||||||||||||
| 171 | 1 | local $/ = $CRLF; | |||||||||||||||||||||||||||||||||
| 172 | 1 | local $\ = ''; # to protect us from outside settings | |||||||||||||||||||||||||||||||||
| 173 | 1 | local $_; | |||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | 1 | my (%mail, $k, | |||||||||||||||||||||||||||||||||
| 176 | $smtp, $server, $port, $connected, $localhost, | ||||||||||||||||||||||||||||||||||
| 177 | $fromaddr, $recip, @recipients, $to, $header, | ||||||||||||||||||||||||||||||||||
| 178 | %esmtp, @wanted_methods, | ||||||||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | use vars qw($server_reply); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 181 | # -------- a few internal subs ---------- | ||||||||||||||||||||||||||||||||||
| 182 | sub fail { | ||||||||||||||||||||||||||||||||||
| 183 | # things to do before returning a sendmail failure | ||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | $error .= join(" ", @_) . "\n"; | ||||||||||||||||||||||||||||||||
|
Subroutine
fail: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($server_reply) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 186 | 0 | $error .= "Server said: $server_reply\n"; | |||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | print STDERR "Server said: $server_reply\n" if $^W; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | 0 | close S; | |||||||||||||||||||||||||||||||||
| 190 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | sub socket_write { | ||||||||||||||||||||||||||||||||||
| 194 | 13 | ▶ | my $i; | ||||||||||||||||||||||||||||||||
|
Subroutine
socket_write: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 195 | 13 | for $i (0..$#_) { | |||||||||||||||||||||||||||||||||
| 196 | # accept references, so we don't copy potentially big data | ||||||||||||||||||||||||||||||||||
| 197 | 15 | ▶ | my $data = ref($_[$i]) ? $_[$i] : \$_[$i]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 198 | 15 | ▶ | if ($mailcfg{'debug'} > 5) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if (length($$data) < 500) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 200 | 0 | print ">", $$data; | |||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||||||||
| 203 | 0 | print "> [...", length($$data), " bytes sent ...]\n"; | |||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||
| 206 | 15 | ▶ | print(S $$data) || return 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||
| 208 | 13 | 1; | |||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||
| 211 | sub socket_read { | ||||||||||||||||||||||||||||||||||
| 212 | 7 | ▶ | $server_reply = ""; | ||||||||||||||||||||||||||||||||
|
Subroutine
socket_read: called CC=5 SCAR=17.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 213 | 7 | ▶ | do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 18 | $_ = <S>; | |||||||||||||||||||||||||||||||||
| 215 | 18 | $server_reply .= $_; | |||||||||||||||||||||||||||||||||
| 216 | #chomp $_; | ||||||||||||||||||||||||||||||||||
| 217 | 18 | ▶ | print "<$_" if $mailcfg{'debug'} > 5; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 218 | 18 | ▶ | if (/^[45]/ or !$_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/^[45]/ or not $_
MC/DC0%0/2
/^[45]/ or not $_
/^[45]/
not $_
Truth table33%1/3
A or not(B)
A= /^[45]/B= not $_
| |||||||||||||||||||||||||||||||||||
| 219 | 0 | chomp $server_reply; | |||||||||||||||||||||||||||||||||
| 220 | 0 | return; # return false | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | } while (/^[\d]+-/); | ||||||||||||||||||||||||||||||||||
| 223 | 7 | chomp $server_reply; | |||||||||||||||||||||||||||||||||
| 224 | 7 | return $server_reply; | |||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||
| 226 | # -------- end of internal subs ---------- | ||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||
| 228 | # all config keys to lowercase, to prevent typo errors | ||||||||||||||||||||||||||||||||||
| 229 | 1 | foreach $k (keys %mailcfg) { | |||||||||||||||||||||||||||||||||
| 230 | 8 | ▶ | if ($k =~ /[A-Z]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 231 | 0 | $mailcfg{lc($k)} = $mailcfg{$k}; | |||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||
| 235 | # redo mail hash, arranging keys case etc... | ||||||||||||||||||||||||||||||||||
| 236 | 1 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 237 | 5 | $k = shift @_; | |||||||||||||||||||||||||||||||||
| 238 | 5 | ▶ | if (!$k and $^W) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $k and $^W
MC/DC0%0/2
not $k and $^W
not $k
$^W
Truth table33%1/3
not(A) and B
A= not $kB= $^W
| |||||||||||||||||||||||||||||||||||
| 239 | 0 | warn "Received false mail hash key: \'$k\'. Did you forget to put it in quotes?\n"; | |||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | # arrange keys case | ||||||||||||||||||||||||||||||||||
| 243 | 5 | $k = ucfirst lc($k); | |||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||
| 245 | 5 | $k =~ s/\s*:\s*$//o; # kill colon (and possible spaces) at end, we add it later. | |||||||||||||||||||||||||||||||||
| 246 | # uppercase also after "-", so people don't complain that headers case is different | ||||||||||||||||||||||||||||||||||
| 247 | # than in Outlook. | ||||||||||||||||||||||||||||||||||
| 248 | 5 | $k =~ s/-(.)/"-" . uc($1)/ge; | |||||||||||||||||||||||||||||||||
| 249 | 5 | $mail{$k} = shift @_; | |||||||||||||||||||||||||||||||||
| 250 | 5 | ▶ | if ($k !~ /^(Message|Body|Text)$/i) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 251 | # normalize possible line endings in headers | ||||||||||||||||||||||||||||||||||
| 252 | 4 | $mail{$k} =~ s/\015\012?/\012/go; | |||||||||||||||||||||||||||||||||
| 253 | 4 | $mail{$k} =~ s/\012/$CRLF/go; | |||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||
| 257 | 1 | ▶ | $smtp = $mail{'Smtp'} || $mail{'Server'}; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$mail{'Smtp'} || $mail{'Server'}
MC/DC0%0/2
$mail{'Smtp'} || $mail{'Server'}
$mail{'Smtp'}
$mail{'Server'}
Truth table33%1/3
A || B
A= $mail{'Smtp'}B= $mail{'Server'}
| |||||||||||||||||||||||||||||||||||
| 258 | 1 | ▶ | unshift @{$mailcfg{'smtp'}}, $smtp if ($smtp and $mailcfg{'smtp'}->[0] ne $smtp); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$smtp and $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
MC/DC0%0/2
$smtp and $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
$smtp
$Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
Truth table33%1/3
A and B
A= $smtpB= $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
| |||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||
| 260 | # delete non-header keys, so we don't send them later as mail headers | ||||||||||||||||||||||||||||||||||
| 261 | # I like this syntax, but it doesn't seem to work with AS port 5.003_07: | ||||||||||||||||||||||||||||||||||
| 262 | # delete @mail{'Smtp', 'Server'}; | ||||||||||||||||||||||||||||||||||
| 263 | # so instead: | ||||||||||||||||||||||||||||||||||
| 264 | 1 | delete $mail{'Smtp'}; delete $mail{'Server'}; | |||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | $mailcfg{'port'} = $mail{'Port'} || $mailcfg{'port'} || 25; | ||||||||||||||||||||||||||||||||
|
Condition40%2/5
$mail{'Port'} || $Mail::Sendmail::mailcfg{'port'} || 25
$mail{"Port"} or $Mail::Sendmail::mailcfg{"port"}
MC/DC0%0/2
$mail{'Port'} || $Mail::Sendmail::mailcfg{'port'} || 25
$mail{"Port"}
$Mail::Sendmail::mailcfg{"port"}
Truth table33%1/3
A or B
A= $mail{"Port"}B= $Mail::Sendmail::mailcfg{"port"}
| |||||||||||||||||||||||||||||||||||
| 267 | 1 | delete $mail{'Port'}; | |||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||
| 269 | 1 | my $auth = $mail{'Auth'}; | |||||||||||||||||||||||||||||||||
| 270 | 1 | delete $mail{'Auth'}; | |||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | 1 | my @parts; | |||||||||||||||||||||||||||||||||
| 273 | 1 | ▶ | push(@parts, $mail{'Message'}) if defined($mail{'Message'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 274 | 1 | ▶ | push(@parts, $mail{'Body'}) if defined($mail{'Body'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | push(@parts, $mail{'Text'}) if defined($mail{'Text'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 276 | 1 | $mail{'Message'} = join("", @parts); | |||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | # delete @mail{'Body', 'Text'}; | ||||||||||||||||||||||||||||||||||
| 279 | 1 | delete $mail{'Body'}; | |||||||||||||||||||||||||||||||||
| 280 | 1 | delete $mail{'Text'}; | |||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||
| 282 | # Extract 'From:' e-mail address to use as envelope sender | ||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | 1 | ▶ | $fromaddr = $mail{'Sender'} || $mail{'From'} || $mailcfg{'from'}; | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$mail{'Sender'} || $mail{'From'} || $Mail::Sendmail::mailcfg{'from'}
$mail{"Sender"} or $mail{"From"}
MC/DC0%0/3
$mail{'Sender'} || $mail{'From'} || $Mail::Sendmail::mailcfg{'from'}
$mail{"Sender"}
$mail{"From"}
$Mail::Sendmail::mailcfg{'from'}
Truth table25%1/4
A or B || C
A= $mail{"Sender"}B= $mail{"From"}C= $Mail::Sendmail::mailcfg{'from'}
| |||||||||||||||||||||||||||||||||||
| 285 | #delete $mail{'Sender'}; | ||||||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | unless ($fromaddr =~ /$address_rx/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 287 | 0 | return fail("Bad or missing From address: \'$fromaddr\'"); | |||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||
| 289 | 1 | $fromaddr = $1; | |||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||
| 291 | # add Date header if needed | ||||||||||||||||||||||||||||||||||
| 292 | 1 | ▶ | $mail{Date} ||= time_to_date() ; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$mail{"Date"} ||= time_to_date()
MC/DC0%0/2
$mail{"Date"} ||= time_to_date()
$mail{"Date"}
time_to_date()
Truth table33%1/3
A ||= B
A= $mail{"Date"}B= time_to_date()
| |||||||||||||||||||||||||||||||||||
| 293 | 1 | $log .= "Date: $mail{Date}\n"; | |||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||
| 295 | # cleanup message, and encode if needed | ||||||||||||||||||||||||||||||||||
| 296 | 1 | $mail{'Message'} =~ s/\r\n/\n/go; # normalize line endings, step 1 of 2 (next step after MIME encoding) | |||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | $mail{'Mime-Version'} ||= '1.0'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mail{"Mime-Version"} ||= "1.0"
MC/DC0%0/1
$mail{"Mime-Version"} ||= "1.0"
$mail{"Mime-Version"}
Truth table50%1/2
A
A= $mail{"Mime-Version"}
| |||||||||||||||||||||||||||||||||||
| 299 | 1 | ▶ | $mail{'Content-Type'} ||= 'text/plain; charset="iso-8859-1"'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mail{"Content-Type"} ||= "text/plain; charset=\"iso-8859-1\""
MC/DC0%0/1
$mail{"Content-Type"} ||= "text/plain; charset=\"iso-8859-1\""
$mail{"Content-Type"}
Truth table50%1/2
A
A= $mail{"Content-Type"}
| |||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||
| 301 | 1 | ▶ | unless ( $mail{'Content-Transfer-Encoding'} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$mail{"Content-Transfer-Encoding"} or $mail{"Content-Type"} =~ /multipart/io
MC/DC0%0/2
$mail{"Content-Transfer-Encoding"} or $mail{"Content-Type"} =~ /multipart/io
$mail{"Content-Transfer-Encoding"}
$mail{"Content-Type"} =~ /multipart/io
Truth table33%1/3
A or B
A= $mail{"Content-Transfer-Encoding"}B= $mail{"Content-Type"} =~ /multipart/io
| |||||||||||||||||||||||||||||||||||
| 302 | || $mail{'Content-Type'} =~ /multipart/io ) | ||||||||||||||||||||||||||||||||||
| 303 | { | ||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | if ($mailcfg{'mime'}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 305 | 1 | $mail{'Content-Transfer-Encoding'} = 'quoted-printable'; | |||||||||||||||||||||||||||||||||
| 306 | 1 | $mail{'Message'} = encode_qp($mail{'Message'}); | |||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||
| 308 | else { | ||||||||||||||||||||||||||||||||||
| 309 | 0 | $mail{'Content-Transfer-Encoding'} = '8bit'; | |||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ($mail{'Message'} =~ /[\x80-\xFF]/o) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 311 | 0 | $error .= "MIME::QuotedPrint not present!\nSending 8bit characters, hoping it will come across OK.\n"; | |||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | warn "MIME::QuotedPrint not present!\n", | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 313 | "Sending 8bit characters without encoding, hoping it will come across OK.\n" | ||||||||||||||||||||||||||||||||||
| 314 | if $^W; | ||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||
| 319 | 1 | $mail{'Message'} =~ s/^\./\.\./gom; # handle . as first character | |||||||||||||||||||||||||||||||||
| 320 | 1 | $mail{'Message'} =~ s/\n/$CRLF/go; # normalize line endings, step 2. | |||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | # Get recipients | ||||||||||||||||||||||||||||||||||
| 323 | { # don't warn for undefined values below | ||||||||||||||||||||||||||||||||||
| 324 | 1 | my @recipients; | |||||||||||||||||||||||||||||||||
| 325 | 1 | ▶ | push(@recipients, $mail{To}) if defined($mail{To}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 326 | 1 | ▶ | push(@recipients, $mail{Cc}) if defined($mail{Cc}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 327 | 1 | ▶ | push(@recipients, $mail{Bcc}) if defined($mail{Bcc}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 328 | 1 | $recip = join(", ", @recipients); | |||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||
| 331 | 1 | delete $mail{'Bcc'}; | |||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||
| 333 | 1 | @recipients = (); | |||||||||||||||||||||||||||||||||
| 334 | 1 | ▶ | while ($recip =~ /$address_rx/go) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 335 | 1 | push @recipients, $1; | |||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||
| 337 | 1 | ▶ | unless (@recipients) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 338 | 0 | return fail("No recipient!") | |||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | # get local hostname for polite HELO | ||||||||||||||||||||||||||||||||||
| 342 | 1 | ▶ | $localhost = hostname() || 'localhost'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
hostname() || 'localhost'
MC/DC0%0/1
hostname() || 'localhost'
hostname()
Truth table50%1/2
A
A= hostname()
| |||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | 1 | foreach $server ( @{$mailcfg{'smtp'}} ) { | |||||||||||||||||||||||||||||||||
| 345 | # open socket needs to be inside this foreach loop on Linux, | ||||||||||||||||||||||||||||||||||
| 346 | # otherwise all servers fail if 1st one fails !??! why? | ||||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | unless ( socket S, AF_INET, SOCK_STREAM, scalar(getprotobyname 'tcp') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 348 | 0 | return fail("socket failed ($!)") | |||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | 1 | ▶ | print "- trying $server\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | 1 | $server =~ s/\s+//go; # remove spaces just in case of a typo | |||||||||||||||||||||||||||||||||
| 354 | # extract port if server name like "mail.domain.com:2525" | ||||||||||||||||||||||||||||||||||
| 355 | 1 | ▶ | $port = ($server =~ s/:(\d+)$//o) ? $1 : $mailcfg{'port'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 356 | 1 | $smtp = $server; # save $server for use outside foreach loop | |||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | 1 | my $smtpaddr = inet_aton $server; | |||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | unless ($smtpaddr) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 360 | 0 | $error .= "$server not found\n"; | |||||||||||||||||||||||||||||||||
| 361 | 0 | next; # next server | |||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||
| 364 | 1 | my $retried = 0; # reset retries for each server | |||||||||||||||||||||||||||||||||
| 365 | 1 | ▶ | while ( ( not $connected = connect S, pack_sockaddr_in($port, $smtpaddr) ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr)) and $retried < $Mail::Sendmail::mailcfg{"retries"}
MC/DC0%0/2
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr)) and $retried < $Mail::Sendmail::mailcfg{"retries"}
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr))
$retried < $Mail::Sendmail::mailcfg{"retries"}
Truth table33%1/3
not(A) and B
A= not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr))B= $retried < $Mail::Sendmail::mailcfg{"retries"}
| |||||||||||||||||||||||||||||||||||
| 366 | and ( $retried < $mailcfg{'retries'} ) | ||||||||||||||||||||||||||||||||||
| 367 | ) { | ||||||||||||||||||||||||||||||||||
| 368 | 0 | $retried++; | |||||||||||||||||||||||||||||||||
| 369 | 0 | $error .= "connect to $server failed ($!)\n"; | |||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | print "- connect to $server failed ($!)\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | print "retrying in $mailcfg{'delay'} seconds...\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 372 | 0 | sleep $mailcfg{'delay'}; | |||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | 1 | ▶ | if ( $connected ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 376 | 1 | ▶ | print "- connected to $server\n" if $mailcfg{'debug'} > 3; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 377 | 1 | last; | |||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||
| 379 | else { | ||||||||||||||||||||||||||||||||||
| 380 | 0 | $error .= "connect to $server failed\n"; | |||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | print "- connect to $server failed, next server...\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 382 | 0 | next; # next server | |||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | 1 | ▶ | unless ( $connected ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 387 | 0 | return fail("connect to $smtp failed ($!) no (more) retries!") | |||||||||||||||||||||||||||||||||
| 388 | }; | ||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | { | ||||||||||||||||||||||||||||||||||
| 391 | 1 | local $^W = 0; # don't warn on undefined variables | |||||||||||||||||||||||||||||||||
| 392 | # Add info to log variable | ||||||||||||||||||||||||||||||||||
| 393 | 1 | $log .= "Server: $smtp Port: $port\n" | |||||||||||||||||||||||||||||||||
| 394 | . "From: $fromaddr\n" | ||||||||||||||||||||||||||||||||||
| 395 | . "Subject: $mail{Subject}\n" | ||||||||||||||||||||||||||||||||||
| 396 | ; | ||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | 1 | my($oldfh) = select(S); $| = 1; select($oldfh); | |||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||
| 401 | 1 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 402 | || return fail("Connection error from $smtp on port $port ($_)"); | ||||||||||||||||||||||||||||||||||
| 403 | 1 | ▶ | socket_write("EHLO $localhost$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 404 | || return fail("send EHLO error (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 405 | 1 | my $ehlo = socket_read(); | |||||||||||||||||||||||||||||||||
| 406 | 1 | ▶ | if ($ehlo) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 407 | # parse EHLO response | ||||||||||||||||||||||||||||||||||
| 408 | map { | ||||||||||||||||||||||||||||||||||
| 409 | 1 | s/^\d+[- ]//; | |||||||||||||||||||||||||||||||||
| 410 | 12 | my ($k, $v) = split /\s+/, $_, 2; | |||||||||||||||||||||||||||||||||
| 411 | 12 | ▶ | $esmtp{$k} = $v || 1 if $k; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$v || 1
MC/DC100%1/1
$v || 1
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||||||||||||||||
| 412 | } split(/\n/, $ehlo); | ||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||
| 414 | else { | ||||||||||||||||||||||||||||||||||
| 415 | # try plain HELO instead | ||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | socket_write("HELO $localhost$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 417 | || return fail("send HELO error (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||
| 420 | 1 | ▶ | if ($auth) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 421 | 0 | ▶ | warn "AUTH requested\n" if ($mailcfg{debug} > 4); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 422 | # reduce wanted methods to those supported | ||||||||||||||||||||||||||||||||||
| 423 | 0 | my @methods = grep {$esmtp{'AUTH'}=~/(^|\s)$_(\s|$)/i} | |||||||||||||||||||||||||||||||||
| 424 | 0 | grep {$auth_support =~ /(^|\s)$_(\s|$)/i} | |||||||||||||||||||||||||||||||||
| 425 | 0 | grep /\S/, split(/\s+/, $auth->{method}); | |||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | if (@methods) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 428 | # try to authenticate | ||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | if (exists $auth->{pass}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 431 | 0 | $auth->{password} = $auth->{pass}; | |||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||
| 434 | 0 | my $method = uc $methods[0]; | |||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | _require_base64() || fail("Could not use MIME::Base64 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | if ($method eq "LOGIN") { | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | print STDERR "Trying AUTH LOGIN\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | socket_write("AUTH LOGIN$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 439 | || return fail("send AUTH LOGIN failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 441 | || return fail("AUTH LOGIN failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 442 | 0 | ▶ | socket_write(encode_base64($auth->{user},$CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 443 | || return fail("send LOGIN username failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 445 | || return fail("LOGIN username failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | socket_write(encode_base64($auth->{password},$CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 447 | || return fail("send LOGIN password failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 449 | || return fail("LOGIN password failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||
| 451 | elsif ($method eq "PLAIN") { | ||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | warn "Trying AUTH PLAIN\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 453 | socket_write( | ||||||||||||||||||||||||||||||||||
| 454 | "AUTH PLAIN " | ||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | . encode_base64(join("\0", $auth->{user}, $auth->{user}, $auth->{password}), $CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 456 | ) || return fail("send AUTH PLAIN failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 458 | || return fail("AUTH PLAIN failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||
| 460 | elsif ($method eq "CRAM-MD5") { | ||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | _require_md5() || fail("Could not use Digest::MD5 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 462 | 0 | ▶ | warn "Trying AUTH CRAM-MD5\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | socket_write("AUTH CRAM-MD5$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 464 | || return fail("send CRAM-MD5 failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | my $challenge = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH CRAM-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH CRAM-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 466 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 467 | 0 | $challenge =~ s/^\d+\s+//; | |||||||||||||||||||||||||||||||||
| 468 | 0 | my $response = _hmac_md5($auth->{password}, decode_base64($challenge)); | |||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | socket_write(encode_base64("$auth->{user} $response", $CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 470 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 472 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||
| 474 | elsif ($method eq "DIGEST-MD5") { | ||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | _require_md5() || fail("Could not use Digest::MD5 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 476 | 0 | ▶ | warn "Trying AUTH DIGEST-MD5\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | socket_write("AUTH DIGEST-MD5$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 478 | || return fail("send CRAM-MD5 failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | my $challenge = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 480 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 481 | 0 | $challenge =~ s/^\d+\s+//; $challenge =~ s/[\r\n]+$//; | |||||||||||||||||||||||||||||||||
| 482 | 0 | ▶ | warn "\nCHALLENGE=", decode_base64($challenge), "\n" if ($mailcfg{debug} > 10); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 483 | 0 | my $response = _digest_md5($auth->{user}, $auth->{password}, decode_base64($challenge), $auth->{realm}); | |||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | warn "\nRESPONSE=$response\n" if ($mailcfg{debug} > 10); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | socket_write(encode_base64($response, ""), $CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 486 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | my $status = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 488 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | if ($status =~ /^3/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 490 | 0 | ▶ | socket_write($CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 491 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 493 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||
| 496 | else { | ||||||||||||||||||||||||||||||||||
| 497 | 0 | return fail("$method not supported (and wrongly advertised as supported by this silly module)\n"); | |||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||
| 499 | 0 | $log .= "AUTH $method succeeded as user $auth->{user}\n"; | |||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||
| 501 | else { | ||||||||||||||||||||||||||||||||||
| 502 | 0 | $esmtp{'AUTH'} =~ s/(^\s+|\s+$)//g; # cleanup for printig it below | |||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | if ($auth->{required}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 504 | 0 | return fail("Required AUTH method '$auth->{method}' not supported. " | |||||||||||||||||||||||||||||||||
| 505 | ."(Server supports '$esmtp{'AUTH'}'. Module supports: '$auth_support')"); | ||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||
| 507 | else { | ||||||||||||||||||||||||||||||||||
| 508 | 0 | warn "No common authentication method! Requested: '$auth->{method}'. Server supports '$esmtp{'AUTH'}'. Module supports: '$auth_support'. Skipping authentication\n"; | |||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||
| 512 | 1 | ▶ | socket_write("MAIL FROM:<$fromaddr>$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 513 | || return fail("send MAIL FROM: error"); | ||||||||||||||||||||||||||||||||||
| 514 | 1 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 515 | || return fail("MAIL FROM: error ($_)"); | ||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||
| 517 | 1 | my $to_ok = 0; | |||||||||||||||||||||||||||||||||
| 518 | 1 | foreach $to (@recipients) { | |||||||||||||||||||||||||||||||||
| 519 | 1 | ▶ | socket_write("RCPT TO:<$to>$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 520 | || return fail("send RCPT TO: error"); | ||||||||||||||||||||||||||||||||||
| 521 | 1 | ▶ | if (socket_read()) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 522 | 1 | $log .= "To: $to\n"; | |||||||||||||||||||||||||||||||||
| 523 | 1 | $to_ok++; | |||||||||||||||||||||||||||||||||
| 524 | } else { | ||||||||||||||||||||||||||||||||||
| 525 | 0 | $log .= "FAILED To: $to ($server_reply)"; | |||||||||||||||||||||||||||||||||
| 526 | 0 | $error .= "Bad recipient <$to>: $server_reply\n"; | |||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||
| 529 | 1 | ▶ | unless ($to_ok) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 530 | 0 | return fail("No valid recipient"); | |||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | # start data part | ||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||
| 535 | 1 | ▶ | socket_write("DATA$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 536 | || return fail("send DATA error"); | ||||||||||||||||||||||||||||||||||
| 537 | 1 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 538 | || return fail("DATA error ($_)"); | ||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||
| 540 | # print headers | ||||||||||||||||||||||||||||||||||
| 541 | 1 | foreach $header (keys %mail) { | |||||||||||||||||||||||||||||||||
| 542 | 8 | ▶ | next if $header eq "Message"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 543 | 7 | $mail{$header} =~ s/\s+$//o; # kill possible trailing garbage | |||||||||||||||||||||||||||||||||
| 544 | 7 | ▶ | socket_write("$header: $mail{$header}$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 545 | || return fail("send $header: error"); | ||||||||||||||||||||||||||||||||||
| 546 | }; | ||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||
| 548 | #- test diconnecting from network here, to see what happens | ||||||||||||||||||||||||||||||||||
| 549 | #- print STDERR "DISCONNECT NOW!\n"; | ||||||||||||||||||||||||||||||||||
| 550 | #- sleep 4; | ||||||||||||||||||||||||||||||||||
| 551 | #- print STDERR "trying to continue, expecting an error... \n"; | ||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||
| 553 | # send message body (passed as a reference, in case it's big) | ||||||||||||||||||||||||||||||||||
| 554 | 1 | ▶ | socket_write($CRLF, \$mail{'Message'}, "$CRLF.$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 555 | || return fail("send message error"); | ||||||||||||||||||||||||||||||||||
| 556 | 1 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 557 | || return fail("message transmission error ($_)"); | ||||||||||||||||||||||||||||||||||
| 558 | 1 | $log .= "\nResult: $_"; | |||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||
| 560 | # finish | ||||||||||||||||||||||||||||||||||
| 561 | 1 | ▶ | socket_write("QUIT$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 562 | || return fail("send QUIT error"); | ||||||||||||||||||||||||||||||||||
| 563 | 1 | socket_read(); | |||||||||||||||||||||||||||||||||
| 564 | 1 | close S; | |||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||
| 566 | 1 | return 1; | |||||||||||||||||||||||||||||||||
| 567 | } # end sub sendmail | ||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||
| 569 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 570 | __END__ | ||||||||||||||||||||||||||||||||||