[Twisted-Python] smtp.SMTP help

Jp Calderone exarkun at intarweb.us
Fri Oct 3 12:43:50 EDT 2003


On Fri, Oct 03, 2003 at 07:45:00AM -0400, Greg wrote:
> Thanks! In order to help others like me who were struggling with this, 
> here's the working code. Please let me know if I did anything 
> egregiously stupid here.
> 
> #!/usr/bin/env python
> # Simple SMTP server shell
> 
> from twisted.internet import reactor, protocol, defer
> from twisted.protocols import smtp
> 
> class MyMessage:
>    __implements__ = (smtp.IMessage)
>       
>    def __init__(self):
>        self.msg = []
> 
>    def lineReceived(self, line):
>        self.msg.append(line)
> 
>    def eomReceived(self):
>        # Handle message here
>        for line in self.msg:
>            print line
>        self.msg = []
>        return defer.succeed('<Whatever>')
>   
> class MyMessageDelivery:
>    __implements__ = (smtp.IMessageDelivery)
>   
>    def validateFrom(self, helo, origin):
>        return origin
>       
>    def validateTo(self, user):
>        return MyMessage

  You need to return an *instance* here.  You probably also want to
associate the user with the instance, too, since you won't be told again who
you're delivering for.  Changing MyMessage.__init__ to take a user argument
and returning "MyMessage(user)" might be a good way to go.

  The validateTo and validateFrom methods are your chance to refuse this
message.  "helo" and "origin" identify the source of the message.  "user"
identifies the destination.  If any of them are not to your liking, you
should raise smtp.SMTPBadRcpt or smtp.SMTPBadSender.

>       
>    def receivedHeader(self, helo, origin, recipients):
>        return None
> 
> class MySMTPFactory(smtp.SMTPFactory):
>    def buildProtocol(self, addr):
>        p = self.protocol(MyMessageDelivery())
>        p.factory = self
>        p.portal = self.portal
>        p.host = "somedomain.com"
>        return p   
>       
> factory = MySMTPFactory()
> factory.protocol = smtp.SMTP
> reactor.listenTCP(25, factory)
> reactor.run()
> 

  Jp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031003/7af48339/attachment.pgp 


More information about the Twisted-Python mailing list