这是我的代码:
import imaplib
from email.parser import HeaderParser
conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('example@gmail.com', 'password')
conn.select()
conn.search(None, 'ALL')
data = conn.fetch('1', '(BODY[HEADER])')
header_data = data[1][0][1]
parser = HeaderParser()
msg = parser.parsestr(header_data)
Run Code Online (Sandbox Code Playgroud)
从这里我得到错误信息:
TypeError: initial_value must be str or none, not bytes
Run Code Online (Sandbox Code Playgroud)
我使用python 3显然自动解码.那么为什么我仍然收到此错误消息?
你可以试试:
header_data = data[1][0][1].decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
我建议这样做,(Python 3)
typ, data = conn.fetch('1', '(RFC822)') # will read the first email
email_content = data[0][1]
msg = email.message_from_bytes(email_content) # this needs to be corrected in your case
emailDate = msg["Date"]
emaiSubject = msg["Subject"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8237 次 |
| 最近记录: |