From f67a4b051545e0a618b5e183656c27a61e86ae74 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Mon, 19 Aug 2019 10:43:07 +0200 Subject: [PATCH] add email client sketch --- email_client.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 email_client.py diff --git a/email_client.py b/email_client.py new file mode 100644 index 0000000..bac087f --- /dev/null +++ b/email_client.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +# +# Very basic example of using Python 3 and IMAP to iterate over emails in a +# gmail folder/label. This code is released into the public domain. +# +# This script is example code from this blog post: +# http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/ +# +# This is an updated version of the original -- modified to work with Python 3.4. +# +import sys +import imaplib +import getpass +import email +import email.header +import datetime +from IPython import embed +import re + + +EMAIL_ACCOUNT = "bzigr02" + +# Use 'INBOX' to read inbox. Note that whatever folder is specified, +# after successfully running this script all emails in that folder +# will be marked as read. +EMAIL_FOLDER = "INBOX" + +fields = ["Fist Name", "Last Name", "e-Mail", "Institution", "Phone", "Country", "City", "ZIP code", + "Career stage and fees", "Farewell event (Thu 20 Feb)", "Presentation type", + "Food preference", "LabTour (Fri 21 Feb)", "Messages"] + + +class Participation(object): + _fields = ["Fist Name", "Last Name", "e-Mail", "Institution", "Phone", "Country", "City", "ZIP code", + "Career stage and fees", "Farewell event (Thu 20 Feb)", "Presentation type", + "Food preference", "LabTour (Fri 21 Feb)", "Messages"] + + pass + + +def process_mailbox(M): + """ + Do something with emails messages in the folder. + For the sake of this example, print some headers. + """ + + rv, data = M.search(None, "ALL") + if rv != 'OK': + print("No messages found!") + return + + for num in data[0].split(): + rv, data = M.fetch(num, '(RFC822)') + if rv != 'OK': + print("ERROR getting message", num) + return + + msg = email.message_from_bytes(data[0][1]) + hdr = email.header.make_header(email.header.decode_header(msg['Subject'])) + embed() + subject = str(hdr) + print('Message %s: %s' % (num, subject)) + print('Raw Date:', msg['Date']) + # Now convert to local date-time + date_tuple = email.utils.parsedate_tz(msg['Date']) + if date_tuple: + local_date = datetime.datetime.fromtimestamp( + email.utils.mktime_tz(date_tuple)) + print ("Local Date:", \ + local_date.strftime("%a, %d %b %Y %H:%M:%S")) + + +def process_message(msg_index): + rv, data = M.fetch(msg_index, '(RFC822)') + if rv != 'OK': + print("ERROR getting message", num) + return + msg = email.message_from_bytes(data[0][1]) + msg_body = msg.get_payload()[0].as_string() + msg_body = re.sub(r'=20', ' ', msg_body) + msg_body = re.sub(r'=\n', '', msg_body) + msg_body = re.sub(r'=E2=82=AC', 'EUR', msg_body) + msg_body = re.sub(r'&', 'and', msg_body) + + lines = msg_body.split('\n') + + embed() + + +def process_registrations(M): + rv, msgs = M.search(None, "SUBJECT", "etho2020") + print("Found %i registration mails" % len(msgs)) + for msg_index in msgs: + process_message(msg_index) + + +M = imaplib.IMAP4_SSL('mailserv.uni-tuebingen.de') +passwd = "r.8-*0Fc" + +try: + rv, data = M.login(EMAIL_ACCOUNT, passwd) +except imaplib.IMAP4.error: + print ("LOGIN FAILED!!! ") + sys.exit(1) + + +rv, mailboxes = M.list() +if rv == 'OK': + print("Mailboxes:") + print(mailboxes) + +rv, data = M.select(EMAIL_FOLDER) +if rv == 'OK': + print("Processing mailbox...\n") + # process_mailbox(M) + +else: + print("ERROR: Unable to open mailbox ", rv) + +process_registrations(M) + +M.close() +M.logout()