Skip to content

Commit

Permalink
Debug email notifications system
Browse files Browse the repository at this point in the history
  • Loading branch information
ygalnezri committed Dec 10, 2024
1 parent c306a67 commit f7aa205
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
1 change: 0 additions & 1 deletion Watcher/Watcher/common/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
26 changes: 18 additions & 8 deletions Watcher/Watcher/common/utils/send_email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
4 changes: 2 additions & 2 deletions Watcher/Watcher/watcher/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
Expand Down

0 comments on commit f7aa205

Please sign in to comment.