[英]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.