我正在努力提取电子邮件正文,以便稍后在我的程序中使用,在该程序中我将提取特定文本部分。到目前为止,该代码适用于从gmail帐户发送到另一个gmail帐户的电子邮件,但是在尝试读取从Outlook收件箱发送的电子邮件时会返回一堆奇怪的文本。
我曾尝试从base64对其进行解码,但似乎无济于事。
import imaplib
import email
username = "XXXX"
password = "YYYY"
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
imap.login(username, password)
imap.select('INBOX')
status, response = imap.search(None, 'UNSEEN')
unreadmsgs = response[0].split()
result, data = imap.fetch(unreadmsgs[0], "(UID BODY[TEXT])")
raw_mail = email.message_from_string(data[0][1])
raw2= raw_mail.get_payload(decode=True)
print(raw2)
当我寻找可读文本时,这将返回如下所示的内容。
--_000_VI1PR07MB3966DEECC2518D0D5C2C2EF6C4850VI1PR07MB3966eurp_
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
DQoNCkZyb206IFBlYWNlVGVjaCBBY2NlbGVyYXRvciA8ZGltaXRyYS5oYXR6dWRpc0BjNWNhcGl0YWwuY29tPg0KU2VudDogTW9uZGF5LCBKYW51YXJ5IDcsIDIwMTkgMTE6NDMgUE0NClRvOiBEaW1pdHJhIEhhdHp1ZGlzIDxEaW1pdHJhLkhhdHp1ZGlzQGM1Y2FwaXRhbC5jb20+OyBIYWRlZWwgQWwtVGFzaGkgPGhhZGVlbC5hbHRhc2hpQGM1Y2FwaXRhbC5jb20+DQpTdWJqZWN0OiBOaW5qYSBGb3JtcyBTdWJtaXNzaW9uDQoNCkNvbXBhbnkgLyBTdGFydHVwIE5hbWU6DQoNClNlbnNlIEFmcmljYQ0KDQpHaXZlIHVzIGEgc2hvcnQgZGVzY3JpcHRpb24gb2YgeW91ciBpbXBhY3QgdmVudHVyZS46DQoNCldlIGhhdmUgYSBnbG9iYWwgdmlzaW9uIG9mIHByb21vdGluZyBjaXRpemVuIHBhcnRpY2lwYXRpb24gaW4gcHVibGljIGRpc2NvdXJzZS4NClNpbmNlIG91ciBsYXVuY2ggaW4gMjAxNy8yMDE4IGluIENhbWVyb29uLCBvdXIgY29tbWl0bWVudCB0byB0aGF0IHZpc2lvbiB3YXMgaW1wbGVtZW50ZWQgYnkgbWFraW5nIGxvY2FsIG5ld3MgYW5kIG90aGVyIG5vbi1maWN0aW9uIG1lZGlhIG1vcmUgYWNjZXNzaWJsZS4gV2UgYXJlIG5vdyB0aGUg......
依此类推
这是base64编码的(请参见数据上方的content-tranfer-encoding)