| 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"
MC/DC0%0/1
$args->{"via"} ||= "smtp"
$args->{"via"}
Truth table0%0/2
A
A= $args->{"via"}
| |||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $args->{port} ||= '25'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"port"} ||= 25
MC/DC0%0/1
$args->{"port"} ||= 25
$args->{"port"}
Truth table0%0/2
A
A= $args->{"port"}
| |||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $hosts = $args->{hostname}; | |||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | unless($hosts) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 36 | 0 | { require Net::Config; | |||||||||||||||||||||||||||||||||||||
| 37 | 0 | $hosts = $Net::Config::NetConfig{smtp_hosts}; | |||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | undef $hosts unless @$hosts; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $args->{hostname} = $hosts; | |||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->SUPER::init($args) or return; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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()
}
}
$args->{"helo"} or eval {
do {
require Net::Config;
$Net::Config::NetConfig{"inet_domain"}
}
}
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()
}
}
| |||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $self->{MTS_net_smtp_opts} = +{ Hello => $helo, Debug => ($args->{smtp_debug} || 0) }; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'smtp_debug'} || 0
MC/DC0%0/1
$args->{'smtp_debug'} || 0
$args->{'smtp_debug'}
Truth table0%0/2
A
A= $args->{'smtp_debug'}
| |||||||||||||||||||||||||||||||||||||||
| 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'} || {}
$args{'esmtp_options'} || {}
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'}
Truth table0%0/2
A
A= $args{'esmtp_options'}
| |||||||||||||||||||||||||||||||||||||||
| 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 || '<>'
$args{"from"} or $self->{"MTS_from"} or $message->sender
$args{"from"} or $self->{"MTS_from"}
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
| |||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $from = $from->address if ref $from && $from->isa('Mail::Address'); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $from and $from->isa("Mail::Address")
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")
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 76 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||
| 78 | #### Send | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $server; | |||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if(wantarray) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | { # In LIST context | ||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $server = $self->contactAnyServer | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 84 | or return (0, 500, "Connection Failed", "CONNECT", 0); | ||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $server->mail($from, %send_options) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 110 | or return 0; | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $server->mail($from, %send_options) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 113 | or ($server->quit, return 0); | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | 0 | foreach (@to) | |||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | { next if $server->to($_); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $server->quit; | |||||||||||||||||||||||||||||||||||||
| 118 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $server->datafast(\$out) #!! destroys $out | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 155 | or next; | ||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | 0 | trace "opened SMTP connection to $host."; | |||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if(defined $username) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | { unless($server->auth($username, $password)) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||