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