Skip to main content

smtplibaio

The smtplibaio package provides an SMTP client session object that can be used to send e-mail in an asynchronous way (i.e. using asyncio).

Examples

Let's start with a very basic example, using SMTP_SSL:

import asyncio
from email.headerregistry import Address
from email.message import EmailMessage

from smtplibaio import SMTP, SMTP_SSL


async def send_email():
    from_addr = "bob@example.net"
    to_addr = "alice@example.org"

    message = "Hi Alice !"

    async with SMTP_SSL() as client:
        await client.sendmail(from_addr, to_addr, message)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_email())
    loop.close()

As you can see, the Asynchronous Context Manager makes it really easy to use.

STARTTLS is supported only if you have the aioopenssl module installed. You must tell SMTP to use it upon instantiation:

async def send_email():
    """
    """
    from_addr = "bob@example.net"
    to_addr = "alice@example.org"

    message = "Hi Alice !"

    async with SMTP(use_aioopenssl=True) as client:
    await client.starttls()
        await client.sendmail(from_addr, to_addr, message)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_email())
    loop.close()

In the next example, we are specifying the server hostname and port, we are using authentication and we are using the objects provided by the email package available in the Python Standard Library (i.e. email.message.EmailMessage) to build a proper email message.

async def send_email():
    # SMTP server:
    smtp_server = "smtp.example.org"
    port = 587

    # Credentials used to authenticate:
    username = "alice"
    passwd = "5ecreT!"

    # Use of Address object is not mandatory:
    from_addr = Address("Alice", "alice", "example.org")
    to_addr = Address("Bob", "bob", "example.net")
    bcc_addr = Address("John", "john", "example.net")

    # E-mail subject and content:
    subject = "Testing smtplibaio"
    content = "Look, all emails sent from this method are BCCed to John !"

    # Build the list of recipients (To + Bcc):
    recipients = [to_addr.addr_spec, bcc_addr.addr_spec]

    # Build the EmailMessage object:
    message = EmailMessage()
    message.add_header("From", str(from_addr))
    message.add_header("To", str(to_addr))
    message.add_header("Bcc", str(bcc_addr))
    message.add_header("Subject", subject)
    message.add_header("Content-type", "text/plain", charset="utf-8")
    message.set_content(content)

    # Send the e-mail:
    async with SMTP_SSL(hostname=smtp_server, port=port) as client:
        await client.auth(username, passwd)
        await client.sendmail(from_addr.addr_spec, recipients, message.as_string())


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_email())
    loop.close()

You can also have a more fine-grained control using the lower-level methods.

Supported SMTP commands

  • EHLO - SMTP.ehlo() ;
  • HELO - SMTP.helo() ;
  • STARTTLS - SMTP.starttls() (depending on aioopenssl availability) ;
  • AUTH - SMTP.auth() (LOGIN, PLAIN and CRAM-MD5 mechanisms are suported) ;
  • MAIL FROM - SMTP.mail() ;
  • RCPT TO - SMTP.rcpt() ;
  • VRFY - SMTP.vrfy() ;
  • DATA - SMTP.data() ;
  • EXPN - SMTP.expn() ;
  • NOOP - SMTP.noop() ;
  • QUIT - SMTP.quit() ;
  • HELP - SMTP.help().

Current limitations

  • There is no direct support for Python's email.message.EmailMessage. You can still use email.message.EmailMessage.as_string() or str(email.message.EmailMessage) instead. See the example above for further details.

How to contribute

We use black, isort in combination with pre-commit to ensure one coding style and reduce the risk of merge conflicts. Please [install pre-commit] to ensure your commits also meet these standards. When you see something to improve, have ideas for better tests or documentation, please create issues or create a pull request.

Metadata

Release files for smtplibaio 2.1.2.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smtplibaio 2.1.2.post1
File Size Uploaded
smtplibaio-2.1.2.post1.tar.gz 16.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smtplibaio 2.1.2.post1
File Interpreter ABI Platform
smtplibaio-2.1.2.post1-py3-none-any.whl Python 3 none any Details

Total release size: 32.8 kB

Release files / smtplibaio-2.1.2.post1.tar.gz

Download URL smtplibaio-2.1.2.post1.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f44a679b0defa820fe6b96860402a352a1c33294571844240baa16d4a83b7f86
BLAKE2b-256 checksum
How to use checksums
f55e734d64d33caadb96d7d244de9ddcd4a41bb9386fce8e59bb1a20748c7ef7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.4 Linux/5.3.1-arch1-1-ARCH

Release files / smtplibaio-2.1.2.post1-py3-none-any.whl

Download URL smtplibaio-2.1.2.post1-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2b1ef320ac1df1df5590769fa5657e1d8cee85c7509749e1733cba4437a1a7f
BLAKE2b-256 checksum
How to use checksums
2d098e483ba0f1e896dda450e497b966fa4cadc450e013de10d0b598531d09c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.4 Linux/5.3.1-arch1-1-ARCH

Release history Release notifications | RSS feed

This release

2.1.2.post1 This release

2 release files

2.1.2

2 release files

2.1.1

1 release file

2.1.0

1 release file

2.0.2

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page