Foundations of Python Network Programming

(WallPaper) #1
Chapter 12 ■ Building and parsing e-Mail

235

$ python3 build_basic_email.py > email.txt
$ python3 display_email.py email.txt
From: Test Sender [email protected]
To: [email protected]
Date: Tue, 25 Mar 2014 17:14:01 -0400
Subject: Test Message, Chapter 12


Hello,
This is a basic message from Chapter 12.



  • Anonymous


But even the most complicated message is not too much for it. The get_body() logic successfully dives inside
the mixed multipart outer layer, into the alternative multipart middle, and finally even down into the related
multipart innards of the message before reemerging with the HTML version of the e-mail body. Moreover, each of the
attachments that were included are inspected as well.


$ python3 build_mime_email.py -i attachment.txt attachment.gz > email.txt
$ python3 display_email.py email.txt
From: Test Sender [email protected]
To: Test Recipient [email protected]
Date: Tue, 25 Mar 2014 17:14:01 -0400
Subject: Foundations of Python Network Programming


Hello,
This is a MIME message from Chapter 12.



  • Anonymous



  • image/gif attachment named 'blue-dot.gif': bytes object of length 35

  • text/plain attachment named 'attachment.txt': str object of length 15

  • application/octet-stream attachment named 'attachment.gz': bytes object of length 33


Walking MIME Parts


If the logic in Listing 12-4 ever winds up not being sufficient for your application—if it cannot find the body text of
a particular e-mail that your project needs to be able to parse, or if certain poorly specified attachments are being
skipped to which your customers need access—then you will need to fall back to visiting every part of an e-mail
message yourself and implementing your own algorithm for which parts to display, which to save as attachments,
and which to ignore or throw away.
There are three basic rules to keep in mind when dismembering a MIME e-mail.


•    Your first call when examining a section should be to the is_multipart() method to
determine whether the MIME part you are inspecting is a container for further MIME
subparts. You can also call get_content_type() if you want the fully qualified type with a
slash between the main type and subtype, and either get_content_maintype() or
get_content_subtype() if you only care about one half or the other.

•    When confronted with a multipart, use the iter_parts() method to loop over or fetch the
parts immediately beneath it so you can in turn discover which of the subparts are themselves
multipart and which instead simply contain content.
Free download pdf