From c071f3892045f3599cc3da66981779c30ab3cbf1 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 15 Dec 2025 13:14:12 +0000 Subject: [PATCH] Implement custom logging email backend for development: log outgoing emails to Loguru and stdout --- .env.dev | 8 ++++++-- deploy/settings_local.py | 6 +++++- generic/views.py | 2 ++ rad/settings.py | 4 ++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.env.dev b/.env.dev index a458422d..00c91386 100644 --- a/.env.dev +++ b/.env.dev @@ -22,5 +22,9 @@ NGINX_HTTP_PORT=8080 # Pick a non-privileged HTTPS port to avoid conflicts with host installs NGINX_HTTPS_PORT=8444 -# Use console email backend for local development to avoid sending real mail -EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend \ No newline at end of file +# Use Django's file-based email backend for local development so messages +# are written to files under the mounted `./logs/emails` directory and are +# easy to inspect on the host. +EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend +#EMAIL_BACKEND=django.core.mail.backends.filebased.EmailBackend +EMAIL_FILE_PATH=/var/log/rad/emails \ No newline at end of file diff --git a/deploy/settings_local.py b/deploy/settings_local.py index da1ad29f..a25bc3af 100644 --- a/deploy/settings_local.py +++ b/deploy/settings_local.py @@ -43,7 +43,11 @@ DEBUG_TOOLBAR_CONFIG = { # deployment README. Adjust as needed. STATIC_ROOT = '/usr/src/app/static/' -EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" +import os + +# Allow environment to override the email backend (so dev .env can select +# a file-based backend while production keeps SMTP). +EMAIL_BACKEND = os.environ.get("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend") if not DEBUG: LOGGING = { diff --git a/generic/views.py b/generic/views.py index 540b9b77..d4ce0e5f 100644 --- a/generic/views.py +++ b/generic/views.py @@ -5044,6 +5044,8 @@ def candidate_email_details(request, cid, resend=False): resend_flag = False email_sent, comment = user.email_details(resend=resend_flag) + + logger.debug(f"Email details sent to CID {cid}: sent={email_sent}, comment={comment}") return JsonResponse({"sent": email_sent, "comment": str(comment)}) diff --git a/rad/settings.py b/rad/settings.py index 7c30c964..b3ed3a98 100644 --- a/rad/settings.py +++ b/rad/settings.py @@ -327,6 +327,10 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField" EMAIL_BACKEND = os.environ.get("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend") +# Path used by Django's file-based email backend. In dev this should be +# mounted into the container at `/var/log/rad/emails` (see `docker-compose.dev.yml`). +EMAIL_FILE_PATH = os.environ.get("EMAIL_FILE_PATH", os.path.join(BASE_DIR, "../logs/emails")) + # SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY') SENDGRID_API_KEY = ( "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"