From f7aa205b6b8dfcc5c9140eb5140882dc18f552a0 Mon Sep 17 00:00:00 2001 From: ygalnezri Date: Tue, 10 Dec 2024 11:03:48 +0100 Subject: [PATCH] Debug email notifications system --- Watcher/Watcher/common/core.py | 1 - .../common/utils/send_email_notifications.py | 26 +++++++++++++------ Watcher/Watcher/watcher/settings.py | 4 +-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Watcher/Watcher/common/core.py b/Watcher/Watcher/common/core.py index 2d43fc4..18a9d0e 100644 --- a/Watcher/Watcher/common/core.py +++ b/Watcher/Watcher/common/core.py @@ -375,7 +375,6 @@ def send_notification(channel, content_template, subscribers_filter, send_func, website_url = site.domain_name alert_id = alert_data.get('id', '-') email_body = get_site_monitoring_template(website_url, alert_id, alert_data) - print(f"alert_data: {alert_data}") elif app_name == 'data_leak': diff --git a/Watcher/Watcher/common/utils/send_email_notifications.py b/Watcher/Watcher/common/utils/send_email_notifications.py index dd60c98..aa59a31 100644 --- a/Watcher/Watcher/common/utils/send_email_notifications.py +++ b/Watcher/Watcher/common/utils/send_email_notifications.py @@ -13,29 +13,39 @@ def send_email_notifications(subject, body, emails_to, app_name): emails_to (list): List of recipients. app_name (str): The name of the sending application. """ - + # Check SMTP and email configuration if not settings.SMTP_SERVER or not settings.EMAIL_FROM: - print(f"{str(timezone.now())} - No configuration for Email, notifications disabled. Configure it in the '.env' file.") + print(f"{str(timezone.now())} - [ERROR] Missing SMTP or EMAIL_FROM configuration.") + print("Ensure SMTP_SERVER and EMAIL_FROM are set in the '.env' file.") return # Filter valid email addresses + print(f"{datetime.now()} - [INFO] Initial recipient list: {emails_to}") emails_to = [email if isinstance(email, str) else getattr(email, 'email', None) for email in emails_to] - emails_to = [email for email in emails_to if email] + emails_to = [email for email in emails_to if email] + print(f"{datetime.now()} - [INFO] Valid recipient list after filtering: {emails_to}") if not emails_to: - print(f"{datetime.now()} - No valid recipients for {app_name}.") + print(f"{datetime.now()} - [WARNING] No valid recipients for {app_name}.") return try: - # Create the email + # Create and send the email + print(f"{datetime.now()} - [INFO] Sending email...") + print(f"Subject: {subject}") + print(f"From: {settings.EMAIL_FROM}") + print(f"To: {emails_to}") + print(f"Email body preview: {body[:100]}... (truncated to 100 characters)") + email = EmailMessage( subject=f"{subject}", body=body, from_email=settings.EMAIL_FROM, to=emails_to, ) - email.content_subtype = "html" + email.content_subtype = "html" # Specify that the content is HTML email.send(fail_silently=False) - print(f"{datetime.now()} - Email successfully sent for {app_name}.") + print(f"{datetime.now()} - [SUCCESS] Email sent successfully for {app_name}.") except Exception as e: - print(f"{datetime.now()} - Failed to send email for {app_name}: {e}") + print(f"{datetime.now()} - [ERROR] Failed to send email for {app_name}.") + print(f"Exception: {e}") diff --git a/Watcher/Watcher/watcher/settings.py b/Watcher/Watcher/watcher/settings.py index 6edd61a..01f76f4 100755 --- a/Watcher/Watcher/watcher/settings.py +++ b/Watcher/Watcher/watcher/settings.py @@ -84,8 +84,8 @@ EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' SMTP_SERVER = os.environ.get('SMTP_SERVER', '') EMAIL_PORT = int(os.environ.get('EMAIL_PORT', 25)) -EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', 'False') == 'True' -EMAIL_USE_SSL = os.environ.get('EMAIL_USE_SSL', 'False') == 'True' +EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', 'False') +EMAIL_USE_SSL = os.environ.get('EMAIL_USE_SSL', 'False') EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', '') EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', '') EMAIL_FROM = os.environ.get('EMAIL_FROM', '')