emails
The 'emails' library is a modern Python package designed for building, templating, transforming, signing, and sending emails. It provides a high-level API to compose HTML and plain-text messages, attach files, embed inline images, render templates, apply HTML transformations, sign with DKIM, and send through SMTP without manually constructing MIME trees. Currently at version 1.1.1, the library is actively maintained with regular releases addressing new features and bug fixes.
Warnings
- breaking Starting with version 1.0.0, 'emails' requires Python 3.10 or newer. Older Python 3.x versions (e.g., 3.9) are no longer supported.
- breaking HTML transformation dependencies (like `cssutils`, `lxml`, `chardet`, `requests`, `premailer`) are no longer installed by default since version 1.0.0. If you use features like CSS inlining or image embedding, you must install the `html` extra.
- breaking The vendored `emails.packages.dkim` module was replaced with the upstream `dkimpy` package in version 1.0.0. Direct imports from `emails.packages.dkim` will no longer work.
- gotcha Jinja2 template support is an optional dependency. If you intend to use `JinjaTemplate` or other Jinja2-based features, you must install the `jinja` extra.
- gotcha The `cc` and `bcc` parameters in the `Message` constructor are for setting email *headers* (e.g., to make recipients visible), not for adding recipients to the actual delivery list. All recipients, including CC/BCC recipients who should receive the email, must be specified in the `to` parameter of the `send()` method.
Install
-
pip install emails -
pip install "emails[html]" -
pip install "emails[jinja]" -
pip install "emails[async]"
Imports
- emails
import emails
Quickstart
import emails
import os
# Configure SMTP details using environment variables for security
SMTP_HOST = os.environ.get('SMTP_HOST', 'smtp.example.com')
SMTP_PORT = int(os.environ.get('SMTP_PORT', 587))
SMTP_USER = os.environ.get('SMTP_USER', 'billing@example.com')
SMTP_PASSWORD = os.environ.get('SMTP_PASSWORD', 'your-app-password')
recipient_email = os.environ.get('RECIPIENT_EMAIL', 'customer@example.com')
sender_email = os.environ.get('SENDER_EMAIL', 'billing@example.com')
sender_name = os.environ.get('SENDER_NAME', 'Billing Dept.')
message = emails.html(
subject="Your Receipt from Example Store",
html="<p>Hello!</p><p>Your payment was successfully received for Order #12345.</p><p>Thank you for your business!</p>",
mail_from=(sender_name, sender_email)
)
# Example of attaching a file (replace with a real file path if needed)
# with open("receipt.pdf", "rb") as f:
# message.attach(filename="receipt.pdf", data=f.read())
try:
response = message.send(
to=recipient_email,
smtp={
"host": SMTP_HOST,
"port": SMTP_PORT,
"tls": True, # Use TLS for secure connection
"user": SMTP_USER,
"password": SMTP_PASSWORD,
},
)
if response.status_code == 250:
print("Email sent successfully!")
else:
print(f"Failed to send email. Status code: {response.status_code}, Message: {response.message}")
except Exception as e:
print(f"An error occurred: {e}")