[Zope3-checkins] SVN: Zope3/trunk/src/zwiki/ WkikMailer now sends
mail encoded in UTF-8
Dmitry Vasiliev
dima at hlabs.spb.ru
Wed Nov 9 08:26:19 EST 2005
Log message for revision 40000:
WkikMailer now sends mail encoded in UTF-8
Changed:
U Zope3/trunk/src/zwiki/tests/test_wikimail.py
U Zope3/trunk/src/zwiki/wikipage.py
-=-
Modified: Zope3/trunk/src/zwiki/tests/test_wikimail.py
===================================================================
--- Zope3/trunk/src/zwiki/tests/test_wikimail.py 2005-11-09 04:10:08 UTC (rev 39999)
+++ Zope3/trunk/src/zwiki/tests/test_wikimail.py 2005-11-09 13:26:18 UTC (rev 40000)
@@ -15,6 +15,9 @@
$Id$
"""
+import email
+from email.Header import decode_header
+
import unittest
from zope.event import subscribers
@@ -37,6 +40,7 @@
mail_result=[]
+
class MailSubscriptionTest(PlacelessSetup):
def getTestObject(self):
@@ -86,8 +90,8 @@
self._sub.removeSubscriptions(('blah at bar.com', 'doh at bar.com'))
self.assertEqual((),
self._sub.context.__annotations__[SubscriberKey])
-
+
class WikiPageMailSubscriptionTest(MailSubscriptionTest, unittest.TestCase):
def getTestObject(self):
@@ -101,7 +105,7 @@
class MailDeliveryStub(object):
-
+
implements(IMailDelivery)
def send(self, fromaddr, toaddrs, message):
@@ -156,23 +160,31 @@
self.assertEqual('wiki at zope3.org',
mail_result[-1][0])
self.assertEqual(('blah at bar.com', 'foo at bar.com'), mail_result[-1][1])
- self.assertEqual(
- u'''Subject: Modified: page1\n\n\n\n??changed:\n'''
- u'''-\n'''
- u'''+Hello World!\n''',
- mail_result[-1][2])
+
+ message = email.message_from_string(mail_result[-1][2])
+ self.assert_(len(message) >= 3)
+ self.assertEqual(decode_header(message["Subject"])[0][0],
+ "Modified: page1")
+ self.assertEqual(message["From"], "wiki at zope3.org")
+ self.assertEqual(message["To"], "wiki at zope3.org")
+ self.assertEqual(message.get_payload(decode=True),
+ "\n??changed:\n-\n+Hello World!\n")
+
page.source = 'Hello New World!'
self.assertEqual('wiki at zope3.org',
mail_result[-1][0])
self.assertEqual(('blah at bar.com', 'foo at bar.com'), mail_result[-1][1])
- self.assertEqual(
- u'''Subject: Modified: page1\n\n\n\n??changed:\n'''
- u'''-Hello World!\n'''
- u'''+Hello New World!\n''',
- mail_result[-1][2])
-
+ message = email.message_from_string(mail_result[-1][2])
+ self.assert_(len(message) >= 3)
+ self.assertEqual(decode_header(message["Subject"])[0][0],
+ "Modified: page1")
+ self.assertEqual(message["From"], "wiki at zope3.org")
+ self.assertEqual(message["To"], "wiki at zope3.org")
+ self.assertEqual(message.get_payload(decode=True),
+ "\n??changed:\n-Hello World!\n+Hello New World!\n")
+
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(WikiMailSubscriptionTest),
Modified: Zope3/trunk/src/zwiki/wikipage.py
===================================================================
--- Zope3/trunk/src/zwiki/wikipage.py 2005-11-09 04:10:08 UTC (rev 39999)
+++ Zope3/trunk/src/zwiki/wikipage.py 2005-11-09 13:26:18 UTC (rev 40000)
@@ -17,6 +17,9 @@
"""
__docformat__ = 'restructuredtext'
+from email.MIMEText import MIMEText
+from email.Header import Header
+
from persistent import Persistent
from zope.interface import implements
@@ -284,7 +287,7 @@
vocab = getVocabularyRegistry().get(self.context, 'SourceTypes')
if type in [term.value for term in vocab]:
self.context.type = unicode(type)
-
+
self.context.source = unicode(data)
@@ -317,7 +320,7 @@
def getSubscriptions(self):
"See zwiki.interfaces.IMailSubscriptions"
return self._annotations[SubscriberKey]
-
+
def addSubscriptions(self, emails):
"See zwiki.interfaces.IMailSubscriptions"
subscribers = list(self._annotations[SubscriberKey])
@@ -325,7 +328,7 @@
if email not in subscribers:
subscribers.append(email.strip())
self._annotations[SubscriberKey] = tuple(subscribers)
-
+
def removeSubscriptions(self, emails):
"See zwiki.interfaces.IMailSubscriptions"
subscribers = list(self._annotations[SubscriberKey])
@@ -333,12 +336,14 @@
if email in subscribers:
subscribers.remove(email)
self._annotations[SubscriberKey] = tuple(subscribers)
-
-class WikiMailer:
+class WikiMailer(object):
"""Class to handle all outgoing mail."""
+ fromaddr = "wiki at zope3.org"
+ encoding = "utf-8"
+
def __call__(self, event):
if IWikiPage.providedBy(event.object):
if IObjectAddedEvent.providedBy(event):
@@ -354,7 +359,7 @@
subject = 'Added: '+zapi.name(object)
emails = self.getAllSubscribers(object)
body = object.source
- self.mail(emails, subject, body)
+ self.mail(emails, subject, body)
def handleModified(self, event):
object = event.object
@@ -382,10 +387,17 @@
"""Mail out the Wiki change message."""
if not emails:
return
- msg = 'Subject: %s\n\n\n%s' %(subject, body)
+ msg = self._getMessage(subject, body)
mail_delivery = zapi.getUtility(IMailDelivery,
'wiki-delivery')
- mail_delivery.send('wiki at zope3.org' , emails, msg)
+ mail_delivery.send(self.fromaddr, emails, msg)
+ def _getMessage(self, subject, body):
+ message = MIMEText(body.encode("utf-8"), "plain", self.encoding)
+ message["Subject"] = Header(subject.encode("utf-8"), self.encoding)
+ message["From"] = self.fromaddr
+ message["To"] = self.fromaddr
+ return message.as_string()
+
# Create a global mailer object.
mailer = WikiMailer()
More information about the Zope3-Checkins
mailing list