amelis-smime-decrypt
is a Python script that connects to an IMAP email mailbox, fetches unread S/MIME encrypted emails, decrypts them using a provided private key, and extracts PDF attachments.
✔️ Connects to an IMAP email server
✔️ Fetches emails with given text in Subject
✔️ Detects and decrypts S/MIME encrypted emails
✔️ Extracts PDF attachments and saves them
(TODO)
✔️ Secure handling of private keys & credentials
Easy.
TODO: This is still necessary to be done.
pip install amelis-smime-decrypt
# install dependencies for building M2Crypto
sudo apt-get install libssl-dev swig python3-dev gcc python3-virtualenv
git clone https://github.com/nerdocs/amelis-smime-decrypt.git cd amelis-smime-decrypt
virtualenv .venv
. .venv/bin/activate
Ensure you have Python 3.8+, then run:
pip install -e .
Create an .env
file (copy the existing .env.example
).
Edit the SAVE_DIRECTORY, IMAP_SERVER, EMAIL_ACCOUNT, and EMAIL_PASSWORD inside .env:
IMAP_SERVER = "imap.example.com" EMAIL_ACCOUNT = "[email protected]" EMAIL_PASSWORD = "your_password"
Store your private key and certificate in a secure location:
.../private_key.pem
.../certificate.pem
Chances are big that your certificate comes encrypted with the more or less deprecated RC2-40-CBC algorithm as [email protected]
file. If this is the case, convert it to a private key and certificate file using:
[email protected]
openssl pkcs12 -in ${MAIL}.p12 -out ${MAIL}.crt.pem -clcerts -nokeys -legacy
openssl pkcs12 -in ${MAIL}.p12 -out ${MAIL}.key.pem -nocerts -nodes -legacy
You might need to provide the import password for the certificate.
Update PRIVATE_KEY_PATH and CERTIFICATE_PATH in the .env
file to point to those files.
Run the script:
python amelis-smime-decrypt.py
The script will:
Connect to the given email inbox.
Fetch unread emails.
If encrypted, decrypt them using S/MIME.
Extract PDF attachments and save them to the given attachments folder.
All extracted PDFs are saved in the directory you specify in SAVE_DIRECTORY
.
attachments/ ├── invoice_123.pdf ├── report.pdf
Protect your private key: Do not share private_key.pem.
Use environment variables for credentials.
Limit IMAP access to trusted networks.
This project is licensed under the GPL v3.0 License or later.
Pull requests are welcome! Feel free to fork and submit PRs.
For issues, please open a GitHub Issue.