[CalendarServer-changes] [2820] CalendarServer/branches/users/sagen/mailgateway-implicit-2745/ twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Wed Aug 13 14:57:00 PDT 2008


Revision: 2820
          http://trac.macosforge.org/projects/calendarserver/changeset/2820
Author:   sagen at apple.com
Date:     2008-08-13 14:56:59 -0700 (Wed, 13 Aug 2008)
Log Message:
-----------
Support HTML mail with embedded image, plus non-ascii content

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/mail.py

Added Paths:
-----------
    CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/images/
    CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/images/mail/
    CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/images/mail/icalserver.png

Added: CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/images/mail/icalserver.png
===================================================================
(Binary files differ)


Property changes on: CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/images/mail/icalserver.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/mail.py
===================================================================
--- CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/mail.py	2008-08-13 19:49:20 UTC (rev 2819)
+++ CalendarServer/branches/users/sagen/mailgateway-implicit-2745/twistedcaldav/mail.py	2008-08-13 21:56:59 UTC (rev 2820)
@@ -38,12 +38,13 @@
 from twistedcaldav.ical import Property
 from zope.interface import Interface, implements
 import email, email.utils
+from email.mime.multipart import MIMEMultipart
+from email.mime.image import MIMEImage
+from email.mime.text import MIMEText
 import uuid
 import os
-import cStringIO
 import datetime
 import base64
-import MimeWriter
 
 __all__ = [
     "IMIPInboxResource",
@@ -662,45 +663,58 @@
 
     def _generateTemplateMessage(self, calendar, organizer):
 
-        caldata = str(calendar)
         title, summary = self._generateCalendarSummary(calendar, organizer)
 
-        data = cStringIO.StringIO()
-        writer = MimeWriter.MimeWriter(data)
-
-        writer.addheader("From", "${fromaddress}")
-        writer.addheader("Reply-To", "${replytoaddress}")
-        writer.addheader("To", "${toaddress}")
-        writer.addheader("Date", rfc822date())
-        writer.addheader("Subject", "Event invitation: %s" % (title,))
+        msg = MIMEMultipart()
+        msg["From"] = "${fromaddress}"
+        msg["Reply-To"] = "${replytoaddress}"
+        msg["To"] = "${toaddress}"
+        msg["Date"] = rfc822date()
+        msg["Subject"] = "Event invitation: %s" % (title,)
         msgId = messageid()
-        writer.addheader("Message-ID", msgId)
-        writer.addheader("Mime-Version", "1.0")
-        writer.flushheaders()
+        msg["Message-ID"] = msgId
 
-        writer.startmultipartbody("mixed")
+        msgAlt = MIMEMultipart("alternative")
+        msg.attach(msgAlt)
 
-        # message body
-        part = writer.nextpart()
-        body = part.startbody("text/plain")
-        body.write("You've been invited to the following event:  %s To accept or decline this invitation, click the link below.\n" % (summary,))
+        # plain text version
+        plainText = u"You've been invited to the following event:  %s To accept or decline this invitation, click the link below.\n" % (summary,)
+        msgPlain = MIMEText(plainText.encode("UTF-8"), "plain", "UTF-8")
+        msgAlt.attach(msgPlain)
 
-        part = writer.nextpart()
-        encoding = "7bit"
-        for i in caldata:
-            if ord(i) > 127:
-                encoding = "base64"
-                caldata = base64.encodestring(caldata)
-                break
-        part.addheader("Content-Transfer-Encoding", encoding)
-        body = part.startbody("text/calendar; charset=utf-8")
-        body.write(caldata.replace("\r", ""))
+        # html version
+        msgHtmlRelated = MIMEMultipart("related", type="text/html")
+        msgAlt.attach(msgHtmlRelated)
+        htmlText = u"""
+<html><body><div>
+<img src="cid:icalserver.png">
+%s
+</div></body></html>
+""" % plainText
+        msgHtml = MIMEText(htmlText.encode("UTF-8"), "html", "UTF-8")
+        msgHtmlRelated.attach(msgHtml)
 
-        # finish
-        writer.lastpart()
+        # an image for html version
+        imageName = "icalserver.png"
+        imageFile = open(os.path.join(os.path.dirname(__file__),
+            "images", "mail", imageName))
+        msgImage = MIMEImage(imageFile.read(),
+            _subtype='image/png;x-apple-mail-type=stationery;name="%s"' %
+            (imageName,))
+        imageFile.close()
+        msgImage.add_header("Content-ID", "<%s>" % (imageName,))
+        msgImage.add_header("Content-Disposition", "inline;filename=%s" %
+            (imageName,))
+        msgHtmlRelated.attach(msgImage)
 
-        return msgId, data.getvalue()
+        # the icalendar attachment
+        msgIcal = MIMEText(str(calendar), "calendar", "UTF-8")
+        msgIcal.add_header("Content-Disposition",
+            "attachment;filename=invitation.ics")
+        msg.attach(msgIcal)
 
+        return msgId, msg.as_string()
+
     def _generateCalendarSummary(self, calendar, organizer):
 
         # Get the most appropriate component
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20080813/d3929899/attachment.html 


More information about the calendarserver-changes mailing list