smtpd-redirectd (1267B)
1 #!/usr/bin/env python3 2 # coding=utf-8 3 # 4 # (c) 2019-2026 Christoph Lohmann <20h@r-36.net> 5 # 6 # This file is published under the terms of the GPLv3. 7 # 8 9 import os 10 import sys 11 import getopt 12 from aiosmtpd.controller import Controller 13 import asyncore 14 import time 15 import subprocess 16 17 class SMTPHandler: 18 async def handle_DATA(self, server, session, envelope): 19 peer = session.peer 20 mail_from = envelope.mail_from 21 rcpt_tos = envelope.rcpt_tos 22 data = envelope.content 23 24 msmtpcmd = "sudo -u praxis msmtp -t -C /home/praxis/.msmtprc" 25 msmtpproc = subprocess.Popen(msmtpcmd, shell=True, \ 26 stdin=subprocess.PIPE, close_fds=True) 27 msmtpproc.stdin.write(data) 28 msmtpproc.stdin.close() 29 msmtpproc.wait() 30 31 return "250 OK" 32 33 def usage(app): 34 app = os.path.basename(app) 35 print("usage: %s [-h]" % (app), file=sys.stderr) 36 sys.exit(1) 37 38 def main(args): 39 try: 40 opts, largs = getopt.getopt(args[1:], "h") 41 except getopt.GetoptError as err: 42 print(str(err)) 43 usage(args[0]) 44 45 for o, a in opts: 46 if o == "-h": 47 usage(args[0]) 48 else: 49 assert False, "unhandled option" 50 51 handler = SMTPHandler() 52 controller = Controller(handler, hostname="127.0.0.1", port=25) 53 controller.start() 54 while 1: 55 time.sleep(10) 56 57 return 0 58 59 if __name__ == "__main__": 60 sys.exit(main(sys.argv)) 61