Skip to main content

asyncio SMTP client

Project description

"aiosmtplib TravisCI build status" "aiosmtplib on the Python Package Index" pypi-python-versions pypi-status pypi-license


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.

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

hostname

Server name (or IP) to connect to

port

Server port as an integer. Defaults to 25 if use_tls is False, 465 if use_tls is True.

source_address

The hostname of the client. Defaults to the result of socket.getfqdn(). Note that this call blocks.

timeout

Default timeout value for all operations, in seconds. Defaults to 60.

loop

IOLoop instance to run on. Defaults to asyncio.get_event_loop().

use_tls

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.

validate_certs

Determines if server certificates are validated if using use_tls. Defaults to True.

client_cert

Path to client side certificate, if one is to be used for the TLS connection.

client_cert

Path to client side key, if one is to be used for the TLS connection.

tls_context

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:

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.

STARTTLS

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='smtp.gmail.com', port=587, loop=loop, use_tls=False)
loop.run_until_complete(smtp.connect())
loop.run_until_complete(smtp.starttls())

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


Download files

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

Source Distribution

aiosmtplib-1.0.1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiosmtplib-1.0.1-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file aiosmtplib-1.0.1.tar.gz.

File metadata

  • Download URL: aiosmtplib-1.0.1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiosmtplib-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3fd84ef413c39d641db0bff7815c66f1cdd274c5b952bd144404697e4b941d26
MD5 d0d2a0fde62e0d13b5352c1ffcc9b1ee
BLAKE2b-256 7fdc9e6a0b01eab2c078452fd33485f0cf8adb2d3d24e5b4b475b099e8ee365f

See more details on using hashes here.

File details

Details for the file aiosmtplib-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aiosmtplib-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 baee7fd37bd9395f17f21ec32e15731db3c807eed53be5901ba1b4767f4e382b
MD5 3669cb3c816f49d6aa27ce789833b26d
BLAKE2b-256 a3c1583a61901bd5d45aac3e8bc18c9402d0d876fc51a0dbe8c6128a775fe9e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page