Skip to content
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

Add ability to send emails that have image attachments #622

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions client/app_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,7 +31,35 @@ def sendEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER):
session.quit()


def emailUser(profile, SUBJECT="", BODY=""):
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
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.

Expand Down Expand Up @@ -73,8 +104,12 @@ def generateSMSEmail(profile):
user = profile['gmail_address']
password = profile['gmail_password']
server = 'smtp.gmail.com'
sendEmail(SUBJECT, BODY, recipient, user,
"Jasper <jasper>", password, server)
if imageFile is None:
sendEmail(SUBJECT, BODY, recipient, user,
"Jasper <jasper>", password, server)
else:
sendImageEmail(SUBJECT, BODY, recipient, user,
"Jasper <jasper>", password, server, imageFile)

return True
except:
Expand Down