smtpd-redirectd

SMTP redirect daemon for malconfigured proprietary email sending software.
git clone git://arztpraxis-lohmann.de/smtpd-redirectd
Log | Files | Refs | LICENSE

commit f963f672152f2de73a0106bf5e4f7ec601342c36
parent ee4c2436402cc224d3886f70f9d063702de0720e
Author: Christoph Lohmann <20h@r-36.net>
Date:   Mon,  2 Mar 2026 15:21:20 +0100

Update to use aiosmtpd for newer python.

Diffstat:
Msmtpd-redirectd | 46+++++++++++++++++++++++-----------------------
1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/smtpd-redirectd b/smtpd-redirectd @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # coding=utf-8 # -# (c) 2019-2022 Christoph Lohmann <20h@r-36.net> +# (c) 2019-2026 Christoph Lohmann <20h@r-36.net> # # This file is published under the terms of the GPLv3. # @@ -9,22 +9,30 @@ import os import sys import getopt -import smtpd +from aiosmtpd.controller import Controller import asyncore +import time import subprocess -class SMTPRedirectd(smtpd.SMTPServer): - sendcmd = None - def process_message(self, peer, mailfrom, rcpttos, data): - sendproc = subprocess.Popen(sendcmd, shell=True, \ +class SMTPHandler: + async def handle_DATA(self, server, session, envelope): + peer = session.peer + mail_from = envelope.mail_from + rcpt_tos = envelope.rcpt_tos + data = envelope.content + + msmtpcmd = "sudo -u praxis msmtp -t -C /home/praxis/.msmtprc" + msmtpproc = subprocess.Popen(msmtpcmd, shell=True, \ stdin=subprocess.PIPE, close_fds=True) - sendproc.stdin.write(data.encode()) - sendproc.stdin.close() - sendproc.wait() + msmtpproc.stdin.write(data) + msmtpproc.stdin.close() + msmtpproc.wait() + + return "250 OK" def usage(app): app = os.path.basename(app) - print("usage: %s [-hd] [sendcmd ...]" % (app), file=sys.stderr) + print("usage: %s [-h]" % (app), file=sys.stderr) sys.exit(1) def main(args): @@ -34,25 +42,17 @@ def main(args): print(str(err)) usage(args[0]) - sendcmd = "sudo -u praxis msmtp -t -C /home/praxis/.msmtprc" - dodebug = 0 for o, a in opts: if o == "-h": usage(args[0]) - elif o == "-d": - dodebug = 1 else: assert False, "unhandled option" - if len(largs) > 0: - sendcmd = " ".join(largs) - - if dodebug == 1: - server = smtpd.DebuggingServer(('127.0.0.1', 25), None) - else: - server = SMTPRedirectd(('127.0.0.1', 25), None) - server.sendcmd = sendcmd - asyncore.loop() + handler = SMTPHandler() + controller = Controller(handler, hostname="127.0.0.1", port=25) + controller.start() + while 1: + time.sleep(10) return 0