From 2bfc042b3b812ae2206918ad2c03e80141fd5e3e Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Thu, 25 Nov 2010 07:55:49 +0100 Subject: E-mail notifications are now sent with an HTML content and with an alternative plain text version (MIMEMultipart). --- utils.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/utils.py b/utils.py index 2409563e..8a22b2be 100755 --- a/utils.py +++ b/utils.py @@ -44,6 +44,7 @@ except: pass import smtplib +from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import urllib2 @@ -217,20 +218,34 @@ def send_mail(mfrom, mto, feed_title, article_title, description): """ Send the article via mail. """ - content = """\n\n%s\n\n\n%s\n\n""" % \ + # Create the body of the message (a plain-text and an HTML version). + html = """\n\n%s\n\n\n%s\n\n""" % \ (feed_title + ": " + article_title, description) - mail = MIMEText(content) - mail['From'] = mfrom - mail['To'] = mto - mail['Subject'] = '[pyAggr3g470r] - ' + feed_title + ": " + article_title - #email['Text'] = message - - server = smtplib.SMTP(smtp_server) - server.login(username, password) - server.sendmail(mfrom, \ - mto, \ - mail.as_string()) - server.quit() + text = clear_string(description) + + # Create message container - the correct MIME type is multipart/alternative. + msg = MIMEMultipart('alternative') + msg['Subject'] = '[pyAggr3g470r] ' + feed_title + ": " + article_title + msg['From'] = mfrom + msg['To'] = mto + + # Record the MIME types of both parts - text/plain and text/html. + part1 = MIMEText(text, 'plain') + part2 = MIMEText(html, 'html') + + # Attach parts into message container. + # According to RFC 2046, the last part of a multipart message, in this case + # the HTML message, is best and preferred. + msg.attach(part1) + msg.attach(part2) + + # Send the message via local SMTP server. + s = smtplib.SMTP(smtp_server) + s.login(username, password) + # sendmail function takes 3 arguments: sender's address, recipient's address + # and message to send - here it is sent as one string. + s.sendmail(mfrom, mto, msg.as_string()) + s.quit() def string_to_datetime(stringtime): """ -- cgit