This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

asyncio SMTP client

Project Description


aiosmtplib is an SMTP client for use with asyncio.

Basic usage:

import asyncio
import aiosmtplib

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname='localhost', port=1025, loop=loop)

async def send_a_message():
    sender = 'root@localhost'
    recipient = 'somebody@localhost'
    message = "Hello World"
    await smtp.sendmail(sender, [recipient], message)



Python 3.5+, compiled with SSL support, is required.

Connecting to an SMTP server

Initialize a new aiosmtplib.SMTP instance, then run its connect coroutine. Unlike in smtplib, initializing an instance does not automatically connect to the server, as that is a blocking operation.

Allowed arguments to initialize the client (or the connect method):

Server name (or IP) to connect to
Server port as an integer. Defaults to 25 if use_tls is False, 465 if use_tls is True.
The hostname of the client. Defaults to the result of socket.getfqdn(). Note that this call blocks.
Default timeout value for all operations, in seconds. Defaults to 60.
IOLoop instance to run on. Defaults to asyncio.get_event_loop().
If True, make the initial connection to the server over TLS/SSL. Note that if the server supports STARTTLS only, this should be False; see STARTTLS below.
Determines if server certificates are validated if using use_tls. Defaults to True.
Path to client side certificate, if one is to be used for the TLS connection.
Path to client side key, if one is to be used for the TLS connection.
An SSLContext object, used for the TLS connection. Mutually exclusive with client_cert/client_key.

Sending messages

Use SMTP.sendmail to send raw messages. Allowed arguments are:

The address sending this mail.
A list of addresses to send this mail to. A bare string will be treated as a list with 1 address.
The message string to send.
List of options (such as ESMTP 8bitmime) for the mail command.
List of options (such as DSN commands) for all the rcpt commands.

Use SMTP.send_message to send email.message.Message objects.


All commands accept a timeout keyword argument of a numerical value in seconds. This value is used for all socket operations, and will raise STMPTimeoutError if exceeded. Timeout values passed to init or connect will be used as the default value for commands executed on the connection.

The default timeout is 60 seconds.


Many SMTP servers support the STARTTLS extension over port 587. To connect to one of these, set use_tls to False, and call starttls on the client.

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(
    hostname='', port=587, loop=loop, use_tls=False)

Parallel execution

SMTP is a sequential protocol. Multiple commands must be sent to send an email, and they must be sent in the correct sequence. As a consequence of this, executing multiple sendmail tasks in parallell (i.e. with asyncio.gather) is not any more efficient than executing in sequence, as the client must wait until one mail is sent before beginning the next.

If you have a lot of emails to send, consider creating multiple connections (SMTP instances) and splitting the work between them.


aiosmtplib is now feature complete, however test coverage and documentation need a lot of work. Feature requests and bug reports are welcome via Github issues.

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
aiosmtplib-1.0.1-py3-none-any.whl (24.9 kB) Copy SHA256 Checksum SHA256 py3 Wheel Apr 7, 2017
aiosmtplib-1.0.1.tar.gz (19.4 kB) Copy SHA256 Checksum SHA256 Source Apr 7, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting