Skip to main content

Authenticate incoming emails with SPF, DKIM, DMARC, and ARC.

Project description

emailsec

builds.sr.ht status PyPI version

emailsec authenticates incoming emails with SPF, DKIM, DMARC, and ARC.

This project is still in early development.

Authentication Protocols

  • SPF (Sender Policy Framework) - RFC 7208
    Verifies the sending IP address is authorized to send email for a domain

  • DKIM (DomainKeys Identified Mail) - RFC 6376
    Validates email authenticity using cryptographic signatures

  • DMARC (Domain-based Message Authentication, Reporting, and Conformance) - RFC 7489
    Combines SPF and DKIM results with policy enforcement

  • ARC (Authenticated Received Chain) - RFC 8617
    Preserves authentication results across email forwarding

Message Authentication

>>> import asyncio
>>> from emailsec import authenticate_message, SMTPContext
>>>
>>> smtp_ctx = SMTPContext(
...     client_ip="203.0.113.42",
...     mail_from="alice@example.com",
... )
>>> raw_email = b"""Received: from mail.example.com (mail.example.com [203.0.113.42])
...     by mx.company.com (Postfix) with ESMTPS id 4A8B2C0FFEE
...     for <recipient@company.com>; Mon, 27 Jan 2025 10:30:45 +0000 (UTC)
... DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com;
...     s=selector1; t=1756656697;
...     h=from:to:subject:date:message-id;
...     bh=eKhvPb2btxd7/zv/sYlR5Z4ws09I2c1WJzPa=;
...     b=C70Bf8rWjJZJt/RcOnoFJquifA1XNB/yiKVP==
... Received-SPF: pass (mx.company.com: domain of example.com designates
...     203.0.113.42 as permitted sender) client-ip=203.0.113.42
... From: Alice <alice@example.com>
... To: Bob <bob@company.com>
... Subject: Hello
... Date: Mon, 27 Jan 2025 10:30:45 +0000
... Message-ID: <20250127103045.4A8B2C0FFEE@mail.example.com>
... MIME-Version: 1.0
... Content-Type: text/plain; charset=UTF-8
...
... Hi Bob,
...
... Cheers,
... Alice
... """
>>>
>>> asyncio.run(authenticate_message(smtp_ctx, raw_email))
AuthenticationResult(
    delivery_action=<DeliveryAction.ACCEPT: 'accept'>,
    spf_result=SPFResult(
        result=<SPFResult.PASS: 'pass'>,
        domain="example.com",
        sender_ip="203.0.113.42",
        exp=""
    ),
    dkim_result=DKIMResult(
        result=<DKIMResult.SUCCESS: 'SUCCESS'>,
        domain="example.com",
        selector="selector1",
        signature={
            'v': '1',
            'a': 'rsa-sha256',
            'c': 'relaxed/relaxed',
            'd': 'example.com',
            'h': 'from:to:subject:date:message-id',
            's': 'selector1',
            't': 1756656697,
            'bh': 'eKhvPb2btxd7/zv/sYlR5Z4ws09I2c1WJzPa...',
            'b': 'C70Bf8rWjJZJt/RcOnoFJquifA1XNB/yiKVP...'
        }
    ),
    dmarc_result=DMARCResult(
        result="pass",
        policy=policy=<DMARCPolicy.QUARANTINE: 'quarantine'>,
        spf_aligned=True,
        dkim_aligned=True,
        arc_override_applied=False
    ),
    arc_result=ARCResult(
        result=<ARCChainStatus.NONE: 'none'>,
        reason="No ARC Sets",
        signer=None,
        aar_header=None
    )
)

Sender Policy Framework

RFC 7208-compliant parser and checker.

Parser

>>> from emailsec.spf.parser import parse_record
>>> parse_record("v=spf1 +a mx/30 mx:example.org/30 -all")
[A(qualifier=<Qualifier.PASS: '+'>, domain_spec=None, cidr=None),
 MX(qualifier=<Qualifier.PASS: '+'>, domain_spec=None, cidr='/30'),
 MX(qualifier=<Qualifier.PASS: '+'>, domain_spec='example.org', cidr='/30'),
 All(qualifier=<Qualifier.FAIL: '-'>)]

Checker

>>> import asyncio
>>> from emailsec.spf import check_spf
>>> asyncio.run(check_spf(sender_ip="192.0.2.10", sender="hello@example.com"))
SPFCheck(result=<SPFResult.PASS: 'pass'>, domain='example.com', explanation='')

Documentation

Project documentation is available at https://emailsec.hexa.ninja/.

Contribution

Contributions are welcome but please open an issue to start a discussion before starting something consequent.

License

Copyright (c) 2025 Thomas Sileo and contributors. Released under the MIT license.

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

emailsec-0.4.0.tar.gz (632.8 kB view details)

Uploaded Source

Built Distribution

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

emailsec-0.4.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file emailsec-0.4.0.tar.gz.

File metadata

  • Download URL: emailsec-0.4.0.tar.gz
  • Upload date:
  • Size: 632.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.3

File hashes

Hashes for emailsec-0.4.0.tar.gz
Algorithm Hash digest
SHA256 23f3a1bc8da5adcced6f7027898c435206bcbd7b6e3da1e7cab0a5a73b42a829
MD5 83d35fbf2257cf28e46765e86d89c14f
BLAKE2b-256 fb9119d46be541ea2374effd34678c34df28b544794706dc69c98f2a8b2cc9ce

See more details on using hashes here.

File details

Details for the file emailsec-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: emailsec-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.3

File hashes

Hashes for emailsec-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f02fa6555e65d237c85bef555062da1a7331596af2d49b198514d02c847909f
MD5 7f1eb01abcaed7b5dc0261e8a85e1044
BLAKE2b-256 373f5d489ca412a04ab86da6145503395d8f14db22e6e55fa8b98cb86b1a90c6

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