asyncio SMTP client
Project description
Introduction
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)
loop.run_until_complete(smtp.connect())
async def send_a_message():
sender = 'root@localhost'
recipient = 'somebody@localhost'
message = "Hello World"
await smtp.sendmail(sender, [recipient], message)
loop.run_until_complete(send_a_message())
Requirements
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.
Sending messages
Use SMTP.sendmail to send raw messages. Allowed arguments are:
- sender
The address sending this mail.
- recipients
A list of addresses to send this mail to. A bare string will be treated as a list with 1 address.
- message
The message string to send.
- mail_options
List of options (such as ESMTP 8bitmime) for the mail command.
- rcpt_options
List of options (such as DSN commands) for all the rcpt commands.
Use SMTP.send_message to send email.message.Message objects.
Timeouts
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.
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.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aiosmtplib-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bba26f2f6278bc3224d154157dd926b3d76260e03c8506a035c8710a6e8fbd62 |
|
MD5 | 8812ac637352b98654649fe17991e608 |
|
BLAKE2b-256 | 3f8a9a1b2c0f0c4a1b21ba3f16ec0e7a84df5d10573e2a96eb2789edf37b6ba0 |