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.

pytest SMTPD Fixture

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 fisture 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 fixture import it into your test file and then use it like any other pytest fixture. for example:

# test_mail.py
from smtplib import SMTP

from bebleo_smtpd_fixture 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
from bebleo_smtpd_fixture import smtpd


@pytest.fixture
def mock_use_starttls(monkeypatch):
    monkeypatch.setenv('SMTPD_USE_STARTTLS', 'True')

def test_sendmail(mock_use_starttls, smtpd):
    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.

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, Written with ☕ and ❤ in Montreal, QC

Metadata

Release files for smtpdfix 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smtpdfix 0.2.4
File Size Uploaded
smtpdfix-0.2.4.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smtpdfix 0.2.4
File Interpreter ABI Platform
smtpdfix-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 28.5 kB

Release files / smtpdfix-0.2.4.tar.gz

Download URL smtpdfix-0.2.4.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1e52c12cb9aff4a7a9ab20442539bea6ec08504cf3691074cfca0558ab8ebdc3
BLAKE2b-256 checksum
How to use checksums
53ab81e09fcf51397ccb82eca58dbf147234f18b6c43e13495950760f3bfc138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release files / smtpdfix-0.2.4-py3-none-any.whl

Download URL smtpdfix-0.2.4-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da35a075db42fde8747bb88e4a79a214a115177fac9d4fc59055a2a48b39db69
BLAKE2b-256 checksum
How to use checksums
376ce8af7a728b8bba3ebde01be0aca930d885d147afc731684962059e9dcc15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release history Release notifications | RSS feed

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

This release

0.2.4 This release

2 release 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