Saving Email Attachments

I am trying to parse email with attached pdf files

def get_files(poruka):
    pdfs = []
    if poruka.is_multipart():
        for part in poruka.get_payload():
            if part.get_content_type() == 'application/pdf':
                data = part.get_payload()
                temppdf = tempfile.NamedTemporaryFile('w+b', -1)
                temppdf.write(base64.b64decode(data))
                pdfs.append(temppdf)

    return pdfs

This works, but in pdfsI have file instances. I am trying to save files with the original name inside a folder called storage.

When I try open(temppdf, 'wb').write(temppdf.get_payload(decode=True)), I got an error TypeError: coercing to Unicode: need string or buffer, instance found.

Also how to get pdfs [0] filename?

+4
source share
1 answer

, -, . open(temppdf.name), ( ). Python 2.6 delete=False NamedTemporaryFile, temppdf.name .

0

Source: https://habr.com/ru/post/1613481/


All Articles