Python email bot Pyzmail/IMAPclient error

会有一股神秘感。 提交于 2019-12-06 07:52:38

问题


So I'm working on a Python script to extract text from an email and following these instructions to do so. This is the script thus far:

import imapclient
import pprint
import pyzmail

mymail = "my@email.com"
password = input("Password: ")

imapObj = imapclient.IMAPClient('imap.gmail.com' , ssl=True)
imapObj.login(mymail , password)
imapObj.select_folder('INBOX', readonly=False)
UIDs = imapObj.search(['SUBJECT Testing'])
rawMessages = imapObj.fetch([5484], ['BODY[]'])
message = pyzmail.PyzMessage.factory(rawMessages[5484]['BODY[]'])

However I'm getting this error:

message = pyzmail.PyzMessage.factory(rawMessages[5484]['BODY[]'])
KeyError: 5484

5484 being the ID for the email that the search function finds. I've also tried putting UIDs in instead of 5484, but that doesn't work either. Thanks in advance!


回答1:


Thank you @Madalin Stroe .

I use python3.6.2 and pyzmail1.0.3 on Win10. When I run message = pyzmail.PyzMessage.factory(rawMessages[4]['BODY[]']) The ERR shows like this:

Traceback (most recent call last):
File "PATH/TO/mySinaEmail.py", line 42, in <module>
message = pyzmail.PyzMessage.factory(rawMessages[4]['BODY[]'])
KeyError: 'BODY[]'

When I modified this to message = pyzmail.PyzMessage.factory(rawMessages[4][b'BODY[]']), it run well.




回答2:


Try replacing ['BODY[]'] with [b'BODY[]']



来源:https://stackoverflow.com/questions/40900880/python-email-bot-pyzmail-imapclient-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!