Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple SMTP server (in Python)

Tags:

python

smtp

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 convert the crux of the email into XML format and FTP it to another machine.

like image 400
fixxxer Avatar asked Apr 22 '10 13:04

fixxxer


People also ask

What is SMTP server Python?

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

How can you use Python built-in mail server?

Python comes with the built-in smtplib module for sending emails using the Simple Mail Transfer Protocol (SMTP). smtplib uses the RFC 821 protocol for SMTP. The examples in this tutorial will use the Gmail SMTP server to send emails, but the same principles apply to other email services.


1 Answers

Take a look at this SMTP sink server:

from __future__ import print_function from datetime import datetime import asyncore from smtpd import SMTPServer  class EmlServer(SMTPServer):     no = 0     def process_message(self, peer, mailfrom, rcpttos, data):         filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),                 self.no)         f = open(filename, 'w')         f.write(data)         f.close         print('%s saved.' % filename)         self.no += 1   def run():     # start the smtp server on localhost:1025     foo = EmlServer(('localhost', 1025), None)     try:         asyncore.loop()     except KeyboardInterrupt:         pass   if __name__ == '__main__':     run() 

It uses smtpd.SMTPServer to dump emails to files.

like image 161
hasen Avatar answered Sep 18 '22 10:09

hasen