Message library improvements
I’ve been working on improving Hep’s messaging library, fixing the things that have been bugging me about it. The messaging.Message class in particular has seen a lot of improvements. I’m feeling good about it now.
Basically, messaging.Message is just a thin convenience wrapper on top of email.Message (although it doesn’t inherit from it at the moment). This last set of changes was focused on improving the ability to edit existing Message objects. Up until now Hep has created mutipart/alternative messages, and generated the text and HTML versions of a message at the time of generation. But this has some disadvantages: it wastes disk space, and you lose track of the original message format – was it text, with generated HTML, or HTML, with generated text? Also, it wasn’t possible to inject the RSS link URL into the message body as needed, so I had to put it in there at the time of generation, which was less than ideal since it’s really metadata.
Anyhow, now it’s much nicer to work with. Sample code:
>>> import messaging
>>> m = messaging.Message()
>>> m.title = "Hello World"
>>> m.author = "abe@fettig.net"
>>> m.link = "http://www.fettig.net"
>>> m.setHTML("""
... <h1>Test Message</h1>
... This is a message in HTML format.
... """)
>>> print m.asEmail()
Date: Wed, 21 May 2003 03:23:47 -0000
Subject: Hello World
From: abe@fettig.net
MIME-Version: 1.0
Content-Type: text/html
X-Hep-Link: http://www.fettig.net
<h1>Test message</h1>
This is a message in HTML format.
Notice how m.asEmail() gives you a nice simple e-mail message, with a content type of ‘text/html‘.
Now, to get a nice mutipart/alternative version of the message, with a text version for mail clients that
don’t support HTML display, and the link embedded in the message body, all you have to do is:
>>> print m.asEmail(generateMissingParts=1)
Date: Wed, 21 May 2003 03:23:47 -0000
Subject: Hello World
From: abe@fettig.net
X-Hep-Link: http://www.fettig.net
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="===============19186210073222743=="
Message-Id: <md5id:73d197064b90f25bb97b870cd82510d4>
--===============19186210073222743==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Test message
This is a message in HTML format.
http://www.fettig.net
--===============19186210073222743==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<h1>Test message</h1>
This is a message in HTML format.
<p><a href="http://www.fettig.net">http://www.fettig.net</a></p>
--===============19186210073222743==--
If you‘re using Hep from CVS, now would be a good time to do a cvs update. If there are bugs in this code I’d like to know about it.