Skip to content

Commit 9caba84

Browse files
committed
robust logs for email action
1 parent b867e56 commit 9caba84

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

backend/scripts/send_newsletter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
def send_newsletter_to_all():
2525
"""Send newsletter to all active subscribers"""
26+
27+
# Test encryption first
28+
from utils.encryption_utils import email_encryption
29+
if not email_encryption.test_encryption():
30+
print("❌ Encryption test failed! Cannot proceed with newsletter send.")
31+
return
2632

2733
# Get all active subscribers
2834
active_subscribers = NewsletterSubscriber.objects.filter(is_active=True)
@@ -44,13 +50,16 @@ def send_to_subscriber(subscriber):
4450
# Get the email address, handle decryption failures
4551
email_address = subscriber.get_email()
4652
if not email_address:
53+
print(f"❌ Failed to decrypt email for subscriber {subscriber.id}")
4754
return False, "Failed to decrypt email address"
4855

56+
print(f"📧 Sending to: {email_address}")
4957
email_sent = email_service.send_newsletter_email(
5058
email_address, str(subscriber.unsubscribe_token)
5159
)
5260
return email_sent, None
5361
except Exception as e:
62+
print(f"❌ Exception sending to subscriber {subscriber.id}: {e}")
5463
return False, str(e)
5564

5665
for subscriber in active_subscribers:

backend/utils/encryption_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def encrypt_email(self, email):
4444
def decrypt_email(self, encrypted_email):
4545
"""Decrypt an email address."""
4646
if not encrypted_email:
47+
print("Warning: No encrypted email provided for decryption")
4748
return None
4849

4950
try:
@@ -55,8 +56,13 @@ def decrypt_email(self, encrypted_email):
5556

5657
# Return as string
5758
return decrypted_bytes.decode("utf-8")
59+
except base64.binascii.Error as e:
60+
print(f"Error: Invalid base64 encoding in encrypted email: {e}")
61+
print(f"Encrypted email (first 50 chars): {encrypted_email[:50]}...")
62+
return None
5863
except Exception as e:
5964
print(f"Error decrypting email: {e}")
65+
print(f"Encrypted email (first 50 chars): {encrypted_email[:50]}...")
6066
return None
6167

6268
def create_email_hash(self, email):
@@ -66,7 +72,6 @@ def create_email_hash(self, email):
6672

6773
def create_hmac_email_hash(self, email):
6874
"""Create HMAC hash of the email (for newsletter uniqueness)."""
69-
"""Create a hash of the email for username field (SHA-256)."""
7075
normalized_email = email.lower().strip()
7176
return hashlib.sha256(normalized_email.encode("utf-8")).hexdigest()
7277

@@ -122,6 +127,17 @@ def update_user_email(self, user, new_email):
122127
user.save()
123128
return user
124129

130+
def test_encryption(self):
131+
"""Test if encryption/decryption is working properly."""
132+
test_email = "test@example.com"
133+
try:
134+
encrypted = self.encrypt_email(test_email)
135+
decrypted = self.decrypt_email(encrypted)
136+
return decrypted == test_email
137+
except Exception as e:
138+
print(f"Encryption test failed: {e}")
139+
return False
140+
125141

126142
# Global instance
127143
email_encryption = EmailEncryption()
2.66 MB
Binary file not shown.

0 commit comments

Comments
 (0)