++ed by:

1 PAUSE user
1 non-PAUSE user.

Author image Mons Anderson
and 1 contributors


AnyEvent::SMTP::Client - Simple asyncronous SMTP Client


    use AnyEvent::SMTP::Client 'sendmail';
        from => 'mons@cpan.org',
        to   => 'mons@cpan.org', # SMTP host will be detected from addres by MX record
        data => 'Test message '.time().' '.$$,
        cb   => sub {
            if (my $ok = shift) {
                warn "Successfully sent";
            if (my $err = shift) {
                warn "Failed to send: $err";


Asyncronously connect to SMTP server, resolve MX, if needed, then send HELO => MAIL => RCPT => DATA => QUIT and return responce


sendmail ... , cb => $cb->(OK,ERR)

Argument names are case insensitive. So, it may be calles as

    sendmail From => ..., To => ..., ...

and as

    sendmail from => ..., to => ..., ...

Arguments description are below

host => 'smtp.server'

SMTP server to use. Optional. By default will be resolved MX record

port => 2525

SMTP server port. Optional. By default = 25

server => 'some.server:25'

SMTP server. The same as pair of host:port

helo => 'hostname'

HELO message. Optional. By default = hostname()

from => 'mail@addr.ess'
to => 'mail@addr.ess'
to => [ 'mail@addr.ess', ... ]
data => 'Message body'
Message => 'Message body'

Message text. For message composing may be used, for ex: MIME::Lite

timeout => int

Use timeout during network operations

debug => 0 | 1

Enable connection debugging

cb => $cb->(OK,ERR)


When $args{to} is a single argument:

    OK - latest response from server
    If OK is undef, then something failed, see ERR
    ERR - error response from server

When $args{to} is an array:

    OK - hash of success responces or undef.
    keys are addresses, values are responces

    ERR - hash of error responces.
    keys are addresses, values are responces

See examples

cv => AnyEvent->condvar

If passed, used as group callback operand

    sendmail ... cv => $cv, cb => sub { ...; };

is the same as

    sendmail ... cb => sub { ...; $cv->end };


$MAXCON [ = 10]

Maximum number of connections to any host. Default is 10


Per-host configuration for maximum number of connection

Please note, host is hostname passed in argument, or resolved MX record.

So, if passed host = 'localhost'>, should be used $MAXCON{localhost}, if passed host = ''>, should be used $MAXCON{''}

        # set default limit to 20
        $AnyEvent::SMTP::Client::MAXCON = 20;
        # don't limit localhost connections
        $AnyEvent::SMTP::Client::MAXCON{'localhost'} = 0;
        # big limit for one of gmail MX
        $AnyEvent::SMTP::Client::MAXCON{'gmail-smtp-in.l.google.com.'} = 100;


Number of currently active connections


Number of currently active connections per host


Bug reports are welcome in CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-SMTP


Mons Anderson, <mons at cpan.org>


Copyright 2009 Mons Anderson, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.