Skip to content

Commit

Permalink
Log sent emails to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
aapris committed Nov 3, 2024
1 parent 8aed888 commit a2b3e6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down Expand Up @@ -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()

Expand All @@ -55,7 +62,7 @@ def send_emails(
mail.EmailMessage(
f"Pilot manager: {subject}",
pilot_manager_email,
"[email protected]",
settings.DEFAULT_FROM_EMAIL,
[pilot_manager_email_address],
connection=connection,
)
Expand All @@ -65,11 +72,16 @@ def send_emails(
mail.EmailMessage(
f"Jury member: {subject}",
jury_email,
"[email protected]",
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()
Expand Down
2 changes: 1 addition & 1 deletion django_server/application_evaluator_config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a2b3e6d

Please sign in to comment.