A library and CLI tool to fetch and decrypt S/MIME encrypted emails and extract PDF attachments.
Project description
📧 amelis-smime-decrypt
A Python library and CLI tool that connects to an IMAP email mailbox, fetches S/MIME encrypted emails, decrypts them using a P12/PFX certificate, and extracts PDF attachments with intelligent deduplication and flexible post-processing.
🚀 Features
✔️ IMAP Integration - Connect to any IMAP email server
✔️ S/MIME Decryption - Decrypt emails using P12/PFX certificates (RSA-OAEP support)
✔️ Subject Filtering - Fetch emails matching specific keywords
✔️ Deduplication - Automatically handle duplicate emails (same subject)
✔️ Flexible Actions - Mark as seen, delete, or move emails after processing
✔️ CLI + Library - Use as standalone tool or import as Python library
✔️ PDF Data Extraction - Extract patient data from PDFs and rename files with flexible patterns
📦 Installation
For Users
pip install amelis-smime-decrypt
For Development
Prerequisites (Linux/Debian)
sudo apt-get install libssl-dev swig python3-dev gcc python3-virtualenv
Clone and Setup
git clone https://github.com/nerdocs/amelis-smime-decrypt.git
cd amelis-smime-decrypt
# Using uv (recommended)
uv venv
. .venv/bin/activate # or: source .venv/bin/activate
uv sync
# Or using pip
pip install -e ".[dev]"
🔑 Configuration
Using .env File
Create an .env file from the template:
cp .env.example .env
Edit the configuration:
# IMAP Configuration
IMAP_SERVER=imap.example.com
IMAP_PORT=993
EMAIL_ACCOUNT=info@example.com
EMAIL_PASSWORD=supersecretpassword
# S/MIME Certificate (P12/PFX format)
P12_CERTIFICATE_PATH=./certificate.p12
PFX_PASSWORD=your_certificate_password
# Output Configuration
SAVE_DIRECTORY=./output
SUBJECT_KEYWORD="Auftrag"
# Email Post-Processing
EMAIL_ACTION=mark_seen # Options: mark_seen | delete | move:FolderName
DUPLICATE_ACTION=mark_seen # Action for older duplicate emails
# PDF Renaming Pattern (optional)
# Available variables: {last_name}, {first_name}, {birth_date}, {barcode_number},
# {samplecollectiondate}, {receiptdate}, {finalreport}, etc.
RENAME_PATTERN="{last_name}_{first_name}_{birth_date}_{barcode_number}.pdf"
Using CLI Arguments (Override .env)
All settings can be overridden via command-line arguments:
amelis-smime-decrypt \
--imap-server imap.example.com \
--imap-port 993 \
--imap-user user@example.com \
--imap-pass secret \
--cert certificate.p12 \
--password pfx_pass \
--subject "Order" \
--output ./orders \
--email-action delete \
--duplicate-action "move:Archive"
🛠️ Usage
Basic Usage
Run with default configuration from .env:
amelis-smime-decrypt
With Custom Arguments
amelis-smime-decrypt --subject "Auftrag" --output ./output_directory
Get Help
amelis-smime-decrypt --help
How It Works
- Connects to IMAP server with provided credentials
- Fetches emails matching the subject keyword
- Deduplicates emails with same subject (keeps latest)
- Decrypts S/MIME encrypted messages
- Extracts PDF attachments
- Renames PDFs based on extracted data (if pattern configured)
- Performs configured action (mark seen/delete/move)
📂 Output
Extracted PDFs are saved to SAVE_DIRECTORY (default: ./output/):
output/
├── Mueller_Hans_01.01.1980_BC123456.pdf
├── Schmidt_Anna_15.05.1975_BC789012.pdf
Security Considerations
- Protect your certificate: Keep P12/PFX files secure, never commit to version control
- Use environment variables: Store credentials in
.env(add to.gitignore) - Limit IMAP access: Use app-specific passwords when available
- Secure storage: Restrict file permissions on certificates and
.env
🧪 Development
Commands
# Run with arguments
amelis-smime-decrypt --subject "Auftrag" --output ./output_directory
# Format code
black src/
# Run tests
pytest
# Run tests with coverage
pytest --cov=amelis_smime_decrypt --cov-report=term-missing
# Verbose test output
pytest -v
📚 Architecture
Modular library structure in src/amelis_smime_decrypt/:
Core Modules
- certificate.py -
SMIMECertificateclass for P12/PFX certificate loading - imap.py -
MailboxClientclass for IMAP operations with context manager support - smime.py -
decrypt_email()function for S/MIME decryption - attachment.py -
extract_attachments()function for PDF extraction - cli.py - CLI entry point with argparse support
Key Dependencies
- endesive - S/MIME decryption (RSA-OAEP support)
- cryptography - Certificate/key handling
- imapclient - IMAP protocol wrapper
- python-dotenv - Environment configuration
- pypdf - PDF data extraction
📝 License
This project is licensed under the GPL v3.0 License or later.
🤝 Contributing
Pull requests are welcome! Feel free to fork and submit PRs.
📧 Support
For issues, please open a GitHub Issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file amelis_smime_decrypt-0.2.0.tar.gz.
File metadata
- Download URL: amelis_smime_decrypt-0.2.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2b2e4ad0ca247cb6960e1a0c50766305275049633d6404145196e88f69c577c
|
|
| MD5 |
0b6cd7ce019022f689a5ec79e7e9de68
|
|
| BLAKE2b-256 |
5b012ef99b508b0ad43d3f1171db2d9627c97aed32350b8296d2f7c44e256a4f
|
File details
Details for the file amelis_smime_decrypt-0.2.0-py3-none-any.whl.
File metadata
- Download URL: amelis_smime_decrypt-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2a07c34ddca833a2f961a106418f02d9f049579f2c4440bb85db837603ee8df
|
|
| MD5 |
d822332f3184559bb67df25649def742
|
|
| BLAKE2b-256 |
0c408b655a557bfc110414686c64355022bfc79d0e4a377316fa6979d2fc0e4c
|