With the flexibility that dot-qmail(5) provide by allowing to shell out it is no surprise, that users want their mail delivery on a machine where their home directory resides. Now, when moving users to a new machine, one user at a time, it happens that users within the same mail domain want their mails to be processed on different machines. An easy approach to solve this, when both machines believe themselves authoritative mail servers for that domain, is let the alias user throw those mails to the correct machine. As rsmtp is a standard part of my mail setup anyway, I can just repack the mail into batched SMTP format. This is not hard: in a qmail-command, all envelop information is provided in the environment and the mail on stdin.
#!/usr/bin/env python
import os
import shutil
import sys
import tempfile
EXIT_OK = 0
EXIT_TEMP_FAIL = 111
def main(argv):
if len(argv) != 1:
print "fmtbsmtp does not take arguments"
return EXIT_TEMP_FAIL
sender = os.environ["SENDER"]
newsender = ""
if "NEWSENDER" in os.environ:
newsender = os.environ["NEWSENDER"]
if len(newsender) > 0:
sender = newsender
if len(sender) < 1:
print "fmtbsmtp could not determine sender"
return EXIT_TEMP_FAIL
rcpt = os.environ["RECIPIENT"]
if len(rcpt) < 1:
print "fmtbsmtp could not determine recipient"
return EXIT_TEMP_FAIL
print "HELO mail.example.org"
print "MAIL FROM: %s" % (sender,)
print "RCPT TO: %s" % (rcpt,)
print "DATA"
with tempfile.TemporaryFile() as temp:
shutil.copyfileobj(sys.stdin, temp)
temp.seek(0)
# Drop the initial Delivered-To: line, so that
# the created bsmtp file can be directly used without
# a detected sporious cycle.
line = temp.readline()
if line == ("Delivered-To: %s\n" % (rcpt,)):
line = temp.readline()
while line != "":
line = line.rstrip("\n")
if line.startswith("."):
print ".%s" % (line,)
else:
print "%s" % (line,)
line = temp.readline()
print "."
print "QUIT"
return EXIT_OK
if __name__ == '__main__':
sys.exit(main(sys.argv))