From 1ef405748ba4045458eca3a8561b33fa6a93cd00 Mon Sep 17 00:00:00 2001 From: Mohamed ElKalioby Date: Mon, 18 Sep 2017 15:53:21 +0300 Subject: [PATCH] Moved to Django Emailer --- webapp/autoDeploy/autoDeploy/settings.py | 14 +++---- webapp/autoDeploy/autodeploy/Common.py | 48 +++++++----------------- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/webapp/autoDeploy/autoDeploy/settings.py b/webapp/autoDeploy/autoDeploy/settings.py index 805baf0..75844ff 100644 --- a/webapp/autoDeploy/autoDeploy/settings.py +++ b/webapp/autoDeploy/autoDeploy/settings.py @@ -120,11 +120,9 @@ LOGIN_URL=BASE_URL+"accounts/login" -SMTP= { - 'HOST': 'smtp.gmail.com', - 'PORT': '587', - 'USERNAME': '', - 'PASSWORD': '', - 'FROM':'Auto Deploy' - - } \ No newline at end of file +EMAIL_HOST= 'smtp.gmail.com' +EMAIL_PORT= 587 +EMAIL_HOST_USER= '' +EMAIL_HOST_PASSWORD='' +EMAIL_USE_TLS=True +EMAIL_FROM="AutoDeploy" diff --git a/webapp/autoDeploy/autodeploy/Common.py b/webapp/autoDeploy/autodeploy/Common.py index 8ce1333..2cdb82b 100644 --- a/webapp/autoDeploy/autodeploy/Common.py +++ b/webapp/autoDeploy/autodeploy/Common.py @@ -1,39 +1,17 @@ __author__ = 'mohamed' def send(to,subject,body,fromUser=None,cc="",bcc="",): - from django.conf import settings - import smtplib - from email.mime.multipart import MIMEMultipart - from email.mime.text import MIMEText - From=fromUser - if fromUser==None: - From="%s<%s>"%(settings.SMTP["FROM"],settings.SMTP["USERNAME"]) - try: - msg = MIMEMultipart('alternative') - msg['Subject'] = subject - msg['From'] = From - #msg['To'] = to one email - msg['Cc']= cc - if type(to)==type([]): - msg['To'] = ', '.join( to) #More than one email - else: - msg['To'] = to - html = body - part2 = MIMEText(html, 'html') - msg.attach(part2) - server = smtplib.SMTP(settings.SMTP["HOST"],settings.SMTP["PORT"]) - server.ehlo() - server.starttls() - server.login(settings.SMTP["USERNAME"], settings.SMTP["PASSWORD"]) - for t in to.split(";"): - server.sendmail(From, t, msg.as_string()) - if cc!="": - server.sendmail(From, cc, msg.as_string()) - if bcc!="": - server.sendmail(From, bcc, msg.as_string()) - server.quit() - return True - - except Exception as exp: - return False + from django.core.mail import EmailMessage + From = "%s<%s>" % (fromUser, settings.EMAIL_HOST_USER) + if fromUser == None: + From = "%s<%s>" % (settings.EMAIL_FROM, settings.EMAIL_HOST_USER) + if type(to) != type([]): + to = [to] + email = EmailMessage( + subject, + body, + From, + to, cc=cc, bcc=bcc) + email.content_subtype = "html" + return email.send(True)