17db96d56Sopenharmony_ciimport os 27db96d56Sopenharmony_ciimport sys 37db96d56Sopenharmony_ciimport tempfile 47db96d56Sopenharmony_ciimport mimetypes 57db96d56Sopenharmony_ciimport webbrowser 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci# Import the email modules we'll need 87db96d56Sopenharmony_cifrom email import policy 97db96d56Sopenharmony_cifrom email.parser import BytesParser 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_cidef magic_html_parser(html_text, partfiles): 137db96d56Sopenharmony_ci """Return safety-sanitized html linked to partfiles. 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci Rewrite the href="cid:...." attributes to point to the filenames in partfiles. 167db96d56Sopenharmony_ci Though not trivial, this should be possible using html.parser. 177db96d56Sopenharmony_ci """ 187db96d56Sopenharmony_ci raise NotImplementedError("Add the magic needed") 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_ci# In a real program you'd get the filename from the arguments. 227db96d56Sopenharmony_ciwith open('outgoing.msg', 'rb') as fp: 237db96d56Sopenharmony_ci msg = BytesParser(policy=policy.default).parse(fp) 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci# Now the header items can be accessed as a dictionary, and any non-ASCII will 267db96d56Sopenharmony_ci# be converted to unicode: 277db96d56Sopenharmony_ciprint('To:', msg['to']) 287db96d56Sopenharmony_ciprint('From:', msg['from']) 297db96d56Sopenharmony_ciprint('Subject:', msg['subject']) 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ci# If we want to print a preview of the message content, we can extract whatever 327db96d56Sopenharmony_ci# the least formatted payload is and print the first three lines. Of course, 337db96d56Sopenharmony_ci# if the message has no plain text part printing the first three lines of html 347db96d56Sopenharmony_ci# is probably useless, but this is just a conceptual example. 357db96d56Sopenharmony_cisimplest = msg.get_body(preferencelist=('plain', 'html')) 367db96d56Sopenharmony_ciprint() 377db96d56Sopenharmony_ciprint(''.join(simplest.get_content().splitlines(keepends=True)[:3])) 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_cians = input("View full message?") 407db96d56Sopenharmony_ciif ans.lower()[0] == 'n': 417db96d56Sopenharmony_ci sys.exit() 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci# We can extract the richest alternative in order to display it: 447db96d56Sopenharmony_cirichest = msg.get_body() 457db96d56Sopenharmony_cipartfiles = {} 467db96d56Sopenharmony_ciif richest['content-type'].maintype == 'text': 477db96d56Sopenharmony_ci if richest['content-type'].subtype == 'plain': 487db96d56Sopenharmony_ci for line in richest.get_content().splitlines(): 497db96d56Sopenharmony_ci print(line) 507db96d56Sopenharmony_ci sys.exit() 517db96d56Sopenharmony_ci elif richest['content-type'].subtype == 'html': 527db96d56Sopenharmony_ci body = richest 537db96d56Sopenharmony_ci else: 547db96d56Sopenharmony_ci print("Don't know how to display {}".format(richest.get_content_type())) 557db96d56Sopenharmony_ci sys.exit() 567db96d56Sopenharmony_cielif richest['content-type'].content_type == 'multipart/related': 577db96d56Sopenharmony_ci body = richest.get_body(preferencelist=('html')) 587db96d56Sopenharmony_ci for part in richest.iter_attachments(): 597db96d56Sopenharmony_ci fn = part.get_filename() 607db96d56Sopenharmony_ci if fn: 617db96d56Sopenharmony_ci extension = os.path.splitext(part.get_filename())[1] 627db96d56Sopenharmony_ci else: 637db96d56Sopenharmony_ci extension = mimetypes.guess_extension(part.get_content_type()) 647db96d56Sopenharmony_ci with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as f: 657db96d56Sopenharmony_ci f.write(part.get_content()) 667db96d56Sopenharmony_ci # again strip the <> to go from email form of cid to html form. 677db96d56Sopenharmony_ci partfiles[part['content-id'][1:-1]] = f.name 687db96d56Sopenharmony_cielse: 697db96d56Sopenharmony_ci print("Don't know how to display {}".format(richest.get_content_type())) 707db96d56Sopenharmony_ci sys.exit() 717db96d56Sopenharmony_ciwith tempfile.NamedTemporaryFile(mode='w', delete=False) as f: 727db96d56Sopenharmony_ci f.write(magic_html_parser(body.get_content(), partfiles)) 737db96d56Sopenharmony_ciwebbrowser.open(f.name) 747db96d56Sopenharmony_cios.remove(f.name) 757db96d56Sopenharmony_cifor fn in partfiles.values(): 767db96d56Sopenharmony_ci os.remove(fn) 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci# Of course, there are lots of email messages that could break this simple 797db96d56Sopenharmony_ci# minded program, but it will handle the most common ones. 80