-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mohamed ElKalioby
committed
Sep 18, 2017
1 parent
0fb3955
commit 1ef4057
Showing
2 changed files
with
19 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -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) |