From 184a93725ff83b32d8baf3cd1317242044b3fe41 Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 21:55:29 -0400 Subject: [PATCH 1/6] Initial commit of working script. Still needs work --- scripts/lessonEmail.py | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 scripts/lessonEmail.py diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py new file mode 100644 index 000000000..75c212e30 --- /dev/null +++ b/scripts/lessonEmail.py @@ -0,0 +1,66 @@ +import datetime +import yaml +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + + +today = datetime.date.today() + +# YY/mm/dd +current_date = today.strftime("%Y-%m-%d") +# print(current_date) + +# Identify sender and receiver +sender = 'james.santangelo@mail.utoronto.ca' +receivers = ['james.santangelo37@gmail.com'] + +with open('../_data/events.yml', 'r') as stream: + try: + # Load events with as separate list elements + all_events = yaml.safe_load(stream) + + # Loop through list and parse event dictionary + for event in all_events: + if event['key'] == "project-management": + + date = event['date'] + key = event['key'] + description = event['description'] + subject = '{0} Programming workshop: {1}'.format(date, key) + youtube_link = event['youtube_link'] + # print(youtube_link) + + msg = MIMEMultipart() + msg['From'] = sender + msg['To'] = ', '.join(receivers) + msg['Subject'] = subject + + body = """ + Hey all, + + Here is the link to today's programming workshop: + + Lesson name: {0} + Lesson description: {1} + Link to lesson: {2} + + """.format(key, description, youtube_link) + msg.attach(MIMEText(body, 'plain')) + except yaml.YAMLError as exc: + print(exc) + +try: + smtp = smtplib.SMTP(host='smtp.utoronto.ca', port=587) + smtp.ehlo() + smtp.starttls() + smtp.ehlo() + smtp.login('santang3', 'q63dVyiJqg6f') + text = msg.as_string() + print(text) + smtp.sendmail(sender, receivers, text) + smtp.quit() + print("Successfully sent email") +except Exception as E: + print(E) + # print("Error: unable to send email") From c7f065e06ad0b340f8506b6f95432863ee38f25d Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 23:36:27 -0400 Subject: [PATCH 2/6] Added functions, argparse, and HTML formatting --- scripts/lessonEmail.py | 184 +++++++++++++++++++++++++++++++---------- 1 file changed, 139 insertions(+), 45 deletions(-) diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py index 75c212e30..6c6ddbbb0 100644 --- a/scripts/lessonEmail.py +++ b/scripts/lessonEmail.py @@ -1,66 +1,160 @@ import datetime import yaml import smtplib +import argparse from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -today = datetime.date.today() +def get_event(): + """Retrieve event matching the current date -# YY/mm/dd -current_date = today.strftime("%Y-%m-%d") -# print(current_date) + Args: + None -# Identify sender and receiver -sender = 'james.santangelo@mail.utoronto.ca' -receivers = ['james.santangelo37@gmail.com'] + Returns: + event (:obj:'dict'): Dictionary storing information on current day's event + """ -with open('../_data/events.yml', 'r') as stream: - try: - # Load events with as separate list elements + today = datetime.date.today() + + # YY/mm/dd + current_date = today.strftime("%Y-%m-%d") # E.g. 2019-04-18 + + with open('../_data/events.yml', 'r') as stream: + + # Load all events with as separate list elements all_events = yaml.safe_load(stream) # Loop through list and parse event dictionary for event in all_events: - if event['key'] == "project-management": - date = event['date'] - key = event['key'] - description = event['description'] - subject = '{0} Programming workshop: {1}'.format(date, key) - youtube_link = event['youtube_link'] - # print(youtube_link) + # Get only event matching current date + if event['key'] == 'project-management': + return event + + +def create_message(sender, receivers, event): + """Parses today's event dictionary and composes HTML message with details + + Args: + sender ('str'): Sender Email. Provided ast command-line. Must be UToronto Email + receivers (:obj:'list' of :obj:'str'): List with receiving Emails as string elements. Email sent to all Emails in list. + event (:obj:'dict'): Dictionary storing information on current day's event + + Returns: + msg (:obj:'MIMEMultipart'): Email in HTML format. See email.mime.multipart.MIMEMultipart(). + """ - msg = MIMEMultipart() - msg['From'] = sender - msg['To'] = ', '.join(receivers) - msg['Subject'] = subject + # If event not empty, retrieve details and compose message. + if event: - body = """ + path_to_script = 'https://github.com/utm-coders/studyGroup/blob/gh-pages/scripts/lessonEmail.py' + + date = event['date'] + key = event['key'] + description = event['description'] + subject = '{0} Programming workshop: {1}'.format(date, key) + youtube_link = event['youtube_link'] + # print(youtube_link) + + # Create email MIMEMultipart object + msg = MIMEMultipart() + msg['From'] = sender + msg['To'] = ', '.join(receivers) + msg['Subject'] = subject + + body = """\ + + + Hey all, +
+

Here is the link to today's programming workshop: +

+ +

+ Lesson name: {0}
+ Lesson description: {1}
+ Link to lesson: {2}
+

+ +

+ This Email was sent automatically using + this script +

+
+ + Ahmed and James +
+ + + """.format(key, description, youtube_link, path_to_script) + + # Add body to MIMEMultipart object + msg.attach(MIMEText(body, 'html')) # Make sure message is HTML + + return msg + else: + # If no events matched. Print and exit. + print("There were no events matching the current date. Exiting!") + raise SystemExit + + +def send_email(user, password, msg): + """Send Email with today's event detail, including link to stream + + Args: + user ('str'): UTORID. Asked from user as input. + password ('str'): UTOR password. Asked from user as input. + msg (:obj:'MIMEMultipart'): Email in HTML format. See email.mime.multipart.MIMEMultipart(). + + Returns: + None: Sends ecrypted (TLS) Email over SMTP server. + """ + + try: + # Start SMTP server. Host currently fixed as UofT + smtp = smtplib.SMTP(host='smtp.utoronto.ca', port=587) + + # Identify the client (i.e., yourself), start secure connection and login + smtp.ehlo() + smtp.starttls() + smtp.ehlo() + smtp.login(user=user, password=password) + + # Conver message text to string and send + text = msg.as_string() + # print(text) + smtp.sendmail(sender, receivers, text) + smtp.quit() + print("Successfully sent email") + + except Exception as E: + print(E) + print("Error: unable to send email") + + +if __name__ == '__main__': + + # Define command line arguments + parser = argparse.ArgumentParser( + prog="Send Email to UTM biology department with details for current day's programming workshop, including link to livestream.") + parser.add_argument( + 'sender', help="Email from which to send Email. Currenty, must be UToronto Email.", type=str) + args = vars(parser.parse_args()) + + event = get_event() # Get current day's event + sender = args['sender'] # Get sender from command-line + + # Email sent to all addresses in list + receivers = ['james.santangelo37@gmail.com'] + msg = create_message(sender, receivers, event) # Create Email message + + # Get user-provided UTORID and password. + user = input("What is your UTORID?: ") + password = input("What is your password?: ") - Lesson name: {0} - Lesson description: {1} - Link to lesson: {2} - - """.format(key, description, youtube_link) - msg.attach(MIMEText(body, 'plain')) - except yaml.YAMLError as exc: - print(exc) - -try: - smtp = smtplib.SMTP(host='smtp.utoronto.ca', port=587) - smtp.ehlo() - smtp.starttls() - smtp.ehlo() - smtp.login('santang3', 'q63dVyiJqg6f') - text = msg.as_string() - print(text) - smtp.sendmail(sender, receivers, text) - smtp.quit() - print("Successfully sent email") -except Exception as E: - print(E) - # print("Error: unable to send email") + send_email(user, password, msg) From 483eb945282888691ee75f9c21eb79a8051feff7 Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 23:36:59 -0400 Subject: [PATCH 3/6] Changing to match current date --- scripts/lessonEmail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py index 6c6ddbbb0..58c020b59 100644 --- a/scripts/lessonEmail.py +++ b/scripts/lessonEmail.py @@ -30,7 +30,7 @@ def get_event(): for event in all_events: # Get only event matching current date - if event['key'] == 'project-management': + if event['date'] == current_date: return event From efbae0a9c790b00026260a9cbe6b7dcaff86ec16 Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 23:46:30 -0400 Subject: [PATCH 4/6] Minor typo fixes --- scripts/lessonEmail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py index 58c020b59..d8462f8c3 100644 --- a/scripts/lessonEmail.py +++ b/scripts/lessonEmail.py @@ -38,7 +38,7 @@ def create_message(sender, receivers, event): """Parses today's event dictionary and composes HTML message with details Args: - sender ('str'): Sender Email. Provided ast command-line. Must be UToronto Email + sender ('str'): Sender Email. Provided as command-line. Must be UToronto Email receivers (:obj:'list' of :obj:'str'): List with receiving Emails as string elements. Email sent to all Emails in list. event (:obj:'dict'): Dictionary storing information on current day's event @@ -112,7 +112,7 @@ def send_email(user, password, msg): msg (:obj:'MIMEMultipart'): Email in HTML format. See email.mime.multipart.MIMEMultipart(). Returns: - None: Sends ecrypted (TLS) Email over SMTP server. + None: Sends encrypted (TLS) Email over SMTP server. """ try: @@ -125,7 +125,7 @@ def send_email(user, password, msg): smtp.ehlo() smtp.login(user=user, password=password) - # Conver message text to string and send + # Convert message text to string and send text = msg.as_string() # print(text) smtp.sendmail(sender, receivers, text) From a6077216d281280928d14cd508e0438d6f7334c5 Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 23:47:49 -0400 Subject: [PATCH 5/6] Added Ahmed's Email to receivers list for testing --- scripts/lessonEmail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py index d8462f8c3..05f7cc7ce 100644 --- a/scripts/lessonEmail.py +++ b/scripts/lessonEmail.py @@ -150,7 +150,7 @@ def send_email(user, password, msg): sender = args['sender'] # Get sender from command-line # Email sent to all addresses in list - receivers = ['james.santangelo37@gmail.com'] + receivers = ['james.santangelo37@gmail.com', 'ahmed.hasan@mail.utoronto.ca'] msg = create_message(sender, receivers, event) # Create Email message # Get user-provided UTORID and password. From 33788981c2860684f5a9d7c61bf96f70b07088dd Mon Sep 17 00:00:00 2001 From: James Santangelo Date: Thu, 18 Apr 2019 23:50:15 -0400 Subject: [PATCH 6/6] Uncommented event matching block for testing. Used to ensure match while script being tested --- scripts/lessonEmail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/lessonEmail.py b/scripts/lessonEmail.py index 05f7cc7ce..35d4b857d 100644 --- a/scripts/lessonEmail.py +++ b/scripts/lessonEmail.py @@ -30,7 +30,8 @@ def get_event(): for event in all_events: # Get only event matching current date - if event['date'] == current_date: + # if event['date'] == current_date: + if event['key'] == 'project-management': # Uncomment for testing only. return event