smtpd-redirectd (1283B)
1 #!/usr/bin/env python3 2 # coding=utf-8 3 # 4 # (c) 2019-2022 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 import smtpd 13 import asyncore 14 import subprocess 15 16 class SMTPRedirectd(smtpd.SMTPServer): 17 sendcmd = None 18 def process_message(self, peer, mailfrom, rcpttos, data): 19 sendproc = subprocess.Popen(sendcmd, shell=True, \ 20 stdin=subprocess.PIPE, close_fds=True) 21 sendproc.stdin.write(data.encode()) 22 sendproc.stdin.close() 23 sendproc.wait() 24 25 def usage(app): 26 app = os.path.basename(app) 27 print("usage: %s [-hd] [sendcmd ...]" % (app), file=sys.stderr) 28 sys.exit(1) 29 30 def main(args): 31 try: 32 opts, largs = getopt.getopt(args[1:], "h") 33 except getopt.GetoptError as err: 34 print(str(err)) 35 usage(args[0]) 36 37 sendcmd = "sudo -u praxis msmtp -t -C /home/praxis/.msmtprc" 38 dodebug = 0 39 for o, a in opts: 40 if o == "-h": 41 usage(args[0]) 42 elif o == "-d": 43 dodebug = 1 44 else: 45 assert False, "unhandled option" 46 47 if len(largs) > 0: 48 sendcmd = " ".join(largs) 49 50 if dodebug == 1: 51 server = smtpd.DebuggingServer(('127.0.0.1', 25), None) 52 else: 53 server = SMTPRedirectd(('127.0.0.1', 25), None) 54 server.sendcmd = sendcmd 55 asyncore.loop() 56 57 return 0 58 59 if __name__ == "__main__": 60 sys.exit(main(sys.argv)) 61