Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
send_email: Log IP address sources (and more realms) of send_email.
Alex Vandiver621d0e5123d9
diff --git a/zerver/forms.py b/zerver/forms.pyindex 899461be37b..e0362228c87 100644--- a/zerver/forms.py+++ b/zerver/forms.py@@ -353,6 +353,8 @@ class ZulipPasswordResetForm(PasswordResetForm):from_name=FromAddress.security_email_from_name(user_profile=user),from_address=FromAddress.tokenized_no_reply_address(),context=context,+ realm=realm,+ request=request,)else:context["active_account_in_realm"] = False@@ -370,6 +372,7 @@ class ZulipPasswordResetForm(PasswordResetForm):language=language,context=context,realm=realm,+ request=request,)diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex c77585d688f..6d6450c58cb 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -18,6 +18,7 @@ from django.core.mail.backends.smtp import EmailBackendfrom django.core.mail.message import sanitize_addressfrom django.core.management import CommandErrorfrom django.db import transaction+from django.http import HttpRequestfrom django.template import loaderfrom django.template.exceptions import TemplateDoesNotExistfrom django.utils.timezone import now as timezone_now@@ -226,6 +227,7 @@ def send_email(realm: Optional[Realm] = None,connection: Optional[BaseEmailBackend] = None,dry_run: bool = False,+ request: Optional[HttpRequest] = None,) -> None:mail = build_email(template_prefix,@@ -247,7 +249,15 @@ def send_email(if connection is None:connection = get_connection()- logger.info("Sending %s email to %s", template, mail.to)+ cause = ""+ if request is not None:+ cause = f" (triggered from {request.META['REMOTE_ADDR']})"++ logging_recipient = mail.to+ if realm is not None:+ logging_recipient = f"{mail.to} in {realm.string_id}"++ logger.info("Sending %s email to %s%s", template, logging_recipient, cause)try:# This will call .open() for us, which is a no-op if it's already open;diff --git a/zerver/views/registration.py b/zerver/views/registration.pyindex 520b7612dc3..f63c12a3a1f 100644--- a/zerver/views/registration.py+++ b/zerver/views/registration.py@@ -552,18 +552,23 @@ def prepare_activation_url(def send_confirm_registration_email(- email: str, activation_url: str, language: str, realm: Optional[Realm] = None+ email: str,+ activation_url: str,+ *,+ realm: Optional[Realm] = None,+ request: Optional[HttpRequest] = None,) -> None:send_email("zerver/emails/confirm_registration",to_emails=[email],from_address=FromAddress.tokenized_no_reply_address(),- language=language,+ language=request.LANGUAGE_CODE if request is not None else None,context={"create_realm": (realm is None),"activate_url": activation_url,},realm=realm,+ request=request,)@@ -621,7 +626,7 @@ def create_realm(request: HttpRequest, creation_key: Optional[str] = None) -> Htreturn HttpResponseRedirect(activation_url)try:- send_confirm_registration_email(email, activation_url, request.LANGUAGE_CODE)+ send_confirm_registration_email(email, activation_url, request=request)except EmailNotDeliveredException:logging.error("Error in create_realm")return HttpResponseRedirect("/config-error/smtp")@@ -674,9 +679,7 @@ def accounts_home(email, request, streams=streams_to_subscribe, invited_as=invited_as)try:- send_confirm_registration_email(- email, activation_url, request.LANGUAGE_CODE, realm=realm- )+ send_confirm_registration_email(email, activation_url, request=request, realm=realm)except EmailNotDeliveredException:logging.error("Error in accounts_home")return HttpResponseRedirect("/config-error/smtp")@@ -752,6 +755,7 @@ def find_account(to_user_ids=[user.id],context=context,from_address=FromAddress.SUPPORT,+ request=request,)# Note: Show all the emails in the result otherwise this