aiosmtplib is an asynchronous SMTP client for use with asyncio. It provides an async version of Python's `smtplib` module with similar APIs, enabling non-blocking email sending and interaction with SMTP servers. The current version is 5.1.0, and the project actively maintains compatibility with recent Python versions while introducing new features like XOAUTH2 authentication.
pip install aiosmtplibVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates sending an email using the high-level `aiosmtplib.send` coroutine. It uses environment variables for configuration and handles common TLS/STARTTLS scenarios. For more complex interactions or persistent connections, the `SMTP` client class with an `async with` context manager is recommended.
Upgrade your Python interpreter to 3.10 or newer, or pin `aiosmtplib` to an older version compatible with your Python environment (e.g., `aiosmtplib<5` for Python 3.9).
Review API calls, explicitly use keyword arguments where applicable, and update `source_address` to a `(addr, port)` tuple. Use `local_hostname` for the client's EHLO/HELO identifier.
For direct TLS/SSL (e.g., port 465), set `use_tls=True`. For STARTTLS (e.g., port 587), connect normally then call `await client.starttls()`, or rely on `aiosmtplib`'s default auto-upgrade behavior for STARTTLS if `start_tls` is not explicitly `False`.
If sending many emails concurrently, create and manage multiple `aiosmtplib.SMTP` client instances, each handling a subset of emails, to achieve true parallel processing.
Verify the SMTP server's status and network accessibility. Check the provided host and port, and ensure no firewalls are blocking the connection. If testing locally, make sure a local SMTP server (e.g., Postfix, SMTPServer from Python's smtpd) is active and listening on the specified port.
Ensure an SMTP server is running and accessible on the specified host and port (e.g., localhost:25). Verify network connectivity and firewall rules if connecting remotely or within isolated environments like containers.
Install the package using pip: 'pip install aiosmtplib'.
Ensure the 'SMTP' object is correctly initialized and connected before calling 'sendmail'.
Verify that all required headers (e.g., 'From', 'To') are correctly set in the email message.
Verify your username and password. If using a service like Gmail, generate an 'App Password' for your application and use that instead of your primary account password, and ensure two-factor authentication is enabled if required for app passwords.
Ensure the SMTP server address and port are correct, check network connectivity, confirm the server is running, and handle potential timeouts by setting appropriate `timeout` values for `connect()` and other operations. Ensure you call `await smtp.connect()` before other commands.
No dependency data recorded yet.