From a2b3e6db0711ef79d237a8edf02e8d75a9b61e6f Mon Sep 17 00:00:00 2001 From: "Aapo (f021b) Rista" Date: Sun, 3 Nov 2024 18:43:39 +0200 Subject: [PATCH] Log sent emails to a file --- .../management/commands/email_users_3rd_round.py | 16 ++++++++++++++-- .../application_evaluator_config/settings.py | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django_server/application_evaluator/management/commands/email_users_3rd_round.py b/django_server/application_evaluator/management/commands/email_users_3rd_round.py index 30a8538..5741d07 100644 --- a/django_server/application_evaluator/management/commands/email_users_3rd_round.py +++ b/django_server/application_evaluator/management/commands/email_users_3rd_round.py @@ -1,12 +1,16 @@ # Management command to send emails to users (pilot manager and jury members) # of a specific application round. +import datetime +import pathlib import openpyxl from django.core import mail from django.core.management.base import BaseCommand from application_evaluator.models import ApplicationRound +from django.conf import settings + def read_challenge_doc(filename: str) -> dict: """Read Excel sheet and return it as a dict.""" @@ -46,6 +50,9 @@ def send_emails( pilot_manager_email: str, jury_email: str, ): + # use pathlib to create email_logs directory, if it doesn't exist + pathlib.Path("email_logs").mkdir(parents=True, exist_ok=True) + connection = mail.get_connection() connection.open() @@ -55,7 +62,7 @@ def send_emails( mail.EmailMessage( f"Pilot manager: {subject}", pilot_manager_email, - "noreply-evaluator-message@mg.forumvirium.fi", + settings.DEFAULT_FROM_EMAIL, [pilot_manager_email_address], connection=connection, ) @@ -65,11 +72,16 @@ def send_emails( mail.EmailMessage( f"Jury member: {subject}", jury_email, - "noreply-evaluator-message@mg.forumvirium.fi", + settings.DEFAULT_FROM_EMAIL, [email_address], connection=connection, ) ) + # Loop all emails and log the time, recipient and subject into a file %Y-%m-%d_email_log.txt + log_file = datetime.datetime.now().strftime("email_logs/%Y-%m-%d_email_log.txt") + with open(log_file, "a") as f: + for email in emails: + f.write(f"{datetime.datetime.now().isoformat()} {email.to} {email.subject}\n") connection.send_messages(emails) connection.close() diff --git a/django_server/application_evaluator_config/settings.py b/django_server/application_evaluator_config/settings.py index df15e42..c2100f0 100644 --- a/django_server/application_evaluator_config/settings.py +++ b/django_server/application_evaluator_config/settings.py @@ -212,7 +212,7 @@ EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER", "") EMAIL_PORT = os.environ.get("EMAIL_PORT", 25) EMAIL_USE_SSL = os.environ.get("EMAIL_USE_SSL", False) -DEFAULT_FROM_EMAIL = os.environ.get("EMAIL_HOST_USER", "webmaster@localhost") +DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL", "webmaster@localhost") SENTRY_DSN = os.environ.get("SENTRY_DSN", None) if SENTRY_DSN: