【问题标题】:Not able to get gmail body inner text using imap in Python 3.6 +无法在 Python 3.6 + 中使用 imap 获取 gmail 正文内部文本
【发布时间】:2019-01-04 11:13:05
【问题描述】:

我为我的脚本编写了以下代码:

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()

我无法使用 IMAP 获取 gmail 正文。我使用 IMAP 从电子邮件中成功获取了主题名称。

如何阅读 gmail 正文?

【问题讨论】:

    标签: python python-3.x imap


    【解决方案1】:

    试试:

       msg = email.message_from_string(data[0][1])
       print(msg['From'])
       print(msg.get_payload(decode=True))
    

    还可以查看: Python : How to parse the Body from a raw email , given that raw email does not have a "Body" tag or anything

    【讨论】:

    • 它可以正常获取内部 html 正文,但无法获取准确的纯文本。
    • @AnupPatil 您是否尝试过链接中的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2019-04-11
    • 2015-01-17
    • 2013-03-25
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多