| 1 | package Net::Async::SMTP::Client; | ||||||||||||||||||||
| 2 | 1 | $Net::Async::SMTP::Client::VERSION = '0.002'; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Net::Async::SMTP::Client - sending email with IO::Async | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | version 0.002 | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | #!/usr/bin/env perl | ||||||||||||||||||||
| 18 | use strict; | ||||||||||||||||||||
| 19 | use warnings; | ||||||||||||||||||||
| 20 | use IO::Async::Loop; | ||||||||||||||||||||
| 21 | use Net::Async::SMTP::Client; | ||||||||||||||||||||
| 22 | use Email::Simple; | ||||||||||||||||||||
| 23 | my $email = Email::Simple->create( | ||||||||||||||||||||
| 24 | header => [ | ||||||||||||||||||||
| 25 | From => 'someone@example.com', | ||||||||||||||||||||
| 26 | To => 'other@example.com', | ||||||||||||||||||||
| 27 | Subject => 'NaSMTP test', | ||||||||||||||||||||
| 28 | ], | ||||||||||||||||||||
| 29 | attributes => { | ||||||||||||||||||||
| 30 | encoding => "8bitmime", | ||||||||||||||||||||
| 31 | charset => "UTF-8", | ||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||
| 33 | body_str => '... text ...', | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 36 | $loop->add( | ||||||||||||||||||||
| 37 | my $smtp = Net::Async::SMTP::Client->new( | ||||||||||||||||||||
| 38 | domain => 'example.com', | ||||||||||||||||||||
| 39 | ) | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | $smtp->connected->then(sub { | ||||||||||||||||||||
| 42 | $smtp->login( | ||||||||||||||||||||
| 43 | user => '...', | ||||||||||||||||||||
| 44 | pass => '...', | ||||||||||||||||||||
| 45 | ) | ||||||||||||||||||||
| 46 | })->then(sub { | ||||||||||||||||||||
| 47 | $smtp->send( | ||||||||||||||||||||
| 48 | to => 'someone@example.com', | ||||||||||||||||||||
| 49 | from => 'other@example.com', | ||||||||||||||||||||
| 50 | data => $email->as_string, | ||||||||||||||||||||
| 51 | ) | ||||||||||||||||||||
| 52 | })->get; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | Provides basic email sending capability for L<IO::Async>, using | ||||||||||||||||||||
| 57 | the L<Protocol::SMTP> implementation. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | See L<Protocol::SMTP/DESCRIPTION> for a list of supported features | ||||||||||||||||||||
| 60 | and usage instructions. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | use IO::Async::Resolver::DNS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 1 | ▶ | use Future::Utils qw(try_repeat_until_success); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | ▶ | use Net::Async::SMTP::Connection; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head2 connection | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | Establishes or returns the TCP connection to the SMTP server. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =over 4 | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =item * If we had a host, we'll connect directly. | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =item * If we have a domain, then we'll do an MX lookup on it. | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =item * If we don't have either, you'll probably just see errors | ||||||||||||||||||||
| 82 | or unresolved futures. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Returns the L<Future> representing the connection. Attach events via | ||||||||||||||||||||
| 87 | methods on L<Future> such as C<on_done>, C<then> etc. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | See also: L</connected> | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub connection { | ||||||||||||||||||||
| 94 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
connection: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 95 | (defined($self->host) | ||||||||||||||||||||
| 96 | ? Future->wrap($self->host) | ||||||||||||||||||||
| 97 | : $self->mx_lookup($self->domain))->then(sub { | ||||||||||||||||||||
| 98 | 0 | ▶ | my @hosts = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 99 | try_repeat_until_success { | ||||||||||||||||||||
| 100 | 0 | ▶ | my $host = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 101 | 0 | $self->debug_printf("Trying connection to [%s]", $host); | |||||||||||||||||||
| 102 | $self->loop->connect( | ||||||||||||||||||||
| 103 | socktype => 'stream', | ||||||||||||||||||||
| 104 | host => $host, | ||||||||||||||||||||
| 105 | service => $self->port || 'smtp', | ||||||||||||||||||||
| 106 | )->on_fail(sub { | ||||||||||||||||||||
| 107 | 0 | ▶ | $self->debug_printf("Failed connection to [%s], have %d left to try", $host, scalar @hosts); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 108 | }) | ||||||||||||||||||||
| 109 | 0 | ▶ | } foreach => \@hosts; | ||||||||||||||||||
|
Condition0%0/2
$self->port || 'smtp'
MC/DC0%0/1
$self->port || 'smtp'
$self->port
Truth table0%0/2
A
A= $self->port
| |||||||||||||||||||||
| 110 | 0 | ▶ | }); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head2 mx_lookup | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Looks up MX records for the given domain. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Returns a L<Future> which will resolve to the list of records found. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | sub mx_lookup { | ||||||||||||||||||||
| 122 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
mx_lookup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | 0 | my $domain = shift; | |||||||||||||||||||
| 124 | 0 | my $resolver = $self->loop->resolver; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # Wrap the resolver query as a Future | ||||||||||||||||||||
| 127 | 0 | my $f = $self->loop->new_future; | |||||||||||||||||||
| 128 | $resolver->res_query( | ||||||||||||||||||||
| 129 | dname => $domain, | ||||||||||||||||||||
| 130 | type => "MX", | ||||||||||||||||||||
| 131 | on_resolved => sub { | ||||||||||||||||||||
| 132 | 0 | ▶ | $f->done(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 133 | 0 | undef $f; | |||||||||||||||||||
| 134 | }, | ||||||||||||||||||||
| 135 | on_error => sub { | ||||||||||||||||||||
| 136 | 0 | ▶ | $f->fail(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 137 | 0 | undef $f; | |||||||||||||||||||
| 138 | }, | ||||||||||||||||||||
| 139 | 0 | ); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # ... and return just the list of hosts we want to contact as our result | ||||||||||||||||||||
| 142 | $f->transform( | ||||||||||||||||||||
| 143 | done => sub { | ||||||||||||||||||||
| 144 | 0 | ▶ | my $pkt = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 145 | 0 | my @host; | |||||||||||||||||||
| 146 | 0 | foreach my $mx ( $pkt->answer ) { | |||||||||||||||||||
| 147 | 0 | ▶ | next unless $mx->type eq "MX"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 0 | push @host, [ $mx->preference, $mx->exchange ]; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | # sort things - possibly already handled by the resolver | ||||||||||||||||||||
| 151 | 0 | map $_->[1], sort { $_->[0] <=> $_->[1] } @host; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | 0 | ); | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head2 configure | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Overrides L<IO::Async::Notifier> C<configure> to apply SMTP-specific config. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | sub configure { | ||||||||||||||||||||
| 163 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
configure: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 164 | 0 | my %args = @_; | |||||||||||||||||||
| 165 | 0 | for(grep exists $args{$_}, qw(host user pass auth domain)) { | |||||||||||||||||||
| 166 | 0 | $self->{$_} = delete $args{$_}; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | # SSL support | ||||||||||||||||||||
| 169 | 0 | $self->{$_} = delete $args{$_} for grep /^SSL_/, keys %args; | |||||||||||||||||||
| 170 | 0 | $self->SUPER::configure(%args); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =head2 connected | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | Returns the L<Future> indicating our SMTP connection. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | Resolves to a L<Net::Async::SMTP::Connection> instance on | ||||||||||||||||||||
| 178 | success. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | sub connected { | ||||||||||||||||||||
| 183 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
connected: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 184 | $self->{connected} ||= $self->connection->then(sub { | ||||||||||||||||||||
| 185 | 0 | ▶ | my $sock = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 186 | 0 | ▶ | my $stream = Net::Async::SMTP::Connection->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | handle => $sock, | ||||||||||||||||||||
| 188 | $self->auth | ||||||||||||||||||||
| 189 | ? (auth => $self->auth) | ||||||||||||||||||||
| 190 | : (), | ||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||
| 192 | 0 | $self->add_child($stream); | |||||||||||||||||||
| 193 | $stream->send_greeting->then(sub { | ||||||||||||||||||||
| 194 | 0 | ▶ | return Future->wrap($stream) unless $stream->has_feature('STARTTLS'); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # Currently need to have this loaded to find ->sslwrite | ||||||||||||||||||||
| 197 | 0 | require IO::Async::SSLStream; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 0 | $stream->starttls( | |||||||||||||||||||
| 200 | $self->ssl_parameters | ||||||||||||||||||||
| 201 | ) | ||||||||||||||||||||
| 202 | 0 | }); | |||||||||||||||||||
| 203 | 0 | ▶ | }); | ||||||||||||||||||
|
Condition0%0/3
$self->{"connected"} ||= $self->connection->then(sub {
my $sock = shift();
my $stream = "Net::Async::SMTP::Connection"->new("handle", $sock, $self->auth ? ("auth", $self->auth) : ());
$self->add_child($stream);
$stream->send_greeting->then(sub {
return "Future"->wrap($stream) unless $stream->has_feature("STARTTLS");
require IO::Async::SSLStream;
$stream->starttls($self->ssl_parameters);
}
);
}
)
MC/DC0%0/2
$self->{"connected"} ||= $self->connection->then(sub {
my $sock = shift();
my $stream = "Net::Async::SMTP::Connection"->new("handle", $sock, $self->auth ? ("auth", $self->auth) : ());
$self->add_child($stream);
$stream->send_greeting->then(sub {
return "Future"->wrap($stream) unless $stream->has_feature("STARTTLS");
require IO::Async::SSLStream;
$stream->starttls($self->ssl_parameters);
}
);
}
)
$self->{"connected"}
$self->connection->then(sub {
my $sock = shift();
my $stream = "Net::Async::SMTP::Connection"->new("handle", $sock, $self->auth ? ("auth", $self->auth) : ());
$self->add_child($stream);
$stream->send_greeting->then(sub {
return "Future"->wrap($stream) unless $stream->has_feature("STARTTLS");
require IO::Async::SSLStream;
$stream->starttls($self->ssl_parameters);
}
);
}
)
Truth table0%0/3
A ||= B
A= $self->{"connected"}B= $self->connection->then(sub {
my $sock = shift();
my $stream = "Net::Async::SMTP::Connection"->new("handle", $sock, $self->auth ? ("auth", $self->auth) : ());
$self->add_child($stream);
$stream->send_greeting->then(sub {
return "Future"->wrap($stream) unless $stream->has_feature("STARTTLS");
require IO::Async::SSLStream;
$stream->starttls($self->ssl_parameters);
}
);
}
)
| |||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =head2 ssl_parameters | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | Returns any defined SSL parameters as passed to the constructor | ||||||||||||||||||||
| 209 | or L</configure>. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | sub ssl_parameters { | ||||||||||||||||||||
| 214 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
ssl_parameters: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 215 | 0 | map { $_, $self->{$_} } grep /^SSL_/, keys %$self; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head2 login | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Attempts login, connecting first if required. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | Returns a L<Future> which will resolve with this instance when the login completes. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | sub login { | ||||||||||||||||||||
| 227 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
login: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 228 | 0 | my %args = @_; | |||||||||||||||||||
| 229 | $self->connected->then(sub { | ||||||||||||||||||||
| 230 | 0 | ▶ | my $connection = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 231 | 0 | $connection->login(%args); | |||||||||||||||||||
| 232 | 0 | }); | |||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head2 send | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Attempts to send message(s), connecting first if required. | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | If this server requires login, you'll need to call L</login> yourself. | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | See L<Protocol::SMTP::Client/send>. | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | Returns a L<Future>. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | sub send { | ||||||||||||||||||||
| 248 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
send: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 249 | 0 | my %args = @_; | |||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | $self->connected->then(sub { | ||||||||||||||||||||
| 252 | 0 | ▶ | my $connection = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 253 | 0 | $connection->send(%args); | |||||||||||||||||||
| 254 | }) | ||||||||||||||||||||
| 255 | 0 | } | |||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =head1 METHODS - Accessors | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =head2 port | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | Returns the port used for communicating with the server, | ||||||||||||||||||||
| 264 | or undef for default (25). | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | 0 | ▶ | sub port { shift->{port} } | ||||||||||||||||||
|
Subroutine
port: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | =head2 host | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | Returns the host we're going to connect to. | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | =cut | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | 0 | ▶ | sub host { shift->{host} } | ||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | =head2 domain | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | Returns the domain used for the email server. | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =cut | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 0 | ▶ | sub domain { shift->{domain} } | ||||||||||||||||||
|
Subroutine
domain: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | =head2 auth | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | Returns the auth method used for server authentication. | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | 0 | ▶ | sub auth { shift->{auth} } | ||||||||||||||||||
|
Subroutine
auth: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | 1 | 1; | |||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | __END__ | ||||||||||||||||||||