Get the Gmail attachment filename without downloading it

后端 未结 4 1986
有刺的猬
有刺的猬 2020-12-31 09:56

I\'m trying to get all the messages from a Gmail account that may contain some large attachments (about 30MB). I just need the names, not the whole files. I found a piece of

4条回答
  •  长情又很酷
    2020-12-31 10:25

    Old question, but just wanted to share the solution to this I came up with today. Searches for all emails with attachments and outputs the uid, sender, subject, and a formatted list of attachments. Edited relevant code to show how to format BODYSTRUCTURE:

        data   = mailobj.uid('fetch', mail_uid, '(BODYSTRUCTURE)')[1]
        struct = data[0].split()        
        list   = []                     #holds list of attachment filenames
    
        for j, k in enumerate(struct):
            if k == '("FILENAME"':
                count = 1
                val = struct[j + count]
                while val[-3] != '"':
                    count += 1
                    val += " " + struct[j + count]
                list.append(val[1:-3])
            elif k == '"FILENAME"':
                count = 1
                val = struct[j + count]
                while val[-1] != '"':
                    count += 1
                    val += " " + struct[j + count]
                list.append(val[1:-1])
    

    I've also published it on GitHub.

提交回复
热议问题