在Python中从电子邮件附件中获取文件名
我使用我的凭据登录服务器,并搜索具有特定主题的电子邮件。这封电子邮件有一个附件,我想知道它的文件名和可能的扩展名。
我在 Python 中执行此操作,但每次询问文件名时,它都会返回 NONE,而实际上附件中有文件名。
from imaplib import *
import base64
import email
import os
import sys
import errno
import mimetypes
server = IMAP4("SERVER LOCATION");
server.login("USER", "PASS");
server.select("Inbox");
typ, data = server.search(None, '(SUBJECT "Hello World")');
for num in data[0].split():
typ, data = server.fetch(num, '(RFC822)');
print (data);
msg = email.message_from_string(str(data[0][1]));
counter = 1
for part in msg.walk():
print (part.as_string() + "\n")
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
filename = part.get_filename()
print (filename);
fn = msg.get_filename()
print("The Filename was:", (fn));
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
server.close()
server.logout();
我不知道为什么我一直得不到答案,有什么帮助吗?
I am loggin into a server with my credentials and I search for an email with specific subject. This certain email has an attachment which I want to know its filename and possibly extension later on.
I am doing this in Python but everytime ask for the filename, it returns NONE when in fact there is a filename in the attachment.
from imaplib import *
import base64
import email
import os
import sys
import errno
import mimetypes
server = IMAP4("SERVER LOCATION");
server.login("USER", "PASS");
server.select("Inbox");
typ, data = server.search(None, '(SUBJECT "Hello World")');
for num in data[0].split():
typ, data = server.fetch(num, '(RFC822)');
print (data);
msg = email.message_from_string(str(data[0][1]));
counter = 1
for part in msg.walk():
print (part.as_string() + "\n")
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
filename = part.get_filename()
print (filename);
fn = msg.get_filename()
print("The Filename was:", (fn));
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
server.close()
server.logout();
I don't know why I keep getting NONE as an answer, any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我遇到了同样的问题,这就是我解决它的方法:
I had the same problem, here is what I did to solve it :
您需要首先检查:
,然后在主 for 循环中:
继续检查邮件正文中存在但您不需要的内容类型。
您还可以直接检查所需的 content_type,然后读取文件名。
ex -我遇到了同样的问题,我的内容类型是 multipart 和 text/html 和 application/json
我没有检查文本/html 并想要阅读“application/json”中的附件。我直接读取文件名,因此出现错误 - 文件名无。
当我检查时-
不会出现错误。
我希望它有帮助
You need to first check:
and then in the main for loop:
just continue for the content types that are present in the mail body but are not required by you.
You can also put a check directly for the content_type required and then read the filename.
ex -I faced a same problem and my content types were multipart and text/html and application/json
I did not put the check for text/html and wanted to read the attachment which was in "application/json". I was directly reading the filename and hence the error - filename None was coming.
When I put the check-
No error would come.
I hope it helps
我也面临类似的问题。只需删除
如果part.get_content_maintype() == 'multipart':
继续
条件,它会工作得很好。
I also faced similar problem. Just remove
if part.get_content_maintype() == 'multipart':
continue
condition and it will work fine.
用上面的答案解决了这个问题...加上我刚刚在循环中打印结果:
然后确定哪个内容部分具有文件名:
现在代码:
Solved this issue with answers above ... plus I just printed results in the loop:
Then identify which content part has the file name:
Now code: