Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
send_custom_email: Only put the unsubscribe footer on marketing emails.
Alex Vandiver95b0ab31bed2
diff --git a/templates/zerver/emails/custom_email_base.pre.html b/templates/zerver/emails/custom_email_base.pre.htmlindex 9ff778eac12..d8ad3932c78 100644--- a/templates/zerver/emails/custom_email_base.pre.html+++ b/templates/zerver/emails/custom_email_base.pre.html@@ -14,7 +14,7 @@{% block manage_preferences %}{% if remote_server_email %}<p>You are receiving this email to update you about important changes to Zulip's Terms of Service.</p>- {% else %}+ {% elif unsubscribe_link %}<p><a href="{{ realm_uri }}/#settings/notifications">{{ _("Manage email preferences") }}</a> | <a href="{{ unsubscribe_link }}">{{ _("Unsubscribe from marketing emails") }}</a></p>{% endif %}{% endblock %}diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex 7e4122e37e8..c904de3696a 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -27,7 +27,7 @@ from django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django.utils.translation import override as override_language-from confirmation.models import generate_key, one_click_unsubscribe_link+from confirmation.models import generate_keyfrom zerver.lib.logging_util import log_to_filefrom zerver.models import EMAIL_TYPES, Realm, ScheduledEmail, UserProfile, get_user_profile_by_idfrom zproject.email_backends import EmailLogBackEnd, get_forward_address@@ -565,7 +565,6 @@ def send_custom_email(context: Dict[str, Union[List[str], str]] = {"realm_uri": user_profile.realm.uri,"realm_name": user_profile.realm.name,- "unsubscribe_link": one_click_unsubscribe_link(user_profile, "marketing"),}if add_context is not None:add_context(context, user_profile)diff --git a/zerver/management/commands/send_custom_email.py b/zerver/management/commands/send_custom_email.pyindex b7a27d7e3f2..5e0a116e383 100644--- a/zerver/management/commands/send_custom_email.py+++ b/zerver/management/commands/send_custom_email.py@@ -1,9 +1,10 @@from argparse import ArgumentParser-from typing import Any, List+from typing import Any, Callable, Dict, List, Optional, Unionfrom django.conf import settingsfrom django.db.models import Q, QuerySet+from confirmation.models import one_click_unsubscribe_linkfrom zerver.lib.management import ZulipBaseCommandfrom zerver.lib.send_email import send_custom_emailfrom zerver.models import Realm, UserProfile@@ -79,6 +80,9 @@ class Command(ZulipBaseCommand):def handle(self, *args: Any, **options: str) -> None:target_emails: List[str] = []users: QuerySet[UserProfile] = UserProfile.objects.none()+ add_context: Optional[+ Callable[[Dict[str, Union[List[str], str]], UserProfile], None]+ ] = Noneif options["entire_server"]:users = UserProfile.objects.filter(@@ -95,6 +99,13 @@ class Command(ZulipBaseCommand):enable_marketing_emails=True,long_term_idle=False,).distinct("delivery_email")++ def add_marketing_unsubscribe(+ context: Dict[str, Union[List[str], str]], user: UserProfile+ ) -> None:+ context["unsubscribe_link"] = one_click_unsubscribe_link(user, "marketing")++ add_context = add_marketing_unsubscribeelif options["remote_servers"]:from zilencer.models import RemoteZulipServer@@ -129,7 +140,9 @@ class Command(ZulipBaseCommand):users = users.exclude(Q(tos_version=None) | Q(tos_version=UserProfile.TOS_VERSION_BEFORE_FIRST_LOGIN))- send_custom_email(users, target_emails=target_emails, options=options)+ send_custom_email(+ users, target_emails=target_emails, options=options, add_context=add_context+ )if options["dry_run"]:print("Would send the above email to:")diff --git a/zerver/tests/fixtures/email/custom_emails/email_base_headers_custom_test.md b/zerver/tests/fixtures/email/custom_emails/email_base_headers_custom_test.mdnew file mode 100644index 00000000000..4cf4a5478d0--- /dev/null+++ b/zerver/tests/fixtures/email/custom_emails/email_base_headers_custom_test.md@@ -0,0 +1,4 @@+From: TestFrom+Subject: Test subject++Test body with {{ custom }} value.diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.pyindex 662beec5436..62b5a85f9a7 100644--- a/zerver/tests/test_email_notifications.py+++ b/zerver/tests/test_email_notifications.py@@ -1,5 +1,6 @@import tempfilefrom datetime import datetime, timedelta, timezone+from typing import Dict, List, Unionfrom unittest.mock import patchimport ldap@@ -105,6 +106,48 @@ class TestCustomEmails(ZulipTestCase):self.assertFalse(msg.reply_to)self.assertEqual("Test body", msg.body)+ def test_send_custom_email_context(self) -> None:+ hamlet = self.example_user("hamlet")+ markdown_template_path = (+ "zerver/tests/fixtures/email/custom_emails/email_base_headers_test.md"+ )+ send_custom_email(+ UserProfile.objects.filter(id=hamlet.id),+ options={+ "markdown_template_path": markdown_template_path,+ "dry_run": False,+ },+ )+ self.assert_length(mail.outbox, 1)+ msg = mail.outbox[0]++ # We default to not including an unsubscribe link in the headers+ self.assertEqual(msg.extra_headers.get("X-Auto-Response-Suppress"), "All")+ self.assertIsNone(msg.extra_headers.get("List-Unsubscribe"))++ mail.outbox = []+ markdown_template_path = (+ "zerver/tests/fixtures/email/custom_emails/email_base_headers_custom_test.md"+ )++ def add_context(context: Dict[str, Union[List[str], str]], user: UserProfile) -> None:+ context["unsubscribe_link"] = "some@email"+ context["custom"] = str(user.id)++ send_custom_email(+ UserProfile.objects.filter(id=hamlet.id),+ options={+ "markdown_template_path": markdown_template_path,+ "dry_run": False,+ },+ add_context=add_context,+ )+ self.assert_length(mail.outbox, 1)+ msg = mail.outbox[0]+ self.assertEqual(msg.extra_headers.get("X-Auto-Response-Suppress"), "All")+ self.assertEqual(msg.extra_headers.get("List-Unsubscribe"), "<some@email>")+ self.assertIn(f"Test body with {hamlet.id} value", msg.body)+def test_send_custom_email_no_argument(self) -> None:hamlet = self.example_user("hamlet")from_name = "from_name_test"