Ron Paulfan Ron Paulfan - 3 months ago 17
Python Question

Flanker: MimePart is not iterable

I'm a noob and using Flanker to parse emails.
https://github.com/mailgun/flanker

I'm getting a Not Iterable error that I just can't seem to figure out. I've read tons of pages about lists, but I just can't get it to work. I'm hopeful I can get some help.

To run the following code, you will need to install Flanker, and save this file as 'email'.
http://pastebin.com/ZS4q2kYN

I'm trying to read the 'attachmenttype' and do something depending on the response. Can't get it to work though. Here is the test code:

#!/usr/bin/python
#Open Email
from flanker import mime
with open ("email", mode="rb") as myfile:
message_string=myfile.read()
myfile.close()

#Read Email
msg = mime.from_string(message_string)

#read attachment type
attachmenttype = msg.parts[1]
print attachmenttype

#This errors for me: TypeError: argument of type 'MimePart' is not iterable
if attachmenttype:
if '(text/html)' in attachmenttype:
print "woohoo"


Here is the response I get:
myerror

Thanks in advance.

Answer

attachmenttype may be printed a string but it is not a string. However, since you can print it, you're halfway there. Just convert it to a string using str and compare that.

Fix your code like this. I couldn't test it but I don't see how it wouldn't work:

if attachmenttype:
    if '(text/html)' in str(attachmenttype):
        print("woohoo")