Simple sending of smtp emails using environment variables
pip install --upgrade envsmtp
By default (STARTTLS + auth mode), you must set SMTP_USER and SMTP_PASS in your environment.
To send without TLS/SSL/auth, set:
SMTP_USE_TLS=falseSMTP_USE_SSL=falseSMTP_NO_AUTH=trueSMTP_FROM=sender@example.com(required wheneverSMTP_USERis not set, regardless of auth mode, or if you want to use a different sender than the SMTP_USER email)
Alternatively, you may set both SMTP_FROM_NAME and SMTP_FROM_EMAIL instead of SMTP_FROM:
SMTP_FROM_NAME=TestsSMTP_FROM_EMAIL=tests@example.com
See additional optional settings in example.env
This package defaults to STARTTLS on smtp.gmail.com:587 with SMTP auth enabled.
You can change host/port with SMTP_HOST and SMTP_PORT, and transport/auth behavior with:
SMTP_USE_TLS(default:true)SMTP_USE_SSL(default:false)SMTP_NO_AUTH(default:false)
Once installed, here's a simple example of how to use this package:
from envsmtp import EmailMessage
msg = EmailMessage(
sender="sender@example.com",
recipients="recipient@example.com",
subject="envsmtp test",
body="This is just a test message",
)
msg.smtp_send()
To send with attachments:
from envsmtp import EmailMessage, EmailAttachment
attachments = [
EmailAttachment(content='/path/to/file.txt'),
EmailAttachment(content=b'randombytes', filename='bytes_test.txt'),
EmailAttachment(content='/path/to/another.txt', filename='this_name_is_different_.txt')
]
msg = EmailMessage(
sender="sender@example.com",
recipients="recipient@example.com",
subject="envsmtp test",
body="This is just a test message",
attachments=attachments,
)
msg.smtp_send()
Supports Python 3.10 and above (currently tested in CI on 3.10-3.13). Project dependencies are defined in pyproject.toml.
Install project + dev dependencies:
uv sync --locked --group dev
Run all tests (default + integration) with coverage using a single command:
just test
Integration tests require SMTP_USER, SMTP_PASS, and SMTP_TEST. If missing, integration tests are skipped.
For bugs / feature requests please submit issues
If you would like to contribute to this project, you are welcome to submit a pull request
This project is being developed independently, we provide the package "as-is" without any implied warranty or liability, usage is your own responsibility
Just because I like badges