jeremyephron / simplegmail

A simple Gmail API client for applications in Python
MIT License
336 stars 73 forks source link

Add attachment headers #69

Open rasa opened 2 years ago

rasa commented 2 years ago

My editor removed trailing spaces, sorry 'bout that.

Example usage:

from simplegmail import Gmail

gmail = Gmail()

messages = gmail.get_unread_inbox()

message = messages[0]
if message.attachments:
    for attm in message.attachments:
        print('File: ' + attm.filename)
        if 'ex@ex.com' in attm.headers.get('From'):
            attm.save()  # downloads and saves each attachment under its name
        for k, v in attm.headers.items():
            # RFC 5322 allows for duplicate header keys. See https://www.rfc-editor.org/rfc/rfc5322#section-3.6
            if isinstance(v, list):
                v = ', '.join(v)
            print('%s: %s' % (k, v))