简体   繁体   English

如何使用 imaplib、python 获取特定邮件的所有电子邮件附件的名称?

[英]How to get name of all email attachments of a particular mail using imaplib, python?

I am trying to fetch all the attachments of email messages and make a list of those attachments for that particular mail and save that list in a JSON file.我正在尝试获取电子邮件的所有附件并为该特定邮件列出这些附件并将该列表保存在 JSON 文件中。 I have been instructed to use imaplib only.我被指示只使用imaplib

This is the function that I am using to extract the mails data but the part.getfilename() is only returning one attachment even if I have sent multiple attachments.这是我用来提取邮件数据的函数,但是part.getfilename()只返回一个附件,即使我发送了多个附件也是如此。

The output I want is the list of attachments like [attach1.xlss, attach2.xml, attch.csv] .我想要的输出是[attach1.xlss, attach2.xml, attch.csv]之类的附件列表。 Again, I can only use imaplib library.同样,我只能使用imaplib库。 I also don't want to have to download any attachment, so please don't share that code.我也不想下载任何附件,所以请不要共享该代码。 I tried several websites but couldn't find anything that I could use.我尝试了几个网站,但找不到任何我可以使用的东西。

def get_body_and_attachments(msg):
    email_body = None
    filename = None
    html_part = None
    # if the email message is multipart
    if msg.is_multipart():
        # iterate over email parts
        for part in msg.walk():
            # extract content type of email
            content_type = part.get_content_type()
            content_disposition = str(part.get("Content-Disposition"))
            try:
                # get the email body
                body = part.get_payload(decode=True).decode()
            except:
                pass
            if content_type == "text/plain" and "attachment" not in content_disposition:
                # print text/plain emails and skip attachments
                email_body = body
            elif "attachment" in content_disposition:
                # download attachment
                print(part.get_filename(), "helloooo")
                filename = part.get_filename()
                filename = filename
    else:
        # extract content type of email
        content_type = msg.get_content_type()
        # get the email body
        body = msg.get_payload(decode=True).decode()
        if content_type == "text/plain":
            email_body = body
    if content_type == "text/html":
        html_part = body
    return email_body, filename, html_part

It was easy;很容易; I just had to do this.我只需要这样做。

import re
# getting filenames 
filenames = mailbox.uid('fetch', num, '(BODYSTRUCTURE)')[1][0]
filenames = re.findall('\("name".*?\)', str(filenames))

filenames = [filenames[i].split('" "')[1][:-2] for i in range(len(filenames))]

Explanation: mailbox.uid will fetch the message (or mail) of a particular uid (num) and will return a byte string with all the data relating to that message.说明: mailbox.uid将获取特定 uid (num) 的消息(或邮件),并将返回一个字节字符串,其中包含与该消息相关的所有数据。

Now I use re.findall to find all the attachment names and then I clean that return value and save it as a list.现在,我使用re.findall查找所有附件名称,然后清除返回值并将其保存为列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有 HTML 正文和电子邮件附件的 imaplib 在 python 3.7 中阅读电子邮件 - Read email in python 3.7 using imaplib with HTML body and attachments in the email 如何使用imaplib从python电子邮件中获取纯文本 - How to get pure text from python email using imaplib Python-转发带有附件的IMAP电子邮件(imaplib,smtplib) - Python - Forward IMAP Email with Attachments (imaplib, smtplib) 如何使用 imaplib 和 gmail 将电子邮件从所有邮件移动到垃圾箱 - How can I move an email message from All mail to Trash using imaplib and gmail 如果我使用imaplib在收件箱中有此邮件的UID,如何在All Maill文件夹中获取邮件的UID? (GMAIL) - How to get UID of mail in All Maill folder if I have UID of this mail in inbox using imaplib? (GMail) 如何使用python在imaplib中按时间对电子邮件进行排序 - How to sort email by time in imaplib using python 如何在python中使用imaplib获取电子邮件正文? - How to fetch an email body using imaplib in python? 如何在Python和Outlook中使用for循环在电子邮件中保存所有附件? - How to save all attachments in email using for loop in Python and Outlook? 使用Python和imaplib的GMail-在“所有邮件”中搜索 - GMail with Python and imaplib - searching in “All mail” 如何使用 imaplib 和 python3 仅获取 email 的主题、来自和主体 - How to get only subject,from and body of email using imaplib and python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM