Re: First round of comments merging done

Roberto De Almeida <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
This patch transforms the email sent when a comment is posted into a
MIME-message. The message has a HTML version for rich MTA, and a
text-only with HTML tags stripped for text MTA like mutt, pine &
others.

It's a diff against the new version of comments.py.

Roberto


On Thu, 9 Dec 2004 01:49:20 -0800, Ted Leung <[email protected]> wrote:
> Ok,
> 
> So I've merged Will's changes and Bill's changes.  Steven, I tried to
> merge your changes, but patch kept choking on the text version of you
> patch -- could you resend it as an attachment, please?
> 
> The new version is at
> <http://www.sauria.com/~twl/code/python/pyblosxom/comments.py>  (right
> now my DSL is down for system maintenance).
> 
> I commented out Will's changes to the template smashing, but that's
> easy to turn back on.  Other than that I took all the changes.
> 
> I'm going out of town starting Saturday for 4 days, so I'll merge what
> I can as it comes in.
> 
> ----
> Ted Leung                          Blog: <http://www.sauria.com/blog>
> PGP Fingerprint: 1003 7870 251F FA71 A59A  CEE3 BEBA 2B87 F5FC 4B42
> 
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Pyblosxom-devel mailing list
> Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
> 


-- 
Roberto De Almeida <[email protected]>
this email is: [ ] bloggable [ ] ask first [ ] private [x] nonsense
htmlmail.diff (text/x-patch, 2.8 KB)
--- comments.orig	2004-12-09 13:38:19.558189802 +0000
+++ comments.py	2004-12-09 13:46:50.352370923 +0000
@@ -91,6 +91,46 @@
 
     return retval
 
+def createhtmlmail (html, headers):
+    """Create a mime-message that will render HTML in popular
+    MUAs, text in better ones
+
+    Based on: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67083"""
+    import MimeWriter
+    import mimetools
+    import cStringIO
+    
+    out = cStringIO.StringIO() # output buffer for our message 
+    htmlin = cStringIO.StringIO(html)
+
+    text = re.sub('<.*?>', '', html)
+    txtin = cStringIO.StringIO(text)
+    
+    writer = MimeWriter.MimeWriter(out)
+    for header,value in headers:
+        writer.addheader(header, value)
+    writer.addheader("MIME-Version", "1.0")
+    writer.startmultipartbody("alternative")
+    writer.flushheaders()
+
+    subpart = writer.nextpart()
+    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
+    pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
+    mimetools.encode(txtin, pout, 'quoted-printable')
+    txtin.close()
+
+    subpart = writer.nextpart()
+    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
+    pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
+    mimetools.encode(htmlin, pout, 'quoted-printable')
+    htmlin.close()
+
+    writer.lastpart()
+    msg = out.getvalue()
+    out.close()
+
+    return msg
+
 def readComments(entry, config):
     """
     @param: a file entry
@@ -282,16 +322,17 @@
             server = smtplib.SMTP(config['comment_smtp_server'])
             curl = config['base_url']+'/'+entry['file_path']
 
-            message = []
-            message.append("From: %s" % email)
-            message.append("To: %s" % config["comment_smtp_to"])
-            message.append("Date: %s" % formatdate(modTime))
-            message.append("Subject: write back by %s" % author)
-            message.append("")
-            message.append("%s\n%s\n%s\n" % (description, cfn, curl))
+            headers = []
+            headers.append(("From", email))
+            headers.append(("To", config["comment_smtp_to"]))
+            headers.append(("Date", formatdate(modTime)))
+            headers.append(("Subject", "write back by %s" % author))
+
+            html = """%s\n%s\n<a href="%s">%s</a>\n""" % (description, cfn, curl, curl)
+            message = createhtmlmail(html, headers)
             server.sendmail(from_addr=email,
-                            to_addrs=config['comment_smtp_to'], 
-                            msg="\n".join(message))
+                            to_addrs=config['comment_smtp_to'],
+                            msg=message)
             server.quit()
         except:
             tools.log("Error sending mail: %s" % message)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.