From b63f9c04e417072941d82d1040be5512741512b4 Mon Sep 17 00:00:00 2001 From: Wesley Thompson Date: Tue, 6 Jun 2017 18:17:00 -0500 Subject: [PATCH 1/5] Modify emailUser to have optional image file path --- client/app_utils.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/client/app_utils.py b/client/app_utils.py index edc8467ae..5470a84c2 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -1,6 +1,9 @@ # -*- coding: utf-8-*- +import os import smtplib from email.MIMEText import MIMEText +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart import urllib2 import re from pytz import timezone @@ -27,8 +30,35 @@ def sendEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER): session.sendmail(SENDER, TO, msg.as_string()) session.quit() +def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, IMAGE_FILE): + """Sends an HTML email and an image attachment""" + for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': + try: + BODY.encode(body_charset) + except UnicodeError: + pass + else: + break + + imageData = open(IMAGE_FILE, 'rb').read() + msg = MIMEMultipart() + msg['From'] = SENDER + msg['To'] = TO + msg['Subject'] = SUBJECT -def emailUser(profile, SUBJECT="", BODY=""): + emailText = MIMEText(BODY.encode(body_charset), 'html', body_charset) + msg.attach(emailText) + emailImage = MIMEImage(imageData, name=os.path.basename(IMAGE_FILE)) + msg.attach(emailImage) + + SMTP_PORT = 587 + session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) + session.starttls() + session.login(FROM, PASSWORD) + session.sendmail(SENDER, TO, msg.as_string()) + session.quit() + +def emailUser(profile, SUBJECT="", BODY="", imageFile=None): """ sends an email. @@ -73,8 +103,12 @@ def generateSMSEmail(profile): user = profile['gmail_address'] password = profile['gmail_password'] server = 'smtp.gmail.com' - sendEmail(SUBJECT, BODY, recipient, user, + if imageFile is None: + sendEmail(SUBJECT, BODY, recipient, user, "Jasper ", password, server) + else: + sendImageEmail(SUBJECT, BODY, recipient, user, + "Jasper ", password, server, imageFile) return True except: From b0c424b1c44a912b5658a0d7787a81473af3047c Mon Sep 17 00:00:00 2001 From: Wesley Thompson Date: Tue, 6 Jun 2017 18:17:00 -0500 Subject: [PATCH 2/5] Modify emailUser to have optional image file path Add sendImageEmail functionality --- client/app_utils.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/client/app_utils.py b/client/app_utils.py index edc8467ae..5470a84c2 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -1,6 +1,9 @@ # -*- coding: utf-8-*- +import os import smtplib from email.MIMEText import MIMEText +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart import urllib2 import re from pytz import timezone @@ -27,8 +30,35 @@ def sendEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER): session.sendmail(SENDER, TO, msg.as_string()) session.quit() +def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, IMAGE_FILE): + """Sends an HTML email and an image attachment""" + for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': + try: + BODY.encode(body_charset) + except UnicodeError: + pass + else: + break + + imageData = open(IMAGE_FILE, 'rb').read() + msg = MIMEMultipart() + msg['From'] = SENDER + msg['To'] = TO + msg['Subject'] = SUBJECT -def emailUser(profile, SUBJECT="", BODY=""): + emailText = MIMEText(BODY.encode(body_charset), 'html', body_charset) + msg.attach(emailText) + emailImage = MIMEImage(imageData, name=os.path.basename(IMAGE_FILE)) + msg.attach(emailImage) + + SMTP_PORT = 587 + session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) + session.starttls() + session.login(FROM, PASSWORD) + session.sendmail(SENDER, TO, msg.as_string()) + session.quit() + +def emailUser(profile, SUBJECT="", BODY="", imageFile=None): """ sends an email. @@ -73,8 +103,12 @@ def generateSMSEmail(profile): user = profile['gmail_address'] password = profile['gmail_password'] server = 'smtp.gmail.com' - sendEmail(SUBJECT, BODY, recipient, user, + if imageFile is None: + sendEmail(SUBJECT, BODY, recipient, user, "Jasper ", password, server) + else: + sendImageEmail(SUBJECT, BODY, recipient, user, + "Jasper ", password, server, imageFile) return True except: From aa535da13d6cc71aa6845018d54600c8118238c9 Mon Sep 17 00:00:00 2001 From: Wesley Thompson Date: Tue, 6 Jun 2017 19:11:55 -0500 Subject: [PATCH 3/5] Revised formatting after running flake8 --- client/app_utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/client/app_utils.py b/client/app_utils.py index 5470a84c2..479150ec1 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -30,7 +30,9 @@ def sendEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER): session.sendmail(SENDER, TO, msg.as_string()) session.quit() -def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, IMAGE_FILE): + +def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, + IMAGE_FILE): """Sends an HTML email and an image attachment""" for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': try: @@ -39,18 +41,16 @@ def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, IMAGE pass else: break - imageData = open(IMAGE_FILE, 'rb').read() msg = MIMEMultipart() msg['From'] = SENDER msg['To'] = TO msg['Subject'] = SUBJECT - emailText = MIMEText(BODY.encode(body_charset), 'html', body_charset) msg.attach(emailText) emailImage = MIMEImage(imageData, name=os.path.basename(IMAGE_FILE)) msg.attach(emailImage) - + SMTP_PORT = 587 session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.starttls() @@ -58,6 +58,7 @@ def sendImageEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER, IMAGE session.sendmail(SENDER, TO, msg.as_string()) session.quit() + def emailUser(profile, SUBJECT="", BODY="", imageFile=None): """ sends an email. @@ -107,7 +108,7 @@ def generateSMSEmail(profile): sendEmail(SUBJECT, BODY, recipient, user, "Jasper ", password, server) else: - sendImageEmail(SUBJECT, BODY, recipient, user, + sendImageEmail(SUBJECT, BODY, recipient, user, "Jasper ", password, server, imageFile) return True From e17944c5ac3a0bceb8e3edc4cc0e6ecb08150fd1 Mon Sep 17 00:00:00 2001 From: Wesley Thompson Date: Tue, 6 Jun 2017 19:37:26 -0500 Subject: [PATCH 4/5] Add more styling changes --- client/app_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/app_utils.py b/client/app_utils.py index 479150ec1..ac31f63e1 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -106,10 +106,10 @@ def generateSMSEmail(profile): server = 'smtp.gmail.com' if imageFile is None: sendEmail(SUBJECT, BODY, recipient, user, - "Jasper ", password, server) + "Jasper ", password, server) else: sendImageEmail(SUBJECT, BODY, recipient, user, - "Jasper ", password, server, imageFile) + "Jasper ", password, server, imageFile) return True except: From c6707b0e9d5417dc5938cd0c14fdd33c99180df3 Mon Sep 17 00:00:00 2001 From: Wesley Thompson Date: Tue, 6 Jun 2017 19:42:12 -0500 Subject: [PATCH 5/5] Add format fixes after running actual flake8 command --- client/app_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/app_utils.py b/client/app_utils.py index ac31f63e1..f8ddc7f99 100644 --- a/client/app_utils.py +++ b/client/app_utils.py @@ -106,10 +106,10 @@ def generateSMSEmail(profile): server = 'smtp.gmail.com' if imageFile is None: sendEmail(SUBJECT, BODY, recipient, user, - "Jasper ", password, server) + "Jasper ", password, server) else: sendImageEmail(SUBJECT, BODY, recipient, user, - "Jasper ", password, server, imageFile) + "Jasper ", password, server, imageFile) return True except: