Downloading multiple attachments using imaplib

后端 未结 6 967
星月不相逢
星月不相逢 2020-11-29 03:39

How can I download multiple attachments from a single mail using imaplib?

Let\'s say I have an e-mail and that e-mail contains 4 attachments. How can I download all

6条回答
  •  失恋的感觉
    2020-11-29 04:11

    * You can try following function to get mail attachment
    
    def create_message_attachment(self,msg_str):
            count = 1
            body = ''
            content_id = ''
            for part in msg_str.walk():
                file_name_gl = None
                mptype = part.get_content_maintype()
                file_name_gl = part.get_filename()
                if mptype == "multipart":
                    continue
                elif mptype == "text":
                    if not file_name_gl: continue
                elif mptype == "image":
                    content_id = part.get('Content-ID')
                    if not file_name_gl:
                        file_name_gl = 'image_' + str(count) + '.' + part.get_content_subtype()
                        count = count + 1
    
                body = part.get_payload(decode = True)
                if type(body) <> type(None) :
                    body = body.strip()
                    if body <> "":
                        body = base64.encodestring(body)
    

提交回复
热议问题