diff options
author | Antoine Eiche <lewo@abesis.fr> | 2021-01-28 23:58:08 +0100 |
---|---|---|
committer | lewo <lewo@abesis.fr> | 2021-02-06 08:17:43 +0000 |
commit | 5f431207b335424907f9da4a4fedde9bff2cea91 (patch) | |
tree | eebad8e516e6e5eb7a13bdc53cc73d582653ddc5 /scripts | |
parent | 17eec31cae1d23e89888da1d0495c960ba624eaf (diff) |
postfix: forwarding emails of login accounts with keeping local copy
When a local account address is forwarded, the mails were not locally
kept. This was due to the way lookup tables were internally managed.
Instead of using lists to represent Postfix lookup tables, we now use
attribute sets: they can then be easily merged.
A regression test for
https://gitlab.com/simple-nixos-mailserver/nixos-mailserver/-/issues/
has been added: it sets a forward on a local address and ensure an
email sent to this address is locally kept.
Fixes #205
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/mail-check.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/scripts/mail-check.py b/scripts/mail-check.py index 47629fb..f5a97a5 100644 --- a/scripts/mail-check.py +++ b/scripts/mail-check.py @@ -24,28 +24,36 @@ def _send_mail(smtp_host, smtp_port, from_addr, from_pwd, to_addr, subject, star retry = RETRY while True: - with smtplib.SMTP(smtp_host, port=smtp_port) as smtp: - if starttls: - smtp.starttls() - if from_pwd is not None: - smtp.login(from_addr, from_pwd) - try: - smtp.sendmail(from_addr, [to_addr], message) - return - except smtplib.SMTPResponseException as e: - # This is a service unavailable error - # In this situation, we want to retry. - if e.smtp_code == 451: - if retry > 0: - retry = retry - 1 - time.sleep(1) - continue + try: + with smtplib.SMTP(smtp_host, port=smtp_port) as smtp: + try: + if starttls: + smtp.starttls() + if from_pwd is not None: + smtp.login(from_addr, from_pwd) + + smtp.sendmail(from_addr, [to_addr], message) + return + except smtplib.SMTPResponseException as e: + if e.smtp_code == 451: # service unavailable error + print(e) + elif e.smtp_code == 454: # smtplib.SMTPResponseException: (454, b'4.3.0 Try again later') + print(e) else: - print("Error while sending mail: %s" % e) - exit(5) - except Exception as e: - print("Error while sending mail: %s" % e) - exit(4) + raise + except OSError as e: + if e.errno in [16, -2]: + print("OSError exception message: ", e) + else: + raise + + if retry > 0: + retry = retry - 1 + time.sleep(1) + print("Retrying") + else: + print("Retry attempts exhausted") + exit(5) def _read_mail( imap_host, |