changeset 0:e033c476f97a

Origination
author Atul Varma <varmaa@toolness.com>
date Wed, 19 Mar 2008 01:34:52 +0000
parents
children 82844adbddaf
files smtpserver.py
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smtpserver.py	Wed Mar 19 01:34:52 2008 +0000
@@ -0,0 +1,44 @@
+import os
+import sys
+import smtpd
+import smtplib
+import asyncore
+import grp
+import pwd
+
+class MyServer( smtpd.SMTPServer ):
+    import smtpconfig as config
+
+    def __init__( self ):
+        smtpd.SMTPServer.__init__( self, self.config.my_addr, None )
+
+    def bind( self, addr ):
+        retval = smtpd.SMTPServer.bind( self, addr )
+        gid = grp.getgrnam( self.config.gid )[2]
+        uid = pwd.getpwnam( self.config.uid )[2]
+        os.setgid( gid )
+        os.setuid( uid )
+        print "Bound to %s and changed user to %s." % ( addr,
+                                                        self.config.uid )
+        return retval
+
+    def process_message( self, peer, mailfrom, rcpttos, data ):
+        if peer[0] != "127.0.0.1":
+            print "not from localhost: %s" % peer
+            return
+        print "sending message from %s to %s" % (mailfrom, rcpttos)
+        server = smtplib.SMTP( self.config.server, self.config.port )
+        server.set_debuglevel( 1 )
+        server.login( self.config.username, self.config.password )
+        server.sendmail( mailfrom, rcpttos, data )
+        server.quit()
+        print "done."
+
+if __name__ == "__main__":
+    if os.getuid() != 0:
+        print "This program must be run as root."
+        sys.exit( -1 )
+    server = MyServer()
+    print "Listening for incoming connections on port %s." % \
+        server.config.my_addr[1]
+    asyncore.loop()