Skip to content

Commit 1ef4057

Browse files
author
Mohamed ElKalioby
committed
Moved to Django Emailer
1 parent 0fb3955 commit 1ef4057

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

webapp/autoDeploy/autoDeploy/settings.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@
120120

121121
LOGIN_URL=BASE_URL+"accounts/login"
122122

123-
SMTP= {
124-
'HOST': 'smtp.gmail.com',
125-
'PORT': '587',
126-
'USERNAME': '',
127-
'PASSWORD': '',
128-
'FROM':'Auto Deploy'
129-
130-
}
123+
EMAIL_HOST= 'smtp.gmail.com'
124+
EMAIL_PORT= 587
125+
EMAIL_HOST_USER= ''
126+
EMAIL_HOST_PASSWORD=''
127+
EMAIL_USE_TLS=True
128+
EMAIL_FROM="AutoDeploy"
Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,17 @@
11
__author__ = 'mohamed'
22

33
def send(to,subject,body,fromUser=None,cc="",bcc="",):
4-
54
from django.conf import settings
6-
import smtplib
7-
from email.mime.multipart import MIMEMultipart
8-
from email.mime.text import MIMEText
9-
From=fromUser
10-
if fromUser==None:
11-
From="%s<%s>"%(settings.SMTP["FROM"],settings.SMTP["USERNAME"])
12-
try:
13-
msg = MIMEMultipart('alternative')
14-
msg['Subject'] = subject
15-
msg['From'] = From
16-
#msg['To'] = to one email
17-
msg['Cc']= cc
18-
if type(to)==type([]):
19-
msg['To'] = ', '.join( to) #More than one email
20-
else:
21-
msg['To'] = to
22-
html = body
23-
part2 = MIMEText(html, 'html')
24-
msg.attach(part2)
25-
server = smtplib.SMTP(settings.SMTP["HOST"],settings.SMTP["PORT"])
26-
server.ehlo()
27-
server.starttls()
28-
server.login(settings.SMTP["USERNAME"], settings.SMTP["PASSWORD"])
29-
for t in to.split(";"):
30-
server.sendmail(From, t, msg.as_string())
31-
if cc!="":
32-
server.sendmail(From, cc, msg.as_string())
33-
if bcc!="":
34-
server.sendmail(From, bcc, msg.as_string())
35-
server.quit()
36-
return True
37-
38-
except Exception as exp:
39-
return False
5+
from django.core.mail import EmailMessage
6+
From = "%s<%s>" % (fromUser, settings.EMAIL_HOST_USER)
7+
if fromUser == None:
8+
From = "%s<%s>" % (settings.EMAIL_FROM, settings.EMAIL_HOST_USER)
9+
if type(to) != type([]):
10+
to = [to]
11+
email = EmailMessage(
12+
subject,
13+
body,
14+
From,
15+
to, cc=cc, bcc=bcc)
16+
email.content_subtype = "html"
17+
return email.send(True)

0 commit comments

Comments
 (0)