Merge pull request #99 from simple-login/to-header

append alias into the TO header if it's not present in To or CC
This commit is contained in:
Son Nguyen Kim 2020-03-05 21:20:00 +01:00 committed by GitHub
commit 32713b5fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 0 deletions

View File

@ -244,6 +244,17 @@ def get_or_create_forward_email(
return forward_email
def should_append_alias(msg, alias):
"""whether an alias should be appened to TO header in message"""
if msg["To"] and alias in msg["To"]:
return False
if msg["Cc"] and alias in msg["Cc"]:
return False
return True
def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
"""return *status_code message*"""
alias = rcpt_to.lower() # alias@SL
@ -281,6 +292,16 @@ def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
msg.replace_header("From", from_header)
LOG.d("new from header:%s", from_header)
# append alias into the TO header if it's not present in To or CC
if should_append_alias(msg, alias):
LOG.d("append alias %s to TO header %s", alias, msg["To"])
if msg["To"]:
to_header = msg["To"] + "," + alias
else:
to_header = alias
msg.replace_header("To", to_header)
# add List-Unsubscribe header
unsubscribe_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}"
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")