1

i want a python script to download automatic periodically (like every hour) gmail attachment which is a .jpg format i tried this code but it is not working.

def get_csv_or_xl_attachments_from_msg_id(service, messageId):
    """returns a dict of all CSV attachments as pd.DataFrames
    in the email associated with `messageId`. The keys for the
    dictionary are the csv filenames"""
    msg = service.messages().get(userId='me', id=messageId).execute()
    msg_parts = msg.get('payload').get('parts')
    headers = msg.get('payload').get('headers')
    subject = [h['value'] for h in headers if h['name']=='Subject'][0]
    if not msg_parts:
        return []
    msg_parts = _flatten_nested_email_parts(msg_parts)
    att_parts = [p for p in msg_parts if p['mimeType'] in [
            CSV_MIME_TYPE, XLSX_MIME_TYPE]]
    types = [mime_type_to_dtype(p['mimeType']) for p in att_parts]
    filenames = [p['filename'] for p in att_parts]
    datas = [_get_attachment_from_part(service, messageId, p) for p in att_parts]
    dfs = [_convert_attachment_data_to_dataframe(d, t)
            for d, t in zip(datas, types)]
    return [{'emailsubject': subject, 'filename': f, 'data': d}
            for f, d in zip(filenames, dfs)]


def query_for_csv_or_xl_attachments(service, search_query):
    message_ids = query_for_message_ids(service, search_query)
    csvs = [] 
    for msg_id in message_ids:
        loop_csvs = get_csv_or_xl_attachments_from_msg_id(service, msg_id)
        csvs.extend(loop_csvs)
    return csvs

above code is returning blank list. even though I've mails with attachment. could anyone help me in this? below is URL for whole repository. https://github.com/robertdavidwest/google_api

D2D Info
  • 31
  • 2
  • Try using this link : https://stackoverflow.com/questions/41749236/download-a-csv-file-from-gmail-using-python – Anonymous Apr 10 '19 at 11:01
  • Possible duplicate of [Download a csv file from gmail using python](https://stackoverflow.com/questions/41749236/download-a-csv-file-from-gmail-using-python) – greenmarker Apr 10 '19 at 11:05

0 Answers0