A simple SMTP server (in Python)

前端 未结 9 2606
小鲜肉
小鲜肉 2020-12-12 17:09

Could you please suggest a simple SMTP server with the very basic APIs (by very basic I mean, to read, write, delete email), that could be run on a linux box? I just need to

9条回答
  •  抹茶落季
    2020-12-12 18:11

    To get Hasen's script working in Python 3 I had to tweak it slightly:

    from datetime import datetime
    import asyncore
    from smtpd import SMTPServer
    
    class EmlServer(SMTPServer):
        no = 0
        def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
            filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),
                self.no)
            print(filename)
            f = open(filename, 'wb')
            f.write(data)
            f.close
            print('%s saved.' % filename)
            self.no += 1
    
    def run():
        EmlServer(('localhost', 25), None)
        try:
            asyncore.loop()
        except KeyboardInterrupt:
            pass
    
    if __name__ == '__main__':
        run()
    

提交回复
热议问题