Skip to main content

asyncio SMTP client

Project description

"aiosmtplib TravisCI build status" coveralls "aiosmtplib on the Python Package Index" pypi-python-versions pypi-status pypi-license "Code style: black"

aiosmtplib is an asynchronous SMTP client for use with asyncio.

For complete documentation, see Read The Docs.


import asyncio
from email.mime.text import MIMEText

import aiosmtplib

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

message = MIMEText('Sent via aiosmtplib')
message['From'] = 'root@localhost'
message['To'] = ''
message['Subject'] = 'Hello World!'



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

Connecting to an SMTP server

Initialize a new SMTP instance, then await its connect coroutine. Initializing an instance does not automatically connect to the server, as that is a blocking operation.

gmail_client = SMTP()

loop = asyncio.get_event_loop()
    gmail_client.connect(hostname='', port=587))

Sending messages


Use send_message to send email.message.Message objects.

message = MIMEText('Sent via aiosmtplib')
message['From'] = 'root@localhost'
message['To'] = ''
message['Subject'] = 'Hello World!'

loop = asyncio.get_event_loop()

This is the simplest API, and is the recommended way to send messages, as it makes it easy to set headers correctly and handle multi part messages. For details on creating email.message.Message objects, see the stdlib documentation examples.


Use sendmail to send raw messages.

sender = 'root@localhost'
recipients = ['']
message = '''To:
From: root@localhost
Subject: Hello World!

Sent via aiosmtplib

loop = asyncio.get_event_loop()
loop.run_until_complete(smtp.sendmail(sender, recipients, message))

Note that when using this method, you must format the message headers yourself.

STARTTLS Connections

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

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


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.

Project details

Download files

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

Files for aiosmtplib, version 1.0.3
Filename, size File type Python version Upload date Hashes
Filename, size aiosmtplib-1.0.3-py3-none-any.whl (23.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size aiosmtplib-1.0.3.tar.gz (51.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page