emlectomy
Surgical removal of the redundancy in an email chain.
Long reply-all chains repeat themselves. Every message quotes every earlier one, forwards stack up, signatures and legal disclaimers pile on, and the same PDF gets attached five times. That is painful to read and expensive to feed to an LLM.
emlectomy reads the mess and writes one folder with the conversation told once, in
order, containing only what each person actually added:
python emlectomy.py reply.eml
emlectomy-out/reply/
├── thread.json for agents: chronological messages, only each one's new text
├── thread.html for humans: the same clean thread (attachments are links, never embedded)
└── attachments/ every attached file, safe names, identical files stored once
- One file, zero dependencies. Python 3.9+ standard library only. Copy
emlectomy.pyand run it. - Linux, macOS, Windows. CI runs the tests on all three.
- Reads
.eml,.msg(Outlook),.emlx(Apple Mail),.mbox, folders of them, or stdin. - Agent-friendly JSON. Flat, chronological, stable field names, attachment paths that resolve.
Quick start
pipx install emlectomy # or: pip install emlectomy
emlectomy reply.eml --open
No install needed either: grab emlectomy.py and run python emlectomy.py reply.eml.
Try it on the samples in this repo:
git clone https://github.com/agustinrojas1/emlectomy
cd emlectomy
python emlectomy.py examples/budget-thread.mbox --open
emlectomy FILE [FILE ...] [-o DIR] [--open] [--print-json] [--keep-signatures]
| Option | |
|---|---|
FILE |
Mail files or folders. - reads one from stdin. Several files are merged into one thread. |
-o DIR |
Output folder. Default: emlectomy-out/<file name>. Re-running overwrites the previous output, and it refuses to write into a non-empty folder that isn't an earlier emlectomy output. |
--open |
Open thread.html when done. |
--print-json |
Also print thread.json to stdout (status goes to stderr). Handy for piping to an agent. |
--keep-signatures |
Don't strip signatures and disclaimers. |
What it does
Peels quoted history into separate messages. > quotes at any depth, On … wrote:
attributions (English, Spanish, French, German, Italian, Portuguese, Dutch), Outlook
-----Original Message----- and From: / Sent: / To: / Subject: blocks, forwarded-message
blocks, and HTML replies (blockquote, Outlook's header block). Each older message gets its
sender and date from the attribution or header block.
Drops redundant copies. A quoted message is removed when it is already in the thread,
matched on normalised text (ignoring re-wrapping, > marks and whitespace), near-duplicate
overlap and paragraph coverage, with sender and date as a sanity check. Two real emails are
only merged when they share a Message-ID, or have the same text, sender and timestamp, so
two different "Thanks!" replies stay separate.
Strips boilerplate. Signatures (-- delimiter, or sign-off plus contact block), "Sent
from my iPhone", confidentiality disclaimers, [cid:…] placeholders, "external email"
banners, Outlook SafeLinks wrappers. Each person's signature is kept once under participants.
Deduplicates attachments. Identical files (SHA-256) are saved and listed once. Inline
images (e.g. Gmail screenshots) are attached to the message that shows them, and the text
points at the file: [image: attachments/image (6).png].
Handles inline replies. When someone answers between quoted lines, the first quote level stays next to the answers so they still make sense, and anything quoted deeper is peeled off into its own older messages.
Emails attached to other emails (an .eml forwarded as an attachment, or embedded in a
.msg) are read as messages and merged into the thread rather than listed as files.
thread.json
{
"thread": "Q3 budget",
"base_dir": "/abs/path/emlectomy-out/reply",
"stats": {
"emails_read": 3, "message_blocks_seen": 6, "messages_kept": 3,
"redundant_copies_removed": 3, "signatures_removed": 0,
"attachments_saved": 1, "duplicate_attachments_skipped": 2
},
"participants": [
{"name": "Alice Johnson", "email": "alice@example.com", "signature": "…"}
],
"messages": [
{
"id": 3,
"from": "Alice Johnson <alice@example.com>",
"to": ["Bob Smith <bob@example.com>"],
"date": "2025-03-04T18:00:00+00:00",
"text": "Agreed with Bob on travel. I will cut it by 30 percent…",
"attachments": [
{"name": "budget.pdf", "path": "attachments/budget.pdf",
"mime": "application/pdf", "size": 45}
],
"source": "email"
}
]
}
- Messages are oldest first;
idis the position. - Attachment
paths are relative to the output folder;base_diris its absolute path. ccandsubjectappear only when present, andsubjectonly when it differs fromthread.sourceisemail(a file you gave it),attached_email, orquoted_only(the original wasn't provided, so the message was rebuilt from a quote and its headers may be partial).dateisnullwith adate_textwhen the date couldn't be parsed.noteappears only on inline-reply messages.
Limits
- Heuristics, not magic. Unusual mail clients or heavily edited quotes can leave a bit of
repeated text or split a message in the wrong place.
statstells you what was removed. - A single
.emlonly contains what it quotes. Replies on other branches of a conversation aren't in it. Give emlectomy more files (or an.mbox) to cover more. - Formatting is plain text. Colours and highlights (for example answers typed in yellow) are lost.
.msg: bodies come from the plain-text or HTML property; RTF-only messages aren't decoded..pst/.ostarchives aren't supported. Export to.mboxor.emlfirst.
Development
python -m unittest discover -s tests # run the tests
python tests/fixtures.py # regenerate examples/
The .msg tests use a small OLE writer in tests/fixtures.py rather than real Outlook files,
so real-world .msg bug reports (with a sample, if you can share one) are especially welcome.
Issues and pull requests are welcome.
License
Metadata
Release files for emlectomy 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| emlectomy-0.1.0.tar.gz | 28.9 kB | Without provenance | Details for emlectomy-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| emlectomy-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for emlectomy-0.1.0-py3-none-any.whl |
Total release size: 54.4 kB
Release files / emlectomy-0.1.0.tar.gz
| Download URL | emlectomy-0.1.0.tar.gz |
|---|---|
| Size | 28.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e103e546b10accb837b4d983c908b7ba56ca0b6f5aac110f06b675770f1b6d5
|
|
BLAKE2b-256 checksum How to use checksums |
04062afb51815b6949b8e07997cff3a05d4b0621e241cf956b0a5d1960c2a9eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Release files / emlectomy-0.1.0-py3-none-any.whl
| Download URL | emlectomy-0.1.0-py3-none-any.whl |
|---|---|
| Size | 25.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7930658c08b2475a762f074452e66c94fbc8b0e8e099e8d5831e6cfe3d7fe7cf
|
|
BLAKE2b-256 checksum How to use checksums |
27f4b1b93b91069473ffb8f7612581aa299ba5dd0852ba9a66415b2320de61e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Security for emlectomy 0.1.0
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.