Simple synchronous SMTP email sender with attachments.
Project description
postsmtp
postsmtp is a lightweight synchronous SMTP email library for Python.
Security policy: see SECURITY.md.
- Send plain text or HTML emails
- Optional CC/BCC/Reply-To
- Optional attachments from files, bytes, or streams
- STARTTLS by default, SSL mode optional
Install
pip install postsmtp
Quick Start
from postsmtp import SmtpConfig, send_email
config = SmtpConfig(
smtp_server="smtp.office365.com",
smtp_port=587,
sender_email="sender@example.com",
password="your-app-password",
use_starttls=True,
)
send_email(
config=config,
to_emails=["john@example.com"],
subject="Hello",
body="Email sent with postsmtp.",
)
HTML Example
from postsmtp import SmtpConfig, SmtpMailer
config = SmtpConfig(
smtp_server="smtp.gmail.com",
smtp_port=587,
sender_email="sender@gmail.com",
password="app-password",
use_starttls=True,
)
mailer = SmtpMailer(config)
mailer.send(
to_emails=["john@example.com"],
subject="HTML Demo",
body="Fallback plain text body.",
html_body="<h2>Hello</h2><p>This is an HTML email.</p>",
)
Two Attachments Example
from postsmtp import (
SmtpConfig,
SmtpMailer,
attachment_from_file,
)
config = SmtpConfig(
smtp_server="smtp.office365.com",
smtp_port=587,
sender_email="sender@example.com",
password="app-password",
use_starttls=True,
)
attachment_1 = attachment_from_file("invoice_1001.pdf")
attachment_2 = attachment_from_file("invoice_1002.pdf")
mailer = SmtpMailer(config)
mailer.send(
to_emails=["finance@example.com"],
subject="Invoices",
body="Attached are two invoices.",
attachments=[attachment_1, attachment_2],
)
API
SmtpConfig
Required:
smtp_serversender_emailpassword
Optional:
smtp_port(default:587)username(default:sender_email)use_starttls(default:True)use_ssl(default:False)timeout_seconds(default:30.0)
SmtpMailer.send(...)
Required:
to_emailssubjectbodyorhtml_body
Optional:
html_bodycc_emailsbcc_emailsreply_toattachments
Attachment helpers
attachment_from_file(path, filename=None, content_type=None)attachment_from_bytes(filename, data, content_type=None)attachment_from_stream(filename, stream, content_type=None)
Exceptions
ConfigurationErrorValidationErrorSmtpConnectionErrorAuthenticationErrorSendError
All inherit from PostSmtpError.
Development
python -m venv .venv
source .venv/bin/activate # Windows: .venv\\Scripts\\activate
pip install -e ".[dev]"
pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
postsmtp-0.1.0.tar.gz
(9.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file postsmtp-0.1.0.tar.gz.
File metadata
- Download URL: postsmtp-0.1.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f5e5cccdc0e3255da0f746a800005a089fc9d6cb3037c2e4d4d1c14fbf8fa1d
|
|
| MD5 |
01899f5744c58641c7a1ab62153a282a
|
|
| BLAKE2b-256 |
444fa84350cd20c92928845d9d47d6f1ce849d5b4a1c52c601e6519b13b2400e
|
File details
Details for the file postsmtp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: postsmtp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abf1af723f9fcac935b42c22e493826ccc97aaf52583dd60592c056d5f9ca204
|
|
| MD5 |
5f521fe13a03f168cbb3a1bee533369d
|
|
| BLAKE2b-256 |
934d5f2347f305e0f195922bd21baf2b15ba456282c0d2dfd62ebe96de3975d2
|