簡體   English   中英

使用imaplib從郵件中獲取android附件

[英]Getting android attachments from mail with imaplib

我可以閱讀從所有經過測試的電子郵件客戶端發送的附件,但android的默認電子郵件應用除外。 它可以通過android中的gmail應用程序工作。

這是一些代碼:

import email as emaillib

result, data = imap_conn.uid('fetch', email_id, '(RFC822)')
raw_email = data[0][1]

get_attachments(emaillib.message_from_string(raw_email))

def get_attachments(email_message_instance):
    attachments = []
    for part in email_message_instance.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue

        data = part.get_payload(decode=True)
        if not data:
            continue
        filename = part.get_filename()

        print 'appending attachment with filename: ', filename
        attachments.append((filename, data))

    return attachments

當我從android中的默認電子郵件應用發送一個附件時,它會打印:

appending attachment with filename: None
appending attachment with filename: None
appending attachment with filename: =?utf-8?B?SU1BRzAxOTMuanBn?=

有任何想法嗎?

好的,我解決了問題,看來我的android默認郵件應用為所有內容編寫了內容配置。

文件名也很糟糕。

所以我解決了這個問題(因為我只對媒體附件感興趣):

from email.header import decode_header
    def get_attachments(email_message_instance):
        attachments = []
        for part in email_message_instance.walk():

            if part.get_content_maintype() not in ['image', 'video', 'audio']:
                continue

            if part.get('Content-Disposition') is None:
                continue

            data = part.get_payload(decode=True)
            if not data:
                continue

            filename = part.get_filename()      
            filename =  u' '.join(w.decode(e or 'ascii') for w,e in decode_header(filename))        
            attachments.append((filename, data))

        return attachments

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM