Skip to main content

emlectomy

Surgical removal of the redundancy in an email chain.

tests license: MIT python 3.9+ dependencies: none

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.

Before and after

This is one real-world-shaped email (examples/gmail-chain.eml): Alice's third message, which quotes Bob's reply, which quotes Alice's original, plus her signature and an attachment.

Before: what you have to read
From: Alice Johnson <alice@example.com>
To: Bob Smith <bob@example.com>, Carol Diaz <carol@example.com>
Subject: Re: Q3 budget
Date: Wed, 05 Mar 2025 08:30:00 -0500
Attachment: budget-v2.pdf

Done - revised numbers attached. Travel is now 30% lower.

-- 
Alice Johnson
Finance Lead
+1 555 010 2030
www.example.com

On Tue, Mar 4, 2025 at 9:15 AM Bob Smith <bob@example.com> wrote:
> Looks mostly fine to me. The travel line is too high though, we spent half of that last year and nothing has changed.
>
> Bob
>
> On Mon, Mar 3, 2025 at 4:00 PM Alice Johnson <alice@example.com> wrote:
>> Hi team,
>>
>> The Q3 budget draft is attached. Please review the marketing line items before Friday and tell me if anything looks off.
>>
>> Thanks,
>> Alice
$ emlectomy examples/gmail-chain.eml
emlectomy: 1 email(s) -> 3 message(s) (0 redundant copies removed, 1 signatures stripped)
  attachments: 1 saved (0 duplicates skipped)
  wrote emlectomy-out/gmail-chain/  (thread.json, thread.html, attachments/)

After: thread.html for humans (attachments are links, never embedded):

thread.html: the quoted chain became three separate messages in chronological order

and thread.json for agents (messages 2 and 3 of 3 shown, the signature moved to participants):

[
  {
    "id": 2,
    "from": "Bob Smith <bob@example.com>",
    "to": [],
    "date": "2025-03-04T09:15:00",
    "text": "Looks mostly fine to me. The travel line is too high though, we spent half of that last year and nothing has changed.\n\nBob",
    "attachments": [],
    "source": "quoted_only"
  },
  {
    "id": 3,
    "from": "Alice Johnson <alice@example.com>",
    "to": ["Bob Smith <bob@example.com>", "Carol Diaz <carol@example.com>"],
    "date": "2025-03-05T08:30:00-05:00",
    "text": "Done - revised numbers attached. Travel is now 30% lower.",
    "attachments": [
      {"name": "budget-v2.pdf", "path": "attachments/budget-v2.pdf", "mime": "application/pdf", "size": 45}
    ],
    "source": "email"
  }
]

The folder layout:

emlectomy-out/gmail-chain/
├── thread.json      for agents: chronological messages, only each one's new text
├── thread.html      for humans: the same clean thread
└── attachments/     every attached file, safe names, identical files stored once
  • One file, zero dependencies. Python 3.9+ standard library only. Copy emlectomy.py and 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; id is the position.
  • Attachment paths are relative to the output folder; base_dir is its absolute path.
  • cc and subject appear only when present, and subject only when it differs from thread.
  • source is email (a file you gave it), attached_email, or quoted_only (the original wasn't provided, so the message was rebuilt from a quote and its headers may be partial).
  • date is null with a date_text when the date couldn't be parsed.
  • note appears 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. stats tells you what was removed.
  • A single .eml only 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 / .ost archives aren't supported. Export to .mbox or .eml first.

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

MIT

Metadata

Release files for emlectomy 0.1.1

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

Source distribution (sdist)

Source distribution for emlectomy 0.1.1
File Size Uploaded Provenance Actions
emlectomy-0.1.1.tar.gz 31.1 kB With provenance Details for emlectomy-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 58.2 kB

Release files / emlectomy-0.1.1.tar.gz

Download URL emlectomy-0.1.1.tar.gz
Size 31.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3714a4c50f80fc8db9577ec739672167c2c7fe58c8a92ddaac9f3ba6d273ed5e
BLAKE2b-256 checksum
How to use checksums
b21a07f30c3c52e8bbadd75e27563a09e490288d225483d5a16c7ecdb96f3d7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / emlectomy-0.1.1-py3-none-any.whl

Download URL emlectomy-0.1.1-py3-none-any.whl
Size 27.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a92fab9400ccd5698ef3dce8160d1a37f18673841e44d0748f32a9b8bc38f984
BLAKE2b-256 checksum
How to use checksums
d3617cec926ffa09a096efa7a22feb47725f947779f43d81c68ec0323562fac3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for emlectomy 0.1.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for emlectomy-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

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