python - Gmail API Python - 检索电子邮件正文

标签 python python-2.7 gmail-api

在我的 Python 代码中,我想获取 Gmail 电子邮件的正文。我希望它能够处理任何电子邮件,而不受格式(纯文本、Mime 等)和文本编码的影响。

据我所知,唯一的方法是编写一个能够处理任何场景的解码器。在开始讨论之前,我有几个问题:

1- 我的说法正确吗?或者是否有更简单的方法(从 API 或 Python 模块)获取任何类型电子邮件的正文? (我对 Python 和 Gmail API 都是新手)

2-是否有人已经编写了这样的解码器,我可以将其导入到我的代码中?

谢谢你, 埃里克

最佳答案

是一个简单快速的沙箱案例,你必须重构一些部分

import re
import sys
import imaplib
import getpass
import email
import datetime
import string
import get_mail_search
from sys import stdout

M = imaplib.IMAP4_SSL('imap.gmail.com')
class Get_mail(object):
    """docstring for Get_mail"""
    def __init__(self, *args):
        super(Get_mail, self).__init__()
        c=1
        self.login(c)
        self.toast_msg()

        raw_input()
    def toast_msg(self, *args):
        """docstring for Get_mail"""
        M = self.mailbox()
        stdout.write("\n{}\n".format(get_mail_search.search_help_info))
        serach_input = raw_input()
        rv, data = M.search(None, serach_input)
        if rv != 'OK':
            print "No messages found!"
        id_ls = data[0].split()
        rev_id_ls = [i for i in reversed(id_ls)]
        if rev_id_ls:
            for o in rev_id_ls:
                try:
                    msg_content = self.process_mailbox(M, o)
                    _date_ = msg_content[0]
                    _from_ = msg_content[1]
                    _to_   = msg_content[2]
                    _subject_ = msg_content[3]
                    _msg_   = msg_content[4]
                    stdout.write("$$$$$$$$$$$\nDate: {}\nFrom: {}\nTo: {}\nSubject: {}\nMSG: {}\n".format(_date_,_from_,_to_,_subject_,_msg_))
                except Exception, e:
                    pass
        else:
            stdout.write("No {} Mail Found!".format(serach_input))
            raw_input()
            self.toast_msg()

    def login(self, try_c, *args):
        """docstring for Get_mail"""
        try:
            stdout.write("\nMail:\n")
            mail = raw_input()
            if mail:
                M.login(str(mail), getpass.getpass())
            else:
                sys.exit(1)
        except imaplib.IMAP4.error:
            if try_c<=3:
                stdout.write("Versuch: {}/3\n".format(try_c))
                stdout.write("Die eingegebene E-Mail-Adresse und das Passwort stimmen nicht uberein. Nochmal versuchen")
                try_c+=1
                self.login(try_c)
            else:
                sys.exit(1)
    def mailbox(self, *args):
        """docstring for Get_mail"""
        rv, mailboxes = M.list()
        if rv == 'OK':
            for menu in mailboxes:
                print('{}'.format(menu))
            rv, data = M.select("inbox")
            if rv == 'OK':
                return M
    def eval_decode(self, header, *args):
        """docstring for Get_mail"""
        return email.Header.decode_header(header)[0]

    def process_mailbox(self, M, num, *args):
        """docstring for Get_mail"""
        rv, header = M.fetch(num, '(RFC822)')
        if rv != 'OK':
            print "ERROR getting message", num
        header_msg = email.message_from_string(header[0][1])
        if header_msg.is_multipart():
            body=[payload.get_payload(decode=True) for payload in header_msg.get_payload()]
        else:
            body=payload.get_payload(decode=True)
        from_decode = self.eval_decode(header_msg['From'])
        subject_decode = self.eval_decode(header_msg['Subject'])
        date_decode = self.eval_decode(header_msg['Date'])
        to_decode = self.eval_decode(header_msg['To'])
        return (date_decode[0], from_decode[0], to_decode[0], subject_decode[0], str(body[0]))
def run():
    try:
        Get_mail()
    except KeyboardInterrupt:
        M.close()
        M.logout()
        sys.exit(1)
run()

关于python - Gmail API Python - 检索电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235287/

相关文章:

python - Matlab 过滤器与 Python lfilter ValueError 对象深度太小

multithreading - 在计算时保持GIF动画运行

c# - Google.Apis.Requests.RequestError - 需要 OAuth 2 访问 token

python-2.7 - 提供 Coverage 的路径以转储 .coverage

python-2.7 - sklearn 计数向量化器

google-oauth - Gmail API OAuth 错误 : Parameter not allowed for this message type: redirect_uri

google-api - 比 yyyy/mm/dd 更精确地列出消息

python - 在 python 中从 JSON 数据创建字典列表

python - 使用 Python/PIL 检测 HSV 颜色空间(来自 RGB)中的阈值

Python Geopandas 抛出 "Shell is not a Linear Ring"错误