Handoffs are where small-business workflows leak, and email shows them clearly: a message forwarded from one person to another, then another. Counting those chains is a cheap way to find evidence during a data-driven workflow audit for a small team.
Idea
Group messages by thread. Count how many distinct internal people forwarded or replied before the customer got an answer.
import mailbox, re
from collections import defaultdict
from email.utils import parseaddr
INTERNAL = "@yourbusiness.example"
def norm_subject(s):
return re.sub(r"^((re|fw|fwd):\s*)+", "", (s or "").strip(), flags=re.I).lower()
def chains(mbox_path):
threads = defaultdict(list)
for msg in mailbox.mbox(mbox_path):
sender = parseaddr(msg.get("From", ""))[1].lower()
threads[norm_subject(msg.get("Subject"))].append(sender)
out = []
for subject, senders in threads.items():
internal = [s for s in senders if s.endswith(INTERNAL)]
hops = len(dict.fromkeys(internal)) # distinct internal people, in order
if hops >= 3:
out.append((hops, subject))
return sorted(out, reverse=True)
if __name__ == "__main__":
for hops, subject in chains("export.mbox")[:20]:
print(hops, subject[:70])
Reading the output
- Threads with three or more internal people are handoff candidates.
- Group similar subjects ("quote", "invoice", "booking") to see which workflow they belong to.
- Take the top few into the audit interviews: "Why did this pass through four people?"
Caveats
Subject matching is rough. That is fine for an audit, where you want patterns, not perfect data. Never share raw exports; summarise counts.
See also what a build looks like after the audit.
Top comments (0)