-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_debug_test.py
More file actions
156 lines (122 loc) · 5.63 KB
/
Copy pathemail_debug_test.py
File metadata and controls
156 lines (122 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
"""
Email Debug Test Script
Tests email sending with debug logging to troubleshoot sender name issues
"""
import json
import os
from email_utils import EmailConfig, send_email, load_email_config_from_file
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
def load_config():
"""Load configuration from GUI config file"""
return load_email_config_from_file("media_manager_config.json")
def test_email_method_1(email_config, test_body):
"""Test Method 1: Using shared email_utils module"""
print("\n=== Testing Method 1: Shared email_utils module ===")
try:
subject = "Debug Test - Method 1 (email_utils module)"
success = send_email(email_config, subject, test_body, print)
if success:
print("✓ Method 1 sent successfully")
return True
else:
print("✗ Method 1 failed")
return False
except Exception as e:
print(f"✗ Method 1 failed: {e}")
return False
def test_email_method_2(email_config, test_body):
"""Test Method 2: Direct string format (like Vaultwarden)"""
print("\n=== Testing Method 2: Direct string format ===")
try:
message = MIMEMultipart()
from_header = f"{email_config.sender_name} <{email_config.sender_email}>"
print(f"Setting From header to: {from_header}")
message["From"] = from_header
message["To"] = email_config.receiver_email
message["Subject"] = "Debug Test - Method 2 (Direct String)"
message.attach(MIMEText(test_body, "plain"))
# Show all headers before sending
print("Headers being sent:")
for key, value in message.items():
print(f" {key}: {value}")
# Send email
server = smtplib.SMTP(email_config.smtp_server, int(email_config.smtp_port))
server.starttls()
server.login(email_config.sender_email, email_config.password)
server.sendmail(email_config.sender_email, email_config.receiver_email, message.as_string())
server.quit()
print("✓ Method 2 sent successfully")
return True
except Exception as e:
print(f"✗ Method 2 failed: {e}")
return False
def test_email_method_3(email_config, test_body):
"""Test Method 3: Multiple headers approach"""
print("\n=== Testing Method 3: Multiple headers ===")
try:
message = MIMEMultipart()
from_header = formataddr((email_config.sender_name, email_config.sender_email))
message["From"] = from_header
message["Reply-To"] = from_header
message["Sender"] = email_config.sender_email
message["To"] = email_config.receiver_email
message["Subject"] = "Debug Test - Method 3 (Multiple Headers)"
print(f"Setting From header to: {from_header}")
print(f"Setting Reply-To header to: {from_header}")
print(f"Setting Sender header to: {email_config.sender_email}")
message.attach(MIMEText(test_body, "plain"))
# Show all headers before sending
print("Headers being sent:")
for key, value in message.items():
print(f" {key}: {value}")
# Send email
server = smtplib.SMTP(email_config.smtp_server, int(email_config.smtp_port))
server.starttls()
server.login(email_config.sender_email, email_config.password)
server.sendmail(email_config.sender_email, email_config.receiver_email, message.as_string())
server.quit()
print("✓ Method 3 sent successfully")
return True
except Exception as e:
print(f"✗ Method 3 failed: {e}")
return False
def main():
print("Email Debug Test Script")
print("======================")
# Load configuration
email_config = load_config()
if not email_config:
print("Could not load configuration. Make sure media_manager_config.json exists.")
return
# Verify email config
if not email_config.is_valid():
print("Email configuration is incomplete. Please check your settings.")
return
print("Current email configuration:")
print(f" Sender Name: '{email_config.sender_name}'")
print(f" Sender Email: '{email_config.sender_email}'")
print(f" Receiver Email: '{email_config.receiver_email}'")
print(f" SMTP Server: {email_config.smtp_server}:{email_config.smtp_port}")
test_body = f"""This is a debug test email sent at various times to test sender name display.
Current configuration:
- Sender Name: {email_config.sender_name}
- Sender Email: {email_config.sender_email}
Please check how the sender name appears in your email client and compare between the different test methods.
"""
# Test all methods
print(f"\nSending test emails to: {email_config.receiver_email}")
print("You should receive 3 test emails with different methods...")
test_email_method_1(email_config, test_body + "\nThis email was sent using Method 1 (formataddr)")
test_email_method_2(email_config, test_body + "\nThis email was sent using Method 2 (Direct String)")
test_email_method_3(email_config, test_body + "\nThis email was sent using Method 3 (Multiple Headers)")
print("\n" + "="*50)
print("Debug test complete!")
print("Check your email inbox and compare how the sender name")
print("appears for each of the 3 test emails.")
print("Let me know which method (if any) shows the sender name correctly.")
if __name__ == "__main__":
main()