forked from UofTCoders/studyGroup
-
Notifications
You must be signed in to change notification settings - Fork 1
Add lesson email #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
James-S-Santangelo
wants to merge
6
commits into
utm-coders:gh-pages
Choose a base branch
from
James-S-Santangelo:add_lesson_email
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add lesson email #17
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
184a937
Initial commit of working script. Still needs work
James-S-Santangelo c7f065e
Added functions, argparse, and HTML formatting
James-S-Santangelo 483eb94
Changing to match current date
James-S-Santangelo efbae0a
Minor typo fixes
James-S-Santangelo a607721
Added Ahmed's Email to receivers list for testing
James-S-Santangelo 3378898
Uncommented event matching block for testing. Used to ensure match wh…
James-S-Santangelo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import datetime | ||
import yaml | ||
import smtplib | ||
import argparse | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
|
||
def get_event(): | ||
"""Retrieve event matching the current date | ||
|
||
Args: | ||
None | ||
|
||
Returns: | ||
event (:obj:'dict'): Dictionary storing information on current day's event | ||
""" | ||
|
||
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: | ||
|
||
# Get only event matching current date | ||
# if event['date'] == current_date: | ||
if event['key'] == 'project-management': # Uncomment for testing only. | ||
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 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 | ||
|
||
Returns: | ||
msg (:obj:'MIMEMultipart'): Email in HTML format. See email.mime.multipart.MIMEMultipart(). | ||
""" | ||
|
||
# If event not empty, retrieve details and compose message. | ||
if event: | ||
|
||
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 = """\ | ||
<html> | ||
<head></head> | ||
<body> | ||
Hey all, | ||
<br> | ||
|
||
<p> | ||
Here is the link to today's programming workshop: | ||
</p> | ||
|
||
<p> | ||
<b>Lesson name:</b> {0}<br> | ||
<b>Lesson description:</b> {1}<br> | ||
<b>Link to lesson:</b> {2}<br> | ||
</p> | ||
|
||
<p> | ||
This Email was sent automatically using | ||
<a href="{3}">this script</a> | ||
</p> | ||
<br> | ||
|
||
Ahmed and James | ||
<br> | ||
</body> | ||
</html> | ||
""".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 encrypted (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) | ||
|
||
# Convert 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 = ['[email protected]', '[email protected]'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have the listserv added to it? |
||
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?: ") | ||
|
||
send_email(user, password, msg) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/currenty/currently