From ea5b8fc0572137653906364329f359e95ab5401e Mon Sep 17 00:00:00 2001 From: gau1991 Date: Mon, 13 Apr 2015 17:46:01 +0530 Subject: [PATCH] Added sendmail.py --- ee/core/sendmail.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ee/core/sendmail.py diff --git a/ee/core/sendmail.py b/ee/core/sendmail.py new file mode 100644 index 00000000..21d6cadf --- /dev/null +++ b/ee/core/sendmail.py @@ -0,0 +1,33 @@ +import smtplib +import os +from email.mime.multipart import MIMEMultipart +from email.mime.base import MIMEBase +from email.mime.text import MIMEText +from email.utils import COMMASPACE, formatdate +from email import encoders + + +def EESendMail(send_from, send_to, subject, text, files, server="localhost", + port=587, username='', password='', isTls=True): + msg = MIMEMultipart() + msg['From'] = send_from + msg['To'] = send_to + msg['Date'] = formatdate(localtime=True) + msg['Subject'] = subject + + msg.attach(MIMEText(text)) + + for f in files: + part = MIMEBase('application', "octet-stream") + part.set_payload(open(f, "rb").read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', 'attachment; filename="{0}"' + .format(os.path.basename(f))) + msg.attach(part) + + smtp = smtplib.SMTP(server, port) + if isTls: + smtp.starttls() + + smtp.sendmail(send_from, send_to, msg.as_string()) + smtp.quit()