Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 2.7中将电子邮件附件保存到文件_Python_Email_Python 2.7 - Fatal编程技术网

在python 2.7中将电子邮件附件保存到文件

在python 2.7中将电子邮件附件保存到文件,python,email,python-2.7,Python,Email,Python 2.7,嗨,我有一个类似的问题: 我想通过电子邮件将所有文件附件保存到硬盘上的文件。但如果我是正确的,并不是多部分电子邮件的所有部分都是“真实”文件(例如,电子邮件中html部分的一些图像)。我想100%确定我保存的文件是附件 现在我有这个: mail = ""; for line in sys.stdin: mail += line; msg = email.message_from_string(mail); for part in msg.walk(): check if i

嗨,我有一个类似的问题:

我想通过电子邮件将所有文件附件保存到硬盘上的文件。但如果我是正确的,并不是多部分电子邮件的所有部分都是“真实”文件(例如,电子邮件中html部分的一些图像)。我想100%确定我保存的文件是附件

现在我有这个:

mail = "";
for line in sys.stdin:
    mail += line;

msg = email.message_from_string(mail);

for part in msg.walk():
    check if is file and save
基于

我成功创建了以下代码:

import sys;
import email

class Attachement(object):
    def __init__(self):
        self.data = None;
        self.content_type = None;
        self.size = None;
        self.name = None;



def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None);
    if content_disposition:
        dispositions = content_disposition.strip().split(";");
        if bool(content_disposition and dispositions[0].lower() == "attachment"):

            attachment = Attachement();
            attachment.data = message_part.get_payload(decode=True);
            attachment.content_type = message_part.get_content_type();
            attachment.size = len(attachment.data);
            attachment.name = message_part.get_filename();

            return attachment;

    return None;


if __name__=='__main__':

    mail = "";
    for line in sys.stdin:
        mail += line;


    msg = email.message_from_string(mail);

    attachements = list();

    if(msg.is_multipart()):
        for part in msg.walk():
            attachement = parse_attachment(part);
            if(attachement):
                attachements.append(attachement);




    for att in attachements:
        # write file in binary mode
        file = open(att.name, 'wb');
        file.write(att.data);
        file.close();



    print 'OK';

@kbickar抓得好。