Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

asyncio SMTP client

Project Description


aiosmtplib is an asynchronous SMTP client for use with asyncio.

For complete documentation, see Read The Docs.

Quickstart

import asyncio
from email.mime.text import MIMEText

import aiosmtplib

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname='localhost', port=10025, loop=loop)
loop.run_until_complete(smtp.connect())

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

loop.run_until_complete(smtp.send_message(message))

Requirements

Python 3.5+, 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()
loop.run_until_complete(
    gmail_client.connect(hostname='smtp.gmail.com', port=587))

Sending messages

SMTP.send_message

Use send_message to send email.message.Message objects.

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

loop = asyncio.get_event_loop()
loop.run_until_complete(smtp.send_message(message))

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.

SMTP.sendmail

Use sendmail to send raw messages.

sender = 'root@localhost'
recipients = ['somebody@example.com']
message = '''To: somebody@example.com
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='smtp.gmail.com', port=587, loop=loop, use_tls=False)
loop.run_until_complete(smtp.connect())
loop.run_until_complete(smtp.starttls())

Roadmap

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

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.1.7

History Node

0.1.6

History Node

0.1.5rc2

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1

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.2-py3-none-any.whl (24.8 kB) Copy SHA256 Checksum SHA256 py3 Wheel Aug 27, 2017
aiosmtplib-1.0.2.tar.gz (18.4 kB) Copy SHA256 Checksum SHA256 Source Aug 27, 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