mirror of
https://github.com/simple-login/app.git
synced 2024-11-10 17:35:27 +08:00
handle newsletter unsubscribe when the subject=user_id*
This commit is contained in:
parent
4f7b30c204
commit
f624085aa3
3 changed files with 68 additions and 1 deletions
|
@ -1359,7 +1359,8 @@ def handle_spam(
|
|||
)
|
||||
|
||||
|
||||
def handle_unsubscribe(envelope: Envelope):
|
||||
def handle_unsubscribe(envelope: Envelope) -> str:
|
||||
"""return the SMTP status"""
|
||||
msg = email.message_from_bytes(envelope.original_content)
|
||||
|
||||
# format: alias_id:
|
||||
|
@ -1368,6 +1369,10 @@ def handle_unsubscribe(envelope: Envelope):
|
|||
# subject has the format {alias.id}=
|
||||
if subject.endswith("="):
|
||||
alias_id = int(subject[:-1])
|
||||
# {user.id}*
|
||||
elif subject.endswith("*"):
|
||||
user_id = int(subject[:-1])
|
||||
return handle_unsubscribe_user(user_id, envelope.mail_from)
|
||||
# some email providers might strip off the = suffix
|
||||
else:
|
||||
alias_id = int(subject)
|
||||
|
@ -1415,6 +1420,34 @@ def handle_unsubscribe(envelope: Envelope):
|
|||
return "250 Unsubscribe request accepted"
|
||||
|
||||
|
||||
def handle_unsubscribe_user(user_id: int, mail_from: str) -> str:
|
||||
"""return the SMTP status"""
|
||||
user = User.get(user_id)
|
||||
if not user:
|
||||
LOG.exception("No such user %s %s", user_id, mail_from)
|
||||
return "550 SL E22 so such user"
|
||||
|
||||
if mail_from != user.email:
|
||||
LOG.exception("Unauthorized mail_from %s %s", user, mail_from)
|
||||
return "550 SL E23 unsubscribe error"
|
||||
|
||||
user.notification = False
|
||||
db.session.commit()
|
||||
|
||||
send_email(
|
||||
user.email,
|
||||
f"You have been unsubscribed from SimpleLogin newsletter",
|
||||
render(
|
||||
"transactional/unsubscribe-newsletter.txt",
|
||||
user=user,
|
||||
),
|
||||
render(
|
||||
"transactional/unsubscribe-newsletter.html",
|
||||
user=user,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def handle_sender_email(envelope: Envelope):
|
||||
filename = (
|
||||
arrow.now().format("YYYY-MM-DD_HH-mm-ss") + "_" + random_string(10) + ".eml"
|
||||
|
|
22
templates/emails/transactional/unsubscribe-newsletter.html
Normal file
22
templates/emails/transactional/unsubscribe-newsletter.html
Normal file
|
@ -0,0 +1,22 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
{% call text() %}
|
||||
<h1>
|
||||
You have been unsubscribed from SimpleLogin newsletter.
|
||||
</h1>
|
||||
{% endcall %}
|
||||
|
||||
{% call text() %}
|
||||
We would love to know why do you want to unsubscribe from our communication.
|
||||
Please let us know by replying to this email.
|
||||
{% endcall %}
|
||||
|
||||
{% call text() %}
|
||||
Best, <br/>
|
||||
SimpleLogin Team.
|
||||
{% endcall %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
12
templates/emails/transactional/unsubscribe-newsletter.txt
Normal file
12
templates/emails/transactional/unsubscribe-newsletter.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
You have been unsubscribed from SimpleLogin newsletter.
|
||||
|
||||
We would love to know why do you want to unsubscribe from our communication.
|
||||
|
||||
Please let us know by replying to this email.
|
||||
|
||||
Best,
|
||||
SimpleLogin Team.
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in a new issue