Skip to main content

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_server
  • sender_email
  • password

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_emails
  • subject
  • body or html_body

Optional:

  • html_body
  • cc_emails
  • bcc_emails
  • reply_to
  • attachments

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

  • ConfigurationError
  • ValidationError
  • SmtpConnectionError
  • AuthenticationError
  • SendError

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


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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

postsmtp-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for postsmtp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f5e5cccdc0e3255da0f746a800005a089fc9d6cb3037c2e4d4d1c14fbf8fa1d
MD5 01899f5744c58641c7a1ab62153a282a
BLAKE2b-256 444fa84350cd20c92928845d9d47d6f1ce849d5b4a1c52c601e6519b13b2400e

See more details on using hashes here.

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

Hashes for postsmtp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abf1af723f9fcac935b42c22e493826ccc97aaf52583dd60592c056d5f9ca204
MD5 5f521fe13a03f168cbb3a1bee533369d
BLAKE2b-256 934d5f2347f305e0f195922bd21baf2b15ba456282c0d2dfd62ebe96de3975d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page