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
This commit is contained in:
Antoine Eiche 2021-01-28 23:58:08 +01:00 committed by lewo
parent 17eec31cae
commit 5f431207b3
3 changed files with 122 additions and 54 deletions

View file

@ -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,