Hi,

while working on gpgit.py, I needed a function to show me how the content of an email was structured. I came up with this:

def print_email_mime_tree(email_obj):
tree = [email_obj]
deep = 0
while not len(tree) == 0:
pay = tree.pop()
if pay == "":
deep -= 1
else:
print(" " * deep + "|")
print(" " * deep + "|" + pay.get_content_type())
if pay.is_multipart():
deep += 1
tree.append("")
tree.extend(reversed(pay.get_payload()))

It will print the structure in the form of:

$$ \begin{array}{ll} | & \\ |\textrm{multipart/related} & \\ &| \\ &|\textrm{text/html} \\ &| \\ &|\textrm{image/gif} \\ &| \\ &|\textrm{image/gif} \\ \end{array} $$

I hope it comes in as handy for you as it was for me!