Q:

unicode() argument 2 must be string not None

I'm trying to parse the content of an email that contains html content.
import imaplib
import email
....
    elif part.get_content_type() == "text/html":
        # if html is None:
        html = ""
        html += unicode(part.get_payload(decode=True),part.get_content_charset(),'replace').encode('utf8','replace')
        save_string = str("C:Dumpgmailemail2"+".eml")
        # location on disk
        myfile = open(save_string, 'a')
        myfile.write(str(html))
        #myfile.write(html.decode('utf-8'))
        myfile.close()

But this gives me an error:
Traceback (most recent call last):
  File "extract.py", line 22, in <module>
    html += unicode(part.get_payload(decode=True),part.get_content_charset(),"replace").encode('utf8','replace')
TypeError: unicode() argument 2 must be string, not None

A:

Seems like part.get_content_charset() is None , maybe you can supply some default value if its None to the unicode() function -
html += unicode(part.get_payload(decode=True),part.get_content_charset() if part.get_content_charset() is not None else 'utf-8' ,'replace').encode('utf8','replace')

