如何从Python脚本下载Outlook附件?

发布于 2021-01-29 16:23:15

我需要使用Python Script从邮件中下载没有过去附件的传入附件。

例如:如果有人在此时(现在)发送邮件,则只需将该附件下载到本地驱动器即可,而不是过去的附件。

请任何人帮助我使用python脚本或java下载附件。

关注者
0
被浏览
138
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    复制此代码时,出现错误:

    连接=无^ IndentationError:应缩进的块

    然后,当我缩进代码时,

    class FetchEmail():
        connection = None
        error = None
        mail_server = "imap.gmail.com"
        username = "dummyoffers@gmail.com"
        password = "opennepo"
        self.save_attachment(self, msg, download_folder)
    

    我收到另一个错误:

    self.save_attachment(self,msg,download_folder)NameError:未定义名称“ self”

    import email
    import imaplib
    import os
    
    class FetchEmail():
    
    connection = None
    error = None
    mail_server="host_name"
    username="outlook_username"
    password="password"
    self.save_attachment(self,msg,download_folder)
    def __init__(self, mail_server, username, password):
        self.connection = imaplib.IMAP4_SSL(mail_server)
        self.connection.login(username, password)
        self.connection.select(readonly=False) # so we can mark mails as read
    
    def close_connection(self):
        """
        Close the connection to the IMAP server
        """
        self.connection.close()
    
    def save_attachment(self, msg, download_folder="/tmp"):
        """
        Given a message, save its attachments to the specified
        download folder (default is /tmp)
    
        return: file path to attachment
        """
        att_path = "No attachment found."
        for part in msg.walk():
            if part.get_content_maintype() == 'multipart':
                continue
            if part.get('Content-Disposition') is None:
                continue
    
            filename = part.get_filename()
            att_path = os.path.join(download_folder, filename)
    
            if not os.path.isfile(att_path):
                fp = open(att_path, 'wb')
                fp.write(part.get_payload(decode=True))
                fp.close()
        return att_path
    
    def fetch_unread_messages(self):
        """
        Retrieve unread messages
        """
        emails = []
        (result, messages) = self.connection.search(None, 'UnSeen')
        if result == "OK":
            for message in messages[0].split(' '):
                try: 
                    ret, data = self.connection.fetch(message,'(RFC822)')
                except:
                    print "No new emails to read."
                    self.close_connection()
                    exit()
    
                msg = email.message_from_string(data[0][1])
                if isinstance(msg, str) == False:
                    emails.append(msg)
                response, data = self.connection.store(message, '+FLAGS','\\Seen')
    
            return emails
    
        self.error = "Failed to retreive emails."
        return emails
    

    上面的代码可以让我下载附件,希望对任何人都有帮助。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看