SMTP/Client.pm

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