| 1 | package AnyEvent::SMTP::Client; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | AnyEvent::SMTP::Client - Simple asyncronous SMTP Client | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use common::sense; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | m{# trying to cheat with cpants game ;) | |||||||||||||||||||||||||||||||||||||||||
| 12 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use base 'Object::Event'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use AnyEvent::Socket; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | ▶ | use AnyEvent::DNS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 11 | ▶ | use AnyEvent::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 11 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 11 | ▶ | use Mail::Address; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 11 | ▶ | use AnyEvent::SMTP::Conn; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 11 | ▶ | our $VERSION = $AnyEvent::SMTP::VERSION;use AnyEvent::SMTP (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # vvv This code was partly derived from AnyEvent::HTTP vvv | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | our $MAXCON = 10; # Maximum number of connections to any host | |||||||||||||||||||||||||||||||||||||||||
| 32 | 11 | our %MAXCON; # Maximum number of connections to concrete host | |||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | our $ACTIVE = 0; # Currently active connections | |||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | our %ACTIVE; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | my %CO_SLOT; # number of open connections, and wait queue, per host | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | sub _slot_schedule; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 84 | ▶ | sub _slot_schedule($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_slot_schedule: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 84 | my $host = shift; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 84 | ▶ | my $mc = exists $MAXCON{$host} ? $MAXCON{$host} : $MAXCON; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 84 | ▶ | while (!$mc or ( $mc > 0 and $CO_SLOT{$host}[0] < $mc )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $mc or $mc > 0 and $CO_SLOT{$host}[0] < $mc
$mc > 0 and $CO_SLOT{$host}[0] < $mc
MC/DC66%2/3
not $mc or $mc > 0 and $CO_SLOT{$host}[0] < $mc
not $mc
$mc > 0
$CO_SLOT{$host}[0] < $mc
Truth table75%3/4
not(A) or B and C
A= not $mcB= $mc > 0C= $CO_SLOT{$host}[0] < $mc
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 90 | ▶ | if (my $cb = shift @{ $CO_SLOT{$host}[1] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | # somebody wants that slot | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 42 | ++$CO_SLOT{$host}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 42 | ++$ACTIVE; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 42 | ++$ACTIVE{$host}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 42 | ▶ | $cb->(AnyEvent::Util::guard { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 42 | --$ACTIVE; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 42 | ▶ | --$ACTIVE{$host} > 0 or delete $ACTIVE{$host}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 42 | --$CO_SLOT{$host}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 51 | #warn "Release slot (have $ACTIVE) by @{[ (caller)[1,2] ]}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 42 | _slot_schedule $host; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 42 | }); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # nobody wants the slot, maybe we can forget about it | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 48 | ▶ | delete $CO_SLOT{$host} unless $CO_SLOT{$host}[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 48 | last; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # wait for a free slot on host, call callback | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 42 | ▶ | sub _get_slot($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_slot: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 42 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 42 | _slot_schedule $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 42 | ▶ | sub _tcp_connect($$$;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tcp_connect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 42 | my ($host,$port,$cb,$pr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | #warn "Need slot $host (have $ACTIVE)"; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 42 | ▶ | _get_slot $host, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 42 | my $sg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 73 | #warn "Have slot $host (have $ACTIVE)"; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 42 | ▶ | tcp_connect($host,$port,sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 42 | $cb->(@_,$sg); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 42 | }, $pr); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 42 | } | |||||||||||||||||||||||||||||||||||||||||
| 79 | # ^^^ This code was partly derived from AnyEvent::HTTP ^^^ | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | use AnyEvent::SMTP::Client 'sendmail'; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | sendmail | ||||||||||||||||||||||||||||||||||||||||||
| 88 | from => 'mons@cpan.org', | ||||||||||||||||||||||||||||||||||||||||||
| 89 | to => 'mons@cpan.org', # SMTP host will be detected from addres by MX record | ||||||||||||||||||||||||||||||||||||||||||
| 90 | data => 'Test message '.time().' '.$$, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | cb => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | if (my $ok = shift) { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | warn "Successfully sent"; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | if (my $err = shift) { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | warn "Failed to send: $err"; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | Asyncronously connect to SMTP server, resolve MX, if needed, then send HELO => MAIL => RCPT => DATA => QUIT and return responce | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =head2 sendmail ... , cb => $cb->(OK,ERR) | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | Argument names are case insensitive. So, it may be calles as | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | sendmail From => ..., To => ..., ... | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | and as | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | sendmail from => ..., to => ..., ... | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | Arguments description are below | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =item host => 'smtp.server' | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | SMTP server to use. Optional. By default will be resolved MX record | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =item port => 2525 | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | SMTP server port. Optional. By default = 25 | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =item server => 'some.server:25' | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | SMTP server. The same as pair of host:port | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =item helo => 'hostname' | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | HELO message. Optional. By default = hostname() | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =item from => 'mail@addr.ess' | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =item to => 'mail@addr.ess' | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =item to => [ 'mail@addr.ess', ... ] | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item data => 'Message body' | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =item Message => 'Message body' | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | Message text. For message composing may be used, for ex: L<MIME::Lite> | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =item timeout => int | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | Use timeout during network operations | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item debug => 0 | 1 | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | Enable connection debugging | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =item cb => $cb->(OK,ERR) | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | Callback. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | When $args{to} is a single argument: | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | OK - latest response from server | ||||||||||||||||||||||||||||||||||||||||||
| 164 | If OK is undef, then something failed, see ERR | ||||||||||||||||||||||||||||||||||||||||||
| 165 | ERR - error response from server | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | When $args{to} is an array: | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | OK - hash of success responces or undef. | ||||||||||||||||||||||||||||||||||||||||||
| 170 | keys are addresses, values are responces | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | ERR - hash of error responces. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | keys are addresses, values are responces | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | See examples | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =item cv => AnyEvent->condvar | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | If passed, used as group callback operand | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | sendmail ... cv => $cv, cb => sub { ...; }; | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | is the same as | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | $cv->begin; | ||||||||||||||||||||||||||||||||||||||||||
| 186 | sendmail ... cb => sub { ...; $cv->end }; | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head1 VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =head2 $MAXCON [ = 10] | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | Maximum number of connections to any host. Default is 10 | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =head2 %MAXCON | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Per-host configuration for maximum number of connection | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | Please note, host is hostname passed in argument, or resolved MX record. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | So, if passed C<host => 'localhost'>, should be used C<$MAXCON{localhost}>, if passed C<host => '127.0.0.1'>, should be used C<$MAXCON{'127.0.0.1'}> | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # set default limit to 20 | ||||||||||||||||||||||||||||||||||||||||||
| 205 | $AnyEvent::SMTP::Client::MAXCON = 20; | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # don't limit localhost connections | ||||||||||||||||||||||||||||||||||||||||||
| 208 | $AnyEvent::SMTP::Client::MAXCON{'localhost'} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # big limit for one of gmail MX | ||||||||||||||||||||||||||||||||||||||||||
| 211 | $AnyEvent::SMTP::Client::MAXCON{'gmail-smtp-in.l.google.com.'} = 100; | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | =head2 $ACTIVE | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | Number of currently active connections | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | =head2 %ACTIVE | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | Number of currently active connections per host | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | my $me = shift; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | my $pkg = caller; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 11 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | @_ or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ( $_ eq 'sendmail') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | *{$pkg.'::'.$_} = \&$_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | require Carp; Carp::croak "$_ is not exported by $me"; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 42 | ▶ | sub sendmail(%) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sendmail: called CC=17 SCAR=38.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 42 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 42 | my @keys = keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 42 | @args{map lc, @keys} = delete @args{ @keys }; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 42 | ▶ | $args{data} ||= delete $args{message} || delete $args{body}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition16%1/6
$args{"data"} ||= delete $args{"message"} || delete $args{"body"}
delete $args{'message'} || delete $args{'body'}
MC/DC0%0/3
$args{"data"} ||= delete $args{"message"} || delete $args{"body"}
$args{"data"}
delete $args{'message'}
delete $args{'body'}
Truth table0%0/4
A ||= B || C
A= $args{"data"}B= delete $args{'message'}C= delete $args{'body'}
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 42 | ▶ | $args{helo} ||= hostname(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{"helo"} ||= hostname()
MC/DC0%0/2
$args{"helo"} ||= hostname()
$args{"helo"}
hostname()
Truth table33%1/3
A ||= B
A= $args{"helo"}B= hostname()
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 42 | ▶ | if ($args{server}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my ($h,$p) = $args{server} =~ /^([^:]+)(?:|:(\d+))$/; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $args{host} = $h or return $args{cb}(undef,"Bad option value for `server'"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | $args{port} = $p if defined $p; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 42 | ▶ | $args{port} ||= 25; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"port"} ||= 25
MC/DC0%0/1
$args{"port"} ||= 25
$args{"port"}
Truth table50%1/2
A
A= $args{"port"}
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 42 | ▶ | $args{timeout} ||= 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"timeout"} ||= 30
MC/DC100%1/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 42 | my ($run,$cv,$res,$err); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 42 | ▶ | $args{cv}->begin if $args{cv}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 42 | $cv = AnyEvent->condvar; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 42 | ▶ | my $end = sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 42 | undef $run; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 42 | undef $cv; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 42 | ▶ | $args{cb}( $res, defined $err ? $err : () ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 42 | ▶ | $args{cv}->end if $args{cv}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 42 | %args = (); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 42 | $cv->begin($end); | |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | ($args{from},my @rcpt) = map { $_->address } map { Mail::Address->parse($_) } $args{from},ref $args{to} ? @{$args{to}} : $args{to}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 42 | ▶ | $run = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 42 | my ($host,$port,@to) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 42 | ▶ | warn "connecting to $host:$port\n" if $args{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 42 | my ($exc,$con,$slot_guard); | |||||||||||||||||||||||||||||||||||||||||
| 269 | 42 | ▶ | my $cb = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 42 | undef $exc; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 42 | ▶ | $con and $con->close; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 42 | undef $slot_guard; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 42 | undef $con; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 42 | ▶ | if (@rcpt > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | #warn "multi cb @to: @_"; | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | @$res{@to} = ($_[0])x@to; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | @$err{@to} = ($_[1])x@to; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | #warn "single cb @to: @_"; | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 42 | ($res,$err) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 42 | $cv->end; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 42 | $cv->begin; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 42 | ▶ | _tcp_connect $host,$port,sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 42 | $slot_guard = pop; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 42 | ▶ | my $fh = shift | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | or return $cb->(undef, "$!"); | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 41 | $con = AnyEvent::SMTP::Conn->new( fh => $fh, debug => $args{debug}, timeout => $args{timeout} ); | |||||||||||||||||||||||||||||||||||||||||
| 293 | $exc = $con->reg_cb( | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | disconnect => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | $con or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $cb->(undef,$_[1]); | |||||||||||||||||||||||||||||||||||||||||
| 297 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 41 | ); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 41 | ▶ | $con->line(ok => 220, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 41 | ▶ | shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 41 | ▶ | $con->command("HELO $args{helo}", ok => 250, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 41 | ▶ | shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 41 | ▶ | $con->command("MAIL FROM:<$args{from}>", ok => 250, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 41 | ▶ | shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 41 | my $cv1 = AnyEvent->condvar; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 41 | ▶ | $cv1->begin(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 41 | undef $cv1; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 41 | ▶ | $con->command("DATA", ok => 354, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 41 | ▶ | shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 41 | $con->reply("$args{data}"); | |||||||||||||||||||||||||||||||||||||||||
| 312 | 41 | ▶ | $con->command(".", ok => 250, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 41 | ▶ | my $reply = shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 41 | $cb->($reply); | |||||||||||||||||||||||||||||||||||||||||
| 315 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 317 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 41 | for ( @to ) { | |||||||||||||||||||||||||||||||||||||||||
| 320 | 41 | $cv1->begin; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 41 | ▶ | $con->command("RCPT TO:<$_>", ok => 250, cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 41 | ▶ | shift or return $cb->(undef, @_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 41 | $cv1->end; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 41 | $cv1->end; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 332 | 42 | ▶ | }, sub { $args{timeout} || 30 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 42 | ▶ | if ($args{host}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 42 | $run->($args{host},$args{port}, @rcpt); | |||||||||||||||||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my %domains; | |||||||||||||||||||||||||||||||||||||||||
| 340 | my $dns = AnyEvent::DNS->new( | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $args{timeout} ? ( timeout => [ $args{timeout} ] ) : () | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | $dns->os_config; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | for (@rcpt) { | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | my ($domain) = /^.+\@(.+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | push @{ $domains{$domain} ||= [] }, $_; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$domains{$domain} ||= []
MC/DC0%0/1
$domains{$domain} ||= []
$domains{$domain}
Truth table0%0/2
A
A= $domains{$domain}
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | for my $domain (keys %domains) { | |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | $cv->begin; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | $dns->resolve( $domain => mx => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | if ($AnyEvent::VERSION > 6.0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | @_ = map $_->[5], sort { $a->[4] <=> $b->[4] } @_; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | @_ = map $_->[4], sort { $a->[3] <=> $b->[3] } @_; | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | warn "MX($domain) = [ @_ ]\n" if $args{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | $run->(shift, $args{port}, @{ delete $domains{$domain} }); | |||||||||||||||||||||||||||||||||||||||||
| 359 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | if (@rcpt > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | @$err{ @{ $domains{$domain} } } = ( "No MX record for domain $domain" )x@{ $domains{$domain} }; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | $err = "No MX record for domain $domain"; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | $cv->end; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | undef $dns; | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 42 | $cv->end; | |||||||||||||||||||||||||||||||||||||||||
| 372 | defined wantarray | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | ? AnyEvent::Util::guard { $end->(undef, "Cancelled"); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 42 | ▶ | : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | Bug reports are welcome in CPAN's request tracker L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-SMTP> | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | Mons Anderson, C<< <mons at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | Copyright 2009 Mons Anderson, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 390 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||