2 from zope.interface import implements
4 from twisted.application import service
6 application = service.Application("SMTP Server Tutorial")
8 from twisted.application import internet
9 from twisted.internet import protocol, defer
11 smtpServerFactory = protocol.ServerFactory()
13 from twisted.mail import smtp
15 class FileMessage(object):
16 implements(smtp.IMessage)
18 def __init__(self, fileObj):
19 self.fileObj = fileObj
21 def lineReceived(self, line):
22 self.fileObj.write(line + '\n')
24 def eomReceived(self):
26 return defer.succeed(None)
28 def connectionLost(self):
30 os.remove(self.fileObj.name)
32 class TutorialESMTP(smtp.ESMTP):
35 def validateTo(self, user):
36 fileName = 'tutorial-smtp.' + str(self.counter)
38 return lambda: FileMessage(file(fileName, 'w'))
40 def validateFrom(self, helo, origin):
43 def receivedHeader(self, helo, origin, recipients):
44 return 'Received: Tutorially.'
46 class TutorialESMTPFactory(protocol.ServerFactory):
47 protocol = TutorialESMTP
49 def buildProtocol(self, addr):
54 smtpServerFactory.protocol = TutorialESMTP
56 smtpServerService = internet.TCPServer(2025, smtpServerFactory)
57 smtpServerService.setServiceParent(application)