From b3c7b9273c47913f0813a0ab692de856c766a3bc Mon Sep 17 00:00:00 2001 From: Hasan Wan Date: Tue, 31 Jul 2012 21:41:24 +0800 Subject: [PATCH] add simple email sender Change-Id: I480e0f499f1a60ae5d106b4c19aaaa47d19a1d5c Signed-off-by: Hasan Wan --- send_mail.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 send_mail.py diff --git a/send_mail.py b/send_mail.py new file mode 100644 index 0000000..5a5852e --- /dev/null +++ b/send_mail.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import os +import smtplib + +from email.mime.text import MIMEText +from email.MIMEMultipart import MIMEMultipart +from email.MIMEBase import MIMEBase +from email import Encoders + +SMTP_SERVER = "sh-out.intel.com" + +def sendmail(subject = '', body = '', from_email = None, to = None, + bcc = None, attachment = None): + + if not ( to or bcc ): + return -1 + + if type(to) != list: + to = [to] + if type(bcc) != list: + bcc = [bcc] + + message = MIMEMultipart() + + message['Subject'] = subject + message['From'] = from_email + message['To'] = ','.join(to) + message.attach(MIMEText(body)) + + if attachment and os.path.isfile(attachment): + attach = MIMEBase('application',"octet-stream") + attach.set_payload(open(attachment, "rb").read()) + Encoders.encode_base64(attach) + attach.add_header("Content-Disposition", 'attachment; filename="%s"' %(os.path.basename(attachment))) + message.attach(attach) + + s = smtplib.SMTP(SMTP_SERVER) + s.sendmail(from_email, to, message.as_string()) + s.quit() -- 2.7.4