Merge branch 'weblate-gnu-mailman-mailman' into 'master'
[mailman.git] / contrib / qmail-lmtp
blobe85a0ba031071fcbd37a1a953c51ce0f5600c8d8
1 #!/usr/bin/env python3
3 # Written by Thomas Schneider <qsuscs@qsuscs.de>
4 # This script is placed in public domain.  If this is not applicable, consider
5 # it licensed under the CC-0:
6 # <https://creativecommons.org/publicdomain/zero/1.0/>
8 try:
9     import os
10     import sys
11     import smtplib
13     lmtp_host = sys.argv[3] if len(sys.argv) > 3 else 'localhost'
14     lmtp = smtplib.LMTP(lmtp_host, int(sys.argv[1]))
16     try:
17         # See <http://www.qmail.org/man/man8/qmail-command.html> for qmail command
18         # docs and supplied environment variables. We need to replace "1" with an 
19         # empty string, as qmail only supports EXT, EXT2, EXT3, EXT4.
20         arg_ext = sys.argv[2] if sys.argv[2] != "1" else ""
21         lmtp.sendmail(
22             os.environ['SENDER'],
23             os.environ['EXT' + arg_ext] + "@" + os.environ['HOST'],
24             sys.stdin.buffer.read()
25         )
26     except smtplib.SMTPResponseException as e:
27         if 400 <= e.smtp_code < 500:
28             exit(111)
29         # otherwise, it's either a 5xx aka permanent error or something else
30         # is already b0rked, thus raise -> exit(100) -> have qmail return a
31         # 5xx error
32         else:
33             raise
34 except:
35     exit(100)