Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.5.3 instead.
Reason given by maintainers: Allows for an insecure version of cryptography to be used.

SMTPDFix: Test email, locally

build

A simple SMTP server based on aiosmtpd for use as a fixture with pytest that supports encryption and authentication. All this does is receives messages and appends them to a list as an email.Message.

This fixture is intended to address use-cases where to test an application that sends an email it needs to be intercepted for subsequent processing. For example, sending an email with a code for password reset or two-factor authentication. This fixture allows a test to trigger the email being sent, ensure that it's sent, and read the code from the email.

Installing

Install using pip:

pip install smtpdfix

Or, if you're using setuptools, it can be included in the tests_require section of a setup.py file:

setup(
    ...
    tests_require = [
        "pytest",
        "smtpdfix",
    ],
)

Using

To use the smtpd add pytest_plugins = "smtpfix" to your code. for example:

# test_mail.py
from smtplib import SMTP

from smtpdfix import smtpd


def test_sendmail(smtpd):
    from_addr = "from.addr@example.org"
    to_addrs = "to.addr@example.org"
    msg = f"From: {from_addr}\r\nTo: {to_addrs}\r\nSubject: Foo\r\n\r\nFoo bar"

    with SMTP(smtpd.hostname, smtpd.port) as client:
        client.sendmail(from_addr, to_addrs, msg)

    assert len(smtpd.messages) == 1

To use STARTTLS:

from smtplib import SMTP

import pytest

pytest_plugins = "smtpdfix"


def test_sendmail(monkeypatch, smtpd):
    monkeypatch.setenv('SMTPD_USE_STARTTLS', 'True')
    from_ = "from.addr@example.org"
    to_ = "to.addr@example.org"
    msg = f"From: {from_}\r\nTo: {to_}\r\nSubject: Foo\r\n\r\nFoo bar"

    with SMTP(smtpd.hostname, smtpd.port) as client:
        client.starttls()  # Note that you need to call starttls first.
        client.sendmail(from_addr, to_addrs, msg)

    assert len(smtpd.messages) == 1

The certificates included with the fixture will work for addresses localhost, localhost.localdomain, 127.0.0.1, 0.0.0.1, ::1. If using other addresses the key (key.pem) and certificate (cert.pem) must be in a location specified under SMTP_SSL_CERTS_PATH.

Configuration

Configuration can be handled through environment variables:

Variable Default Description
SMTPD_HOST 127.0.0.1 The hostname that the fixture will listen on.
SMTPD_PORT 8025 The port that the fixture will listen on.
SMPTD_USERNAME user
SMTPD_PASSWORD password
SMTPD_USE_SSL False Whether the fixture should use fixed TLS/SSL for transactions. If using smtplib requires that SMTP_SSL be used instead of SMTP.
SMTPD_USE_STARTTLS False Whether the fixture should use StartTLS to encrypt the connections. If using smptlib requires that the SMTP.starttls() be called before other commands are issued.
SMTPD_ENFORCE_AUTH False If set to true then the fixture refuses MAIL, RCPT, DATA commands until authentication is completed.
SMTPD_SSL_CERTS_PATH \certs\ The path to the key and certificate for encrypted communication.

If these variables are included in a .env file they'll be loaded automatically.

Developing

To develop and test smtpdfix you will need to install pytest-asyncio to run asynchronous tests, isort to sort imports and flake8 to lint. To install in a virtual environment for development:

python -m venv venv
./venv/scripts/activate
pip install -e .[dev]

Code is tested using pytest:

pytest

Before submitting a pull request with your changes you should ensure that all imports are sorted and that the code passes linting with flake8.

# Sorting the imports
isort .

# Linting the code
flake8 .

Known Issues

  • Firewalls may interfere with the operation of the smtp server.
  • Authenticating with LOGIN and PLAIN mechanisms fails over TLS/SSL, but works with STARTTLS. Issue #10
  • Currently no support for termination through signals. Issue #4
  • Key and certificate for encrypted communications must be called key.pem and cert.pem respectively. Issue #15

©2020-2021, Written with ☕ and ❤ in Montreal, QC

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smtpdfix-0.2.6.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

smtpdfix-0.2.6-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file smtpdfix-0.2.6.tar.gz.

File metadata

  • Download URL: smtpdfix-0.2.6.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for smtpdfix-0.2.6.tar.gz
Algorithm Hash digest
SHA256 8afbd79647f9dde254b1fb41b7be6a69b9d08523a1131726d782284b6bfa9c76
MD5 9797f8ef7a2e32db8a418871af096685
BLAKE2b-256 0eebe21e5db88295043c25f4ee20c7474e55b4f141a7b83cf7dd3afc0dfb4d68

See more details on using hashes here.

File details

Details for the file smtpdfix-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: smtpdfix-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for smtpdfix-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8994010455d0a328a007e9bbb537b640b7e0fbcb98612de403d973c1775dd322
MD5 18c53ada8b9f06cce3e8d38c29604c12
BLAKE2b-256 ac2d500dc3c0bcf63623665d200d44f417f7626cd30dd65cad58c4156406c0d5

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

This release

0.2.6 This release

2 files

0.2.5

2 files

0.2.4

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page