#!/usr/bin/python import email, difflib, re, sys inmail="""From: To: Subject: test X-Long-Line: Some really long line contains a lot of text and thus has to be rewrapped because it is some really long line MIME-Version: 1.0 Content-Type: multipart/signed; boundary="borderline"; protocol="application/pgp-signature"; micalg=pgp-sha1 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --borderline Content-Type: text/plain X-Long-Line: Another really long line contains a lot of text and thus has to be rewrapped because it is another really long line This is the signed contents. --borderline Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.6 (GNU/Linux) iD8DBQFG03voRhp6o4m9dFsRApSZAKCCAN3IkJlVRg6NvAiMHlvvIuMGPQCeLZtj FGwfnRHFBFO/S4/DKysm0lI= =t7+s -----END PGP SIGNATURE----- --borderline-- """ # This is a semantic no-op: parse mail and turn it back into a string. outmail = email.message_from_string(inmail).as_string(False) # Extract the first mime part of each message repart = re.compile(r'^--([^\n]+)\n(.*?)\n--\1$', re.DOTALL | re.MULTILINE) inpart = repart.search(inmail).group(2) outpart = repart.search(outmail).group(2) if inpart == outpart: print 'All is well: the signed part survived without any modifications.' sys.exit(0) else: print 'Bad news: the signed part was modified, which broke the signature.' print '\n'.join(difflib.unified_diff(inpart.split('\n'), outpart.split('\n'), fromfile='in.eml', tofile='out.eml', lineterm='')) sys.exit(1)