[Twisted-Python] smtp.SMTP help

Greg greg at digitalinfo.net
Wed Oct 1 17:20:17 EDT 2003

Working on an SMTP filter (consisting of an SMTP server that receives 
messages, some code that mangles them, then and SMTP client that forwards 
them along). The first step is to get a functioning SMTP server working, and 
I'm having some trouble with SMTPFactory / SMTP. I've overloaded the validate 
methods in an attempt accept emails to and from any domain, but I can't 
figure out how to hook in a class that implements IMessageDelivery:

class MySMTPFactory(SMTPFactory):
	def buildProtocol(self, addr):
		self.protocol = MySMTPProtocol
		return SMTPFactory.buildProtocol(self, addr)

class MySMTPProtocol(SMTP):
	def validateFrom(self, helo, origin):
		return origin
	def validateTo(self, user):
		d = defer.Deferred()
		reactor.callLater(0, self.fakeSucceed())
		return d
	def fakeSucceed(self):
		return MyIMessage
class MyIMessage:
	__implements__ = (smtp.IMessageDelivery)
	def __init__(self):
		print "MyIMessage.__init__ got called...."
	def validateTo(self, user):
		print "MyIMessage.validateTo got called..."
		return user

	def validateFrom(self, helo, origin):
		print "MyIMessage.validateFrom got called..."
		return origin

	def receivedHeader(self, helo, origin, recipients):
		print "MyIMessage.receivedHeader got called..."
		return None

I gleaned most of this from looking at the testing code. The SMTP server 
reports "503 Must have valid receiver and originator" after the client issues 
the DATA command, and none of the methods in MyIMessage (except for __INIT__) 
ever get called. Any tips as to what I'm doing wrong? Thanks. 

More information about the Twisted-Python mailing list