首页 > 解决方案 > IMAP - 忽略图片或附件?

问题描述

我正在处理我的第一个 Python 项目,现在我已经被困了一段时间。这是一个程序,可以从特定的电子邮件文件夹中读取我的电子邮件并将消息保存到 csv 文件中。问题是当有人包含图像或附加某些东西时,程序会保存图像代码而不是消息。

def checkmail():
    try:
        # create an IMAP4 class with SSL
        imap = imaplib.IMAP4_SSL("imap.gmail.com")
        imap.login(username, password)
        imap.select("FromB") #Inbox to use
        result, data = imap.uid('search', None, "UNSEEN")  # (ALL/UNSEEN)
        inbox_item_list = data[0].split()

        most_recent = inbox_item_list[-1]

        for item in inbox_item_list:
            result2, email_data = imap.uid('fetch', most_recent, '(RFC822)')
            raw_email = email_data[0][1].decode("utf-8")
            email_message = email.message_from_string(raw_email)
            date_ = email_message['date'].format(time.strftime("%d-%b-%Y"))
            counter = 1
            for part in email_message.walk():
                if part.get_content_maintype() == "multipart":
                    continue
                filename = part.get_filename()
                content_type = part.get_content_type()

                if not filename:
                    ext = mimetypes.guess_extension(part.get_content_type())
                    if not ext:
                        ext = '.bin'
                    if 'text' in content_type:
                        ext = '.txt'
                    filename = 'msg-part-%08d%s' %(counter, ext)
                counter += 1

            save_path = os.path.join(os.getcwd(), "jobb")
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            with open(os.path.join(save_path, filename), 'wb') as fp:
                fp.write(part.get_payload(decode=True))


                #print(content_type)
                if "plain" in content_type:
                    print("z")
                    #print(part.get_payload())
                elif "html" in content_type:
                    html_ = part.get_payload(decode=True)
                    charset = part.get_content_charset('iso-8859-1')
                    chars = html_.decode(charset, 'replace')
                    soup = BeautifulSoup(chars, "html.parser")
                    text_mail = soup.get_text()
                    print("En ny csv sparad")
                else:
                    # Here I have just a print function earlier and everytime 
                    # there was an image the program printed the print function. 
                    # So I figured what if I instead put in the code that saves
                    # the text when there is no image but that didn't work. And 
                    # now I'm stuck I don't know how to solve this and would 
                    # like guidance.
                    html_ = part.get_payload(decode=True)
                    charset = part.get_content_charset('iso-8859-1')
                    chars = html_.decode(charset, 'replace')
                    soup = BeautifulSoup(chars, "html.parser")
                    text_mail = soup.get_text()
                    print("One new csv saved")

        testing123 = {'text': [text_mail],
                      'date': [date_]
                     }

        df = pd.DataFrame(testing123, columns=['text', 'date'])
        dt = datetime.today()
        date_today = dt.timestamp()
        df.to_csv("D:\APMPYTHON\(%s).csv" % date_today)
    except:
        print("No new email")

标签: pythonpandasdataframeimap

解决方案


推荐阅读