diff src/plugins/plugin_misc_smtp.py @ 370:68cdaf6d78e3

plugin smtp: fixed email address parsing and invalid unicode message
author Goffi <goffi@goffi.org>
date Fri, 24 Jun 2011 17:45:22 +0200
parents 0ecd9c33fa3a
children 2a072735e459
line wrap: on
line diff
--- a/src/plugins/plugin_misc_smtp.py	Sun Jun 19 17:40:36 2011 +0200
+++ b/src/plugins/plugin_misc_smtp.py	Fri Jun 24 17:45:22 2011 +0200
@@ -28,10 +28,11 @@
 from twisted.mail import smtp
 from twisted.python import failure
 from email.parser import Parser
+from email.utils import parseaddr
 from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
 import os,os.path
 from twisted.internet import reactor
-import pdb
+import sys
 
 
 from zope.interface import implements
@@ -88,8 +89,13 @@
     def eomReceived(self):
         """handle end of message"""
         mail = Parser().parsestr("\n".join(self.message))
-        self.host.sendMessage(mail['to'].decode('utf-8'), mail.get_payload().decode('utf-8'), 
-                              subject=mail['subject'].decode('utf-8'), type='normal', profile_key=self.profile)
+        try:
+            self.host.sendMessage(parseaddr(mail['to'].decode('utf-8','replace'))[1], mail.get_payload().decode('utf-8','replace'), #TODO: manage other charsets
+                                  subject=mail['subject'].decode('utf-8','replace'), type='normal', profile_key=self.profile)
+        except:
+            exc_type, exc_value, exc_traceback = sys.exc_info()
+            error(_("Can't send message: %s") % exc_value) #The email is invalid or incorreclty parsed
+            return defer.fail()
         self.message=None
         return defer.succeed(None)