comparison 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
comparison
equal deleted inserted replaced
369:e83d0c21d64d 370:68cdaf6d78e3
26 from twisted.cred import portal,checkers,credentials 26 from twisted.cred import portal,checkers,credentials
27 from twisted.cred import error as cred_error 27 from twisted.cred import error as cred_error
28 from twisted.mail import smtp 28 from twisted.mail import smtp
29 from twisted.python import failure 29 from twisted.python import failure
30 from email.parser import Parser 30 from email.parser import Parser
31 from email.utils import parseaddr
31 from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials 32 from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
32 import os,os.path 33 import os,os.path
33 from twisted.internet import reactor 34 from twisted.internet import reactor
34 import pdb 35 import sys
35 36
36 37
37 from zope.interface import implements 38 from zope.interface import implements
38 39
39 40
86 self.message.append(line) 87 self.message.append(line)
87 88
88 def eomReceived(self): 89 def eomReceived(self):
89 """handle end of message""" 90 """handle end of message"""
90 mail = Parser().parsestr("\n".join(self.message)) 91 mail = Parser().parsestr("\n".join(self.message))
91 self.host.sendMessage(mail['to'].decode('utf-8'), mail.get_payload().decode('utf-8'), 92 try:
92 subject=mail['subject'].decode('utf-8'), type='normal', profile_key=self.profile) 93 self.host.sendMessage(parseaddr(mail['to'].decode('utf-8','replace'))[1], mail.get_payload().decode('utf-8','replace'), #TODO: manage other charsets
94 subject=mail['subject'].decode('utf-8','replace'), type='normal', profile_key=self.profile)
95 except:
96 exc_type, exc_value, exc_traceback = sys.exc_info()
97 error(_("Can't send message: %s") % exc_value) #The email is invalid or incorreclty parsed
98 return defer.fail()
93 self.message=None 99 self.message=None
94 return defer.succeed(None) 100 return defer.succeed(None)
95 101
96 def connectionLost(self): 102 def connectionLost(self):
97 """handle message truncated""" 103 """handle message truncated"""