def deSA(fp): import email, re m = email.message_from_string(fp.read()) if m['X-Spam-Status']: if m['X-Spam-Status'].startswith('No'): del m['X-Spam-Status'] del m['X-Spam-Level'] else: del m['X-Spam-Status'] del m['X-Spam-Level'] del m['X-Spam-Flag'] del m['X-Spam-Checker-Version'] pct = m['X-Spam-Prev-Content-Type'] if pct: del m['X-Spam-Prev-Content-Type'] m['Content-Type'] = pct pcte = m['X-Spam-Prev-Content-Transfer-Encoding'] if pcte: del m['Content-Transfer-Encoding'] m['Content-Transfer-Encoding'] = pcte del m['X-Spam-Prev-Content-Transfer-Encoding'] body = m.get_payload() subj = m['Subject'] del m['Subject'] m['Subject'] = re.sub(r'\*\*\*\*\*SPAM\*\*\*\*\* ', '', subj) newbody = [] at_start = 1 for line in body.splitlines(): if at_start and line.startswith('SPAM: '): continue elif at_start: at_start = 0 else: newbody.append(line) m.set_payload("\n".join(newbody)) return m if __name__ == "__main__": import sys print deSA(open(sys.argv[1]))