[英]Not able to get gmail body inner text using imap in Python 3.6 +
I wrote the code below for my script: 我为脚本编写了以下代码:
import sys
import imaplib
import getpass
import email
import email.header
import datetime
EMAIL_ACCOUNT = "notatallawhistleblowerIswear@gmail.com"
EMAIL_FOLDER = "INBOX"
def process_mailbox(M):
rv, data = M.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
subject = str(hdr)
print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date'])
# Now convert to local date-time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print ("Local Date:", \
local_date.strftime("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4_SSL('imap.gmail.com')
rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass())
print(rv, data)
rv, mailboxes = M.list()
rv, data = M.select(EMAIL_FOLDER)
print("Processing mailbox...\n")
process_mailbox(M)
M.close()
M.logout()
I am not able to fetch a gmail body using IMAP. 我无法使用IMAP获取gmail正文。 I am successfully getting the subject name and from the email using IMAP.
我已成功使用IMAP从电子邮件中获取主题名称。
How can I read the gmail body text? 如何阅读gmail正文?
Try: 尝试:
msg = email.message_from_string(data[0][1])
print(msg['From'])
print(msg.get_payload(decode=True))
Also can check out: Python : How to parse the Body from a raw email , given that raw email does not have a "Body" tag or anything 也可以签出: Python:鉴于原始电子邮件没有“正文”标签或其他任何内容,如何从原始电子邮件中解析正文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.