extract credentials to config file

This commit is contained in:
admin 2019-10-11 13:33:34 +02:00
parent 93288946cd
commit c2bcda59b6
3 changed files with 44 additions and 16 deletions

View File

@ -1,2 +1,15 @@
# etho2020_registration # Etho2020 email client listening for registration emails
* checks every 120s for new messages on a given email address, parses the information and sends and confirmation email to the participant and a copy to the conference office.
## Configuration
The root folder contains a ```credentials_template.py``` file
containing dummy credentials. Fill in the valid information and rename
file to ```credentials.py```. The email client expects to find this
file, otherwise it will not work.
**Beware** the valid ```credentials.py``` file must not be committed to
the version controll system and be read-only for all exept the user
running the client.

13
credentials_template.py Normal file
View File

@ -0,0 +1,13 @@
EMAIL_ACCOUNT = 'yourlogin'
EMAIL_FOLDER = 'INBOX'
EMAIL_ADDRESS = 'the email address that needs to be checked'
EMAIL_PSWD = 'your_password for email account'
EMAIL_REPLY = 'email address of the conference office'
SMTP_SERVER = 'outgoing smtp server address'
IMAP_SERVER = 'server address for incoming mails'
SUBJECT_PATTERN = "etho2020"
CHECK_INTERVAL = 120 # check every 120s for new mails
LOGFILE_NAME = 'etho_registration.log'
LOG_STATUS_INTERVAL = 60 # write status message every 60 checks (2h)

View File

@ -13,13 +13,15 @@ import datetime as dt
import time import time
import glob import glob
from credentials import *
EMAIL_ACCOUNT = "bzigr02" EMAIL_ACCOUNT = "bzigr02"
EMAIL_FOLDER = "INBOX" EMAIL_FOLDER = "INBOX"
EMAIL_ADDRESS = 'etho2020registration@biologie.uni-tuebingen.de' EMAIL_ADDRESS = 'etho2020registration@biologie.uni-tuebingen.de'
EMAIL_REPLY = 'etho2020@biologie.uni-tuebingen.de' EMAIL_REPLY = 'etho2020@biologie.uni-tuebingen.de'
SMTP_SERVER = 'smtpserv.uni-tuebingen.de' SMTP_SERVER = 'smtpserv.uni-tuebingen.de'
IMAP_SERVER = 'mailserv.uni-tuebingen.de' IMAP_SERVER = 'mailserv.uni-tuebingen.de'
EMAIL_PSWD = "r.8-*0Fc" EMAIL_PSWD = "4npBaMv9"
PARTICIPANTS_FOLDER = 'participants' PARTICIPANTS_FOLDER = 'participants'
@ -194,7 +196,7 @@ class Participation(object):
values = [self._first_name, self._last_name, self._email, self._phone, self._institution, values = [self._first_name, self._last_name, self._email, self._phone, self._institution,
self._address_street, "%s %s" % (self._address_zip, self._address_city), self._address_street, "%s %s" % (self._address_zip, self._address_city),
self.registration_date.isoformat(), self._role, str(self.amount_due), str(self.farewell_event), self.registration_date.isoformat(), self._role, str(self.amount_due), str(self.farewell_event),
str(self.gwinner_award), str(self.labtour), str(self._icebreaker), str(self._food_vegi), str(self._food_vegan), str(self.gwinner_award), str(self.labtour), str(self.icebreaker), str(self._food_vegi), str(self._food_vegan),
str(self._food_gluten), str(self._food_normal)] str(self._food_gluten), str(self._food_normal)]
with io.open(filename, mode="w", encoding="UTF8") as fd: with io.open(filename, mode="w", encoding="UTF8") as fd:
@ -235,16 +237,15 @@ def process_mailbox(M):
Do something with emails messages in the folder. Do something with emails messages in the folder.
For the sake of this example, print some headers. For the sake of this example, print some headers.
""" """
rv, data = M.search(None, "UNSEEN") rv, data = M.search(None, "UNSEEN")
if rv != 'OK': if rv != 'OK':
print("No messages found!") print("No messages found!", file=log_file)
return return
for num in data[0].split(): for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)') rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK': if rv != 'OK':
print("ERROR getting message", num) print("ERROR getting message", num, file=log_file)
return return
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
@ -264,17 +265,17 @@ def process_mailbox(M):
def process_message(M, msg_index): def process_message(M, msg_index):
rv, data = M.fetch(msg_index.encode('ascii'), '(RFC822)') rv, data = M.fetch(msg_index.encode('ascii'), '(RFC822)')
if rv != 'OK': if rv != 'OK':
print("ERROR getting message", rv) print("ERROR getting message", rv, file=log_file)
return return
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
return Participation(msg) return Participation(msg)
def process_registrations(M): def process_registrations(M):
rv, msgs = M.search(None, "SUBJECT", "etho2020", "UNSEEN") rv, msgs = M.search(None, "SUBJECT", SUBJECT_PATTERN, "UNSEEN")
participations = [] participations = []
if rv != "OK": if rv != "OK":
print("ERROR searching messages", rv) print("ERROR searching messages", rv, file=log_file)
return participations return participations
msgs = msgs[0].decode('ascii').split() msgs = msgs[0].decode('ascii').split()
@ -334,16 +335,17 @@ def check_for_mails():
try: try:
rv, data = M.login(EMAIL_ACCOUNT, EMAIL_PSWD) rv, data = M.login(EMAIL_ACCOUNT, EMAIL_PSWD)
except imaplib.IMAP4.error: except imaplib.IMAP4.error:
print("LOGIN FAILED!!! ") print("LOGIN FAILED!!! ", file=log_file)
sys.exit(1) return
# sys.exit(1)
# rv, mailboxes = M.list() # rv, mailboxes = M.list()
# if rv == 'OK': # if rv == 'OK':
# print("Mailboxes:") # print("Mailboxes:")
# print(mailboxes) # print(mailboxes)
rv, data = M.select(EMAIL_FOLDER) rv, data = M.select(EMAIL_FOLDER)
if rv != 'OK': if rv != 'OK':
print("ERROR: Unable to open mailbox folder ", rv) print("ERROR: Unable to open mailbox folder ", rv, file=log_file)
return
new_participations = process_registrations(M) new_participations = process_registrations(M)
for p in new_participations: for p in new_participations:
print("%s\tNew registration by %s" % (dt.datetime.now().isoformat(), p.name), file=log_file) print("%s\tNew registration by %s" % (dt.datetime.now().isoformat(), p.name), file=log_file)
@ -354,12 +356,12 @@ def check_for_mails():
if __name__ == "__main__": if __name__ == "__main__":
with open('etho_registration.log', 'a', buffering=1) as log_file: with open(LOGFILE_NAME, 'a', buffering=1) as log_file:
count = 0 count = 0
while True: while True:
check_for_mails() check_for_mails()
time.sleep(120) time.sleep(CHECK_INTERVAL)
count +=1 count +=1
if count % 60 == 0: if count % LOG_STATUS_INTERVAL == 0:
print("%s\t Still awake and waiting for registrations!" % (dt.datetime.now().isoformat()), file=log_file) print("%s\t Still awake and waiting for registrations!" % (dt.datetime.now().isoformat()), file=log_file)