小编Tig*_*gre的帖子

在 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 …
Run Code Online (Sandbox Code Playgroud)

python email attachment

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

attachment ×1

email ×1

python ×1