Ejemplo n.º 1
0
def get_email_messages(config,
                       auth,
                       email_from,
                       email_to,
                       subject_regexp=None,
                       date_min=None,
                       date_max=None):
    if config.verbose:
        print('GETTING EMAILS')

    query = 'from:%s AND to:%s' % (email_from, email_to)
    if date_min:
        query += ' AND after:%s' % date_to_str(date_min)
    if date_max:
        query += ' AND before:%s' % date_to_str(
            date_max + timedelta(days=1))  # make it inclusive
    if config.verbose:
        print('EMAIL SEARCH:', query)

    messages = API_Gmail(config, auth, iterate=True).users().messages().list(
        userId='me', q=query).execute()

    subject_filter = re.compile(r'%s' %
                                subject_regexp) if subject_regexp else None
    for message in messages:
        message = API_Gmail(config, auth).users().messages().get(
            userId='me', id=message['id']).execute()
        if subject_filter is None or subject_filter.match(
                get_subject(message)):
            yield message
Ejemplo n.º 2
0
def get_email_attachments(config, auth, message, attachment_regexp):

    file_filter = re.compile(r'%s' %
                             attachment_regexp) if attachment_regexp else None

    try:
        for part in message['payload'].get('parts', []):

            if part['filename']:

                # filter regexp
                if not file_filter or file_filter.match(part['filename']):

                    if 'data' in part['body']:
                        data = part['body']['data']

                    else:
                        att_id = part['body']['attachmentId']
                        att = API_Gmail(
                            config, auth).users().messages().attachments().get(
                                userId='me',
                                messageId=message['id'],
                                id=att_id).execute()
                        data = att['data']

                    file_data = BytesIO(
                        base64.urlsafe_b64decode(data.encode('UTF-8')))
                    yield part['filename'], file_data

    except HttpError as e:
        print('EMAIL ATTACHMENT ERROR:', str(e))
Ejemplo n.º 3
0
def send_email(config,
               auth,
               email_to,
               email_from,
               email_cc,
               subject,
               text,
               html=None,
               attachment_filename=None,
               attachment_rows=None):
    if config.verbose:
        print('SENDING EMAIL', email_to)

    message = MIMEMultipart('alternative')
    message.set_charset('utf8')

    message['to'] = email_to
    message['cc'] = email_cc
    message['from'] = email_from
    message['subject'] = subject
    message.attach(MIMEText(text, 'plain', 'UTF-8'))

    if html:
        message.attach(MIMEText(html, 'html', 'UTF-8'))

    if attachment_filename and attachment_rows:
        attachment = MIMEBase('text', 'csv')
        attachment.set_payload(rows_to_csv(attachment_rows).read())
        attachment.add_header('Content-Disposition',
                              'attachment',
                              filename=attachment_filename)
        encode_base64(attachment)
        message.attach(attachment)

    #API_Gmail(config, auth).users().messages().send(userId='me', body={'raw': base64.urlsafe_b64encode(message.as_string())}).execute()
    API_Gmail(config, auth).users().messages().send(
        userId='me',
        body={
            'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()
        }).execute()