Python email quoted-printable encoding problem

后端 未结 3 842
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 00:12

I am extracting emails from Gmail using the following:

def getMsgs():
 try:
    conn = imaplib.IMAP4_SSL(\"imap.gmail.com\", 993)
  except:
    print \'Faile         


        
3条回答
  •  臣服心动
    2020-12-04 00:35

    You could/should use the email.parser module to decode mail messages, for example (quick and dirty example!):

    from email.parser import FeedParser
    f = FeedParser()
    f.feed("")
    rootMessage = f.close()
    
    # Now you can access the message and its submessages (if it's multipart)
    print rootMessage.is_multipart()
    
    # Or check for errors
    print rootMessage.defects
    
    # If it's a multipart message, you can get the first submessage and then its payload
    # (i.e. content) like so:
    rootMessage.get_payload(0).get_payload(decode=True)
    

    Using the "decode" parameter of Message.get_payload, the module automatically decodes the content, depending on its encoding (e.g. quoted printables as in your question).

提交回复
热议问题