Implement custom logging email backend for development: log outgoing emails to Loguru and stdout

This commit is contained in:
Ross
2025-12-15 13:14:12 +00:00
parent 03b23b9178
commit c071f38920
4 changed files with 17 additions and 3 deletions
+6 -2
View File
@@ -22,5 +22,9 @@ NGINX_HTTP_PORT=8080
# Pick a non-privileged HTTPS port to avoid conflicts with host installs # Pick a non-privileged HTTPS port to avoid conflicts with host installs
NGINX_HTTPS_PORT=8444 NGINX_HTTPS_PORT=8444
# Use console email backend for local development to avoid sending real mail # Use Django's file-based email backend for local development so messages
EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend # 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
+5 -1
View File
@@ -43,7 +43,11 @@ DEBUG_TOOLBAR_CONFIG = {
# deployment README. Adjust as needed. # deployment README. Adjust as needed.
STATIC_ROOT = '/usr/src/app/static/' 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: if not DEBUG:
LOGGING = { LOGGING = {
+2
View File
@@ -5044,6 +5044,8 @@ def candidate_email_details(request, cid, resend=False):
resend_flag = False resend_flag = False
email_sent, comment = user.email_details(resend=resend_flag) 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)}) return JsonResponse({"sent": email_sent, "comment": str(comment)})
+4
View File
@@ -327,6 +327,10 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
EMAIL_BACKEND = os.environ.get("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend") 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 = os.getenv('SENDGRID_API_KEY')
SENDGRID_API_KEY = ( SENDGRID_API_KEY = (
"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" "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"