diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py index 9cfca2df27..1fbbda4b8b 100644 --- a/zerver/lib/send_email.py +++ b/zerver/lib/send_email.py @@ -21,6 +21,7 @@ from django.core.mail.message import sanitize_address from django.core.management import CommandError from django.db import transaction from django.db.models import QuerySet +from django.db.models.functions import Lower from django.http import HttpRequest from django.template import loader from django.utils.timezone import now as timezone_now @@ -583,7 +584,7 @@ def send_custom_email( options: Dict[str, str], add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None, distinct_email: bool = False, -) -> None: +) -> QuerySet[UserProfile]: """ Helper for `manage.py send_custom_email`. @@ -598,7 +599,11 @@ def send_custom_email( users = users.select_related("realm") if distinct_email: - users = users.distinct("delivery_email").order_by("delivery_email", "id") + users = ( + users.annotate(lower_email=Lower("delivery_email")) + .distinct("lower_email") + .order_by("lower_email", "id") + ) else: users = users.order_by("id") for user_profile in users: @@ -617,6 +622,7 @@ def send_custom_email( if dry_run: break + return users def send_custom_server_email( diff --git a/zerver/management/commands/send_custom_email.py b/zerver/management/commands/send_custom_email.py index 2e99d994b7..ce27835a03 100644 --- a/zerver/management/commands/send_custom_email.py +++ b/zerver/management/commands/send_custom_email.py @@ -197,7 +197,7 @@ class Command(ZulipBaseCommand): users = users.exclude( Q(tos_version=None) | Q(tos_version=UserProfile.TOS_VERSION_BEFORE_FIRST_LOGIN) ) - send_custom_email( + users = send_custom_email( users, dry_run=dry_run, options=options,