''' Used by other scripts (eventually) to abstract away e-mail alert related code. ''' import sqlite3, smtplib, os from email import Encoders from email.MIMEBase import MIMEBase from email.MIMEImage import MIMEImage from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate ''' I found the bulk of this function already written on someone's blog for sending mail via outlook 365. I made a few modifications for our purposes. ''' def send_mail(send_from, send_to, subject, text, files=None, data_attachments=None, server="smtp.example.com", port=25, tls=False, html=False, images=None, config_file=None, config=None): if files is None: files = [] if images is None: images = [] if data_attachments is None: data_attachments = [] if config_file is not None: config = ConfigParser.ConfigParser() config.read(config_file) if config is not None: server = config.get('smtp', 'server') port = config.get('smtp', 'port') tls = config.get('smtp', 'tls').lower() in ('true', 'yes', 'y') username = config.get('smtp', 'username') password = config.get('smtp', 'password') msg = MIMEMultipart('related') msg['From'] = send_from msg['To'] = send_to if isinstance(send_to, basestring) else COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach( MIMEText(text, 'html' if html else 'plain') ) 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="%s"' % os.path.basename(f)) msg.attach(part) for f in data_attachments: part = MIMEBase('application', "octet-stream") part.set_payload( f['data'] ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % f['filename']) msg.attach(part) for (n, i) in enumerate(images): fp = open(i, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', ''.format(str(n+1))) msg.attach(msgImage) smtp = smtplib.SMTP(server, int(port)) if tls: smtp.starttls() #Load do not reply account credentials from SQLite DB and authenticate #cursor = sqlite3.connect('D:\\FTP\\ftp.db').execute("SELECT * from accounts where code = 'WMR'") #auth = cursor.fetchone() #smtp.login(auth[1], auth[2]) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() ''' Allows use of html in message. Wraps entire message in font tag that forces a fixed width font for now. I'm not sure that's a great idea or not. ''' def send_alert_html(to, subject, message='', attachments=None, sendFrom='donotreply@example.com'): message = '
' + message + '
' message = message + '


' message = message + 'Please do not reply to this message; it was sent from an unmonitored email address.' send_mail(sendFrom, to, subject, message, files=attachments, html=True) def send_alert(to, subject, message='', attachments=None, sendFrom='donotreply@example.com'): send_mail(sendFrom, to, subject, message, files=attachments) if __name__ == '__main__': #Send a test message if this script is executed test_data = [['1234567890','9876543210'], ['0101379900','0000055555'], ['1000930000','0000052319']] msg = 'Designtex has cut into the following pieces:

' for t in test_data: msg = msg + '' msg = msg + '
Order #Roll #
' + t[0] + '' + t[1] + '
' send_alert_html('admin@example.com', 'Designtex Test', message=msg)