Skip to main content

amelis2dame

A CLI tool and Python library that connects to an IMAP mailbox, fetches S/MIME encrypted emails, decrypts them using a P12/PFX certificate, extracts PDF attachments, and optionally converts them to AEK-EDIFACT (DAME) documents.

Features

  • Connect to any IMAP server and filter emails by subject keyword
  • Decrypt S/MIME encrypted emails using P12/PFX certificates (RSA-OAEP)
  • Deduplicate emails with the same subject (keeps the most recent)
  • Extract PDF attachments to a configurable output directory
  • Rename PDFs using data extracted from the PDF content
  • Convert PDFs to AEK-EDIFACT (.bef) DAME documents
  • Flexible post-processing: mark as seen, delete, or move to a folder
  • Configure via .env file, custom config file, or CLI arguments

Requirements

  • Python 3.13+
  • Linux/Debian system packages: libssl-dev swig python3-dev gcc

Quick Start

1. Install system dependencies (Linux/Debian)

sudo apt-get install libssl-dev swig python3-dev gcc

2. Install amelis2dame

pip install amelis2dame

3. Create your configuration file

cp .env.example .env

Edit .env with your IMAP credentials and certificate path:

IMAP_SERVER=imap.example.com
IMAP_PORT=993
EMAIL_ACCOUNT=you@example.com
EMAIL_PASSWORD=your_imap_password

P12_CERTIFICATE_PATH=./certificate.p12
PFX_PASSWORD=your_certificate_password

SAVE_DIRECTORY=./output
SUBJECT_KEYWORD=Auftrag

4. Run

amelis2dame

PDFs are extracted to ./output/ by default.

Configuration

Configuration is loaded in this priority order: CLI argument > config file > .env file > default.

Configuration file

By default, amelis2dame looks for a .env file in the current working directory. Use --config to point to a different file:

amelis2dame --config /etc/amelis2dame/production.env

All configuration options

Variable CLI flag Default Description
IMAP_SERVER --imap-server — IMAP server hostname
IMAP_PORT --imap-port 993 IMAP server port
EMAIL_ACCOUNT --imap-user — IMAP login username
EMAIL_PASSWORD --imap-pass — IMAP password
P12_CERTIFICATE_PATH --cert — Path to P12/PFX certificate
PFX_PASSWORD --password "" Certificate password
SAVE_DIRECTORY --output ./output Output directory for PDFs
SUBJECT_KEYWORD --subject Auftrag Subject keyword filter
EMAIL_ACTION --email-action mark_seen Action after processing
DUPLICATE_ACTION --duplicate-action mark_seen Action for older duplicates
RENAME_PATTERN --rename — PDF rename pattern (see below)

The .env.example file in the repository contains all options with comments.

Usage

Run with .env config

amelis2dame

Override specific settings

amelis2dame --subject "Invoice" --output ./invoices

Use a custom config file

amelis2dame --config /path/to/custom.env

Full CLI — no config file needed

amelis2dame \
  --imap-server imap.example.com \
  --imap-port 993 \
  --imap-user you@example.com \
  --imap-pass secret \
  --cert certificate.p12 \
  --password pfx_pass \
  --subject "Auftrag" \
  --output ./output \
  --email-action delete \
  --duplicate-action "move:Archive"

Verbose output

amelis2dame -v

Show help

amelis2dame --help

Post-processing actions

After an email is processed, amelis2dame performs one of the following actions. This applies to both EMAIL_ACTION (successfully processed emails) and DUPLICATE_ACTION (older emails with the same subject).

Action Effect
mark_seen Mark the email as read (default)
delete Permanently delete the email
move:FolderName Move to the specified IMAP folder

Example with folder move:

EMAIL_ACTION=move:Processed
DUPLICATE_ACTION=delete

PDF renaming

Set a RENAME_PATTERN (or --rename) to rename extracted PDFs using data extracted from the PDF content.

RENAME_PATTERN="{last_name}_{first_name}_{birth_date}_{barcode_number}.pdf"

Available variables:

Variable Description
{last_name} Last name from PDF
{first_name} First name from PDF
{birth_date} Birth date (DD.MM.YYYY)
{barcode_number} Barcode / sample ID
{samplecollectiondate} Sample collection date (DD.MM.YYYY)
{receipt_datetime} Receipt date and time (DD.MM.YYYY HH:MM)
{finalreport} Final report date (DD.MM.YYYY)
{samplecollectiondate_yyyymmdd} Sample collection date (YYYYMMDD)
{receipt_datetime_yyyymmdd} Receipt date (YYYYMMDD)
{finalreport_yyyymmdd} Final report date (YYYYMMDD)

Example patterns:

# Classic: surname_firstname_birthdate_barcode
RENAME_PATTERN="{last_name}_{first_name}_{birth_date}_{barcode_number}.pdf"

# Date-prefixed for chronological sorting
RENAME_PATTERN="{finalreport_yyyymmdd}_{last_name}_{barcode_number}.pdf"

# Lab-style with sample date
RENAME_PATTERN="Lab_{barcode_number}_{samplecollectiondate_yyyymmdd}.pdf"

Leave RENAME_PATTERN empty (or omit --rename) to keep original filenames.

DAME / AEK-EDIFACT output

amelis2dame can convert extracted PDFs into AEK-EDIFACT (.bef) DAME documents. Both --dame-sender and --dame-receiver must be specified together. The original PDF is replaced by the generated .bef file.

amelis2dame \
  --dame-sender "YOUR_SENDER_ID" \
  --dame-receiver "RECEIVER_ID"

Or in the .env file:

DAME_SENDER=YOUR_SENDER_ID
DAME_RECEIVER=RECEIVER_ID

Workflow

When run, amelis2dame:

  1. Connects to the IMAP server
  2. Fetches emails matching the subject keyword
  3. Deduplicates by subject — keeps the most recent, applies DUPLICATE_ACTION to older ones
  4. Decrypts each S/MIME encrypted message
  5. Extracts PDF attachments to SAVE_DIRECTORY
  6. Optionally renames PDFs using RENAME_PATTERN
  7. Optionally converts PDFs to DAME (.bef) documents
  8. Applies EMAIL_ACTION to the processed email

Security

  • Never commit your .env, .p12, or .pfx files to version control. Add them to .gitignore.
  • Restrict file permissions on certificates and config files: chmod 600 .env certificate.p12
  • Use app-specific passwords for IMAP where available.
  • The certificate's private key is only held in memory during decryption.

Development

Setup

sudo apt-get install libssl-dev swig python3-dev gcc
git clone https://gitlab.com/nerdocs/amelis2dame.git
cd amelis2dame
uv sync

Commands

# Format code
black src/

# Run tests
pytest

# Run tests with coverage
pytest --cov=amelis2dame --cov-report=term-missing

# Verbose tests
pytest -v

Architecture

The library lives in src/amelis2dame/:

Module Responsibility
certificate.py Load and validate P12/PFX certificates
imap.py IMAP connection, fetch, and email actions
smime.py S/MIME decryption
attachment.py Extract PDF attachments from decrypted messages
pdf_parser.py Extract data from PDFs and apply rename patterns
dame.py Generate AEK-EDIFACT (.bef) DAME documents
cli.py CLI entry point and orchestration

Use as a library

from amelis2dame import SMIMECertificate, MailboxClient, decrypt_email, extract_attachments

cert = SMIMECertificate.from_p12("certificate.p12", "password")

with MailboxClient("imap.example.com", 993, "user@example.com", "pass") as mailbox:
    emails = mailbox.fetch_emails(subject="Auftrag")
    for email_id, msg in emails:
        decrypted = decrypt_email(msg, cert)
        if decrypted:
            saved = extract_attachments(decrypted, "./output")

License

GPL-3.0-or-later

Links

Metadata

Release files for amelis2dame 0.2.6

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

Source distribution (sdist)

Source distribution for amelis2dame 0.2.6
File Size Uploaded Provenance
amelis2dame-0.2.6.tar.gz 17.4 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 38.9 kB

Release files / amelis2dame-0.2.6.tar.gz

Download URL amelis2dame-0.2.6.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7921113fae0eb83135141b75d9cdd251f1a385ec968a04e250308f644239cb6d
BLAKE2b-256 checksum
How to use checksums
6ec3bf27edf8517d561e6a6940cb8b932da18ef10b35f3746032cd2e497b592d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / amelis2dame-0.2.6-py3-none-any.whl

Download URL amelis2dame-0.2.6-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9918b93c9b82f3e71e5e7343a589f8ff7db75d867a453a1025150d5a25916da4
BLAKE2b-256 checksum
How to use checksums
da8272ab61cf7000ebaaa97cc7d5e2cf02cc9a533bbfd0034d7d23e70a96c5a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for amelis2dame 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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