| 1 | package Email::Send::SMTP; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '2.202'; | |||||||||||||||||||||||||||
| 5 | 3 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Email::Address 1.80; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | local $Return::Value::NO_CLUCK = 1; | |||||||||||||||||||||||||||
| 10 | 3 | require Return::Value; | |||||||||||||||||||||||||||
| 11 | 3 | Return::Value->import; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub is_available { | ||||||||||||||||||||||||||
|
Subroutine
is_available: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 17 | 1 | my $success = 1; | |||||||||||||||||||||||||||
| 18 | 1 | $success = eval { require Net::SMTP }; | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | $success = eval { require Net::SMTP::SSL } if $args{ssl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $success = eval { require Net::SMTP::TLS } if $args{tls}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | return $success | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | ? success | ||||||||||||||||||||||||||||
| 23 | : failure $@; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub get_env_sender { | ||||||||||||||||||||||||||
|
Subroutine
get_env_sender: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | my ($class, $message) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return unless my $hdr = $message->header('From'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | my $from = (Email::Address->parse($hdr))[0]->address; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub get_env_recipients { | ||||||||||||||||||||||||||
|
Subroutine
get_env_recipients: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | my ($class, $message) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my %to = map { $_->address => 1 } | |||||||||||||||||||||||||||
| 37 | 0 | map { Email::Address->parse($_) } | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | grep { defined and length } | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 39 | 0 | map { $message->header($_) } | |||||||||||||||||||||||||||
| 40 | qw(To Cc Bcc); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | return keys %to; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=20 SCAR=60.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($class, $message, @args) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my %args; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( @args % 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $host = shift @args; | |||||||||||||||||||||||||||
| 51 | 0 | %args = @args; | |||||||||||||||||||||||||||
| 52 | 0 | $args{Host} = $host; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 0 | %args = @args; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $host = delete($args{Host}) || 'localhost'; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'Host'} || 'localhost'
MC/DC0%0/1
delete $args{'Host'} || 'localhost'
delete $args{'Host'}
Truth table0%0/2
A
A= delete $args{'Host'}
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | my $smtp_class = $args{ssl} ? 'Net::SMTP::SSL' | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | : $args{tls} ? 'Net::SMTP::TLS' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | : 'Net::SMTP'; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | eval "require $smtp_class; 1" or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $smtp_class eq 'Net::SMTP::TLS' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | # ::TLS has different User/Password than the rest | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $args{User} ||= $args{username}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{"User"} ||= $args{"username"}
MC/DC0%0/2
$args{"User"} ||= $args{"username"}
$args{"User"}
$args{"username"}
Truth table0%0/3
A ||= B
A= $args{"User"}B= $args{"username"}
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $args{Password} ||= $args{password}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{"Password"} ||= $args{"password"}
MC/DC0%0/2
$args{"Password"} ||= $args{"password"}
$args{"Password"}
$args{"password"}
Truth table0%0/3
A ||= B
A= $args{"Password"}B= $args{"password"}
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $SMTP = $smtp_class->new($host, %args); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | return failure "Couldn't connect to $host" unless $SMTP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my ($user, $pass) = @args{qw[username password]}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # for ::TLS, the auth is done by the new() | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $user and not $smtp_class eq 'Net::SMTP::TLS' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$user and not $smtp_class eq "Net::SMTP::TLS"
MC/DC0%0/2
$user and not $smtp_class eq "Net::SMTP::TLS"
$user
not $smtp_class eq "Net::SMTP::TLS"
Truth table0%0/3
A and not(B)
A= $userB= not $smtp_class eq "Net::SMTP::TLS"
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $SMTP->auth($user, $pass) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | or return failure "Couldn't authenticate '$user:...'"; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my @bad; | |||||||||||||||||||||||||||
| 83 | 0 | eval { | |||||||||||||||||||||||||||
| 84 | 0 | my $from = $class->get_env_sender($message); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # ::TLS has no useful return value, but will croak on failure. | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | eval { $SMTP->mail($from); 1 } or return failure "FROM: <$from> denied"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my @to = $class->get_env_recipients($message); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (eval { $SMTP->isa('Net::SMTP::TLS') }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $SMTP->to(@to); | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 0 | my @ok = $SMTP->to(@to, { SkipBad => 1 }); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( @to != @ok ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | my %to; @to{@to} = (1) x @to; | |||||||||||||||||||||||||||
| 98 | 0 | delete @to{@ok}; | |||||||||||||||||||||||||||
| 99 | 0 | @bad = keys %to; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | return failure "No valid recipients" if @bad == @to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | return failure $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( $smtp_class eq 'Net::SMTP::TLS' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $SMTP->data; | |||||||||||||||||||||||||||
| 110 | 0 | $SMTP->datasend( $message->as_string ); | |||||||||||||||||||||||||||
| 111 | 0 | $SMTP->dataend; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | return failure "Can't send data" unless $SMTP->data( $message->as_string ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $SMTP->quit; | |||||||||||||||||||||||||||
| 118 | 0 | return success "Message sent", prop => { bad => [ @bad ], }; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||