Skip to content

Instantly share code, notes, and snippets.

@clarkbw
Created March 6, 2011 17:53
Show Gist options
  • Save clarkbw/857447 to your computer and use it in GitHub Desktop.
Save clarkbw/857447 to your computer and use it in GitHub Desktop.
A python LMTP server using the smtpd module
#!/bin/env python
from smtpd import SMTPChannel, SMTPServer
import asyncore
class LMTPChannel(SMTPChannel):
# LMTP "LHLO" command is routed to the SMTP/ESMTP command
def smtp_LHLO(self, arg):
self.smtp_HELO(arg)
class LMTPServer(SMTPServer):
def __init__(self, localaddr, remoteaddr):
SMTPServer.__init__(self, localaddr, remoteaddr)
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
print 'Message :', data
return
def handle_accept(self):
conn, addr = self.accept()
channel = LMTPChannel(self, conn, addr)
server = LMTPServer(('localhost', 10025), None)
asyncore.loop()
@wiml
Copy link

wiml commented Jul 19, 2011

nice and compact. doesn't this fall over when there's more than one recipient, though? rfc2033 4.2 says that DATA should return multiple responses, one for each RCPT that was accepted earlier.

@clarkbw
Copy link
Author

clarkbw commented Jul 19, 2011

the rcpttos is actually a list which you can iterate through like so:

for user in rcpttos:
  [username, domain] = user.split("@")

But otherwise everything, like data, is a singleton which could be delivered to each user in the rcpttos

@wiml
Copy link

wiml commented Jul 19, 2011

No, I mean the responses sent back to the LMTP client. smtpd.py's process_message() doesn't have a straightforward way to return multiple responses.

@clarkbw
Copy link
Author

clarkbw commented Jul 19, 2011

Oh, for this you would end up needing to return specific text codes, like : "550 No such user here" or "550 mailbox not found".

The spec also claims that you could return a 550 code with a list of recipients that failed. However I'm not sure that is actually supported so you'd have to test that out.

See page 50 in the spec for all the details:
http://www.ietf.org/rfc/rfc2821.txt

But to reject a specific recipient you are supposed to be hooked into the RCPT channel, something like here. (most of the method is copied from the SMTPChannel source code)

class LMTPChannel(SMTPChannel):
    def smtp_RCPT(self, arg):
        print >> DEBUGSTREAM, '===> RCPT', arg
        if not self.__mailfrom:
            self.push('503 Error: need MAIL command')
            return
        address = self.__getaddr('TO:', arg) if arg else None
        if not address:
            self.push('501 Syntax: RCPT TO: <address>')
            return
        ### Check for a valid mailbox
        if not address in VALID_ADDRESSES:
            self.push("550 No such user here")
            return
        self.__rcpttos.append(address)
        print >> DEBUGSTREAM, 'recips:', self.__rcpttos
        self.push('250 Ok')

@wiml
Copy link

wiml commented Jul 19, 2011

No, this is one of the ways LMTP differs from SMTP. rfc2033 4.2 says that DATA should return multiple responses, one for each RCPT that was accepted earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment