Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

send_email: Add a flag to force all emails through the queue.

Alex Vandiver6b3143d7fce3
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py
index 1dab60ad1c5..8a694a3cef4 100644
--- a/zerver/lib/send_email.py
+++ b/zerver/lib/send_email.py
@@ -257,7 +257,7 @@ class NoEmailArgumentError(CommandError):
# When changing the arguments to this function, you may need to write a
# migration to change or remove any emails in ScheduledEmail.
-def send_email(
+def send_immediate_email(
template_prefix: str,
to_user_ids: list[int] | None = None,
to_emails: list[str] | None = None,
@@ -326,6 +326,55 @@ def send_email(
raise EmailNotDeliveredError
+def send_email(
+ template_prefix: str,
+ to_user_ids: list[int] | None = None,
+ to_emails: list[str] | None = None,
+ from_name: str | None = None,
+ from_address: str | None = None,
+ reply_to_email: str | None = None,
+ language: str | None = None,
+ date: str | None = None,
+ context: Mapping[str, Any] = {},
+ realm: Realm | None = None,
+ connection: BaseEmailBackend | None = None,
+ dry_run: bool = False,
+ request: HttpRequest | None = None,
+) -> None:
+ if settings.EMAIL_ALWAYS_ENQUEUED and not dry_run:
+ queue_event_on_commit(
+ "email_senders",
+ dict(
+ template_prefix=template_prefix,
+ to_user_ids=to_user_ids,
+ to_emails=to_emails,
+ from_name=from_name,
+ from_address=from_address,
+ reply_to_email=reply_to_email,
+ language=language,
+ date=date,
+ context=context,
+ realm_id=realm.id if realm is not None else None,
+ ),
+ )
+ else:
+ send_immediate_email(
+ template_prefix,
+ to_user_ids,
+ to_emails,
+ from_name,
+ from_address,
+ reply_to_email,
+ language,
+ date,
+ context,
+ realm,
+ connection,
+ dry_run,
+ request,
+ )
+
+
@backoff.on_exception(backoff.expo, OSError, max_tries=MAX_CONNECTION_TRIES, logger=None)
def initialize_connection(connection: BaseEmailBackend | None = None) -> BaseEmailBackend:
if not connection:
@@ -382,7 +431,7 @@ def send_future_email(
}
if settings.DEVELOPMENT_LOG_EMAILS:
- send_email(
+ send_immediate_email(
template_prefix,
to_user_ids=to_user_ids,
to_emails=to_emails,
@@ -603,7 +652,7 @@ def custom_email_sender(
) -> None:
assert to_user_id is not None or to_email is not None
with suppress(EmailNotDeliveredError):
- send_email(
+ send_immediate_email(
email_id,
to_user_ids=[to_user_id] if to_user_id is not None else None,
to_emails=[to_email] if to_email is not None else None,
diff --git a/zerver/worker/email_senders_base.py b/zerver/worker/email_senders_base.py
index 712155b3666..fbd2a4f8ddb 100644
--- a/zerver/worker/email_senders_base.py
+++ b/zerver/worker/email_senders_base.py
@@ -14,7 +14,7 @@ from zerver.lib.send_email import (
EmailNotDeliveredError,
handle_send_email_format_changes,
initialize_connection,
- send_email,
+ send_immediate_email,
)
from zerver.models import Realm
from zerver.worker.base import ConcreteQueueWorker, LoopQueueProcessingWorker
@@ -69,7 +69,7 @@ class EmailSendingWorker(LoopQueueProcessingWorker):
copied_event["realm"] = Realm.objects.get(id=copied_event["realm_id"])
del copied_event["realm_id"]
self.connection = initialize_connection(self.connection)
- send_email(**copied_event, connection=self.connection)
+ send_immediate_email(**copied_event, connection=self.connection)
@override
def consume_batch(self, events: list[dict[str, Any]]) -> None:
diff --git a/zproject/default_settings.py b/zproject/default_settings.py
index e488a9d4c76..90eced0c343 100644
--- a/zproject/default_settings.py
+++ b/zproject/default_settings.py
@@ -449,6 +449,13 @@ EMAIL_BACKEND: str | None = None
# Set in settings.py when email isn't configured.
WARN_NO_EMAIL = False
+# If enabled, all email-sending will happen in the worker. This means
+# that the UI cannot display configuration errors which prevented
+# email sending, so this is usually left off except in
+# well-established deployments which know the configuration is
+# correct.
+EMAIL_ALWAYS_ENQUEUED = False
+
# If True, disable rate-limiting and other filters on sending error messages
# to admins, and enable logging on the error-reporting itself. Useful
# mainly in development.