Creating Python Email (receiving) server

2019-03-15 15:57发布

问题:

I am trying to produce a simple python script for a Linux VPS that will allow me to receive mail, (and then I can do stuff to it in python, like print it to stdout). Nothing more complex than that.

I don't want to use a 'heavy' solution or server program, I am really just after a simple python script that I can run, and is capable of receiving mail.

Will Pythons' smtpd module suffice for this task? I have heard conflicting opinions thus far. If not, what else would you suggest? Perhaps you have hacked together some code yourself?

At this stage, even projects like lamson seem too heavy (though this may be unavoidable if I cannot find a better solution).

回答1:

Pythons smtpd is sufficient.

You might also want to take a look at inbox.py and this example



回答2:

Yes SMTPD Module will be help full. Example code is here:

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

    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)
        return

server = CustomSMTPServer(('127.0.0.1', 1025), None)
asyncore.loop()