|
|
@@ -0,0 +1,41 @@
|
|
|
+import smtplib
|
|
|
+import os
|
|
|
+from dotenv import load_dotenv
|
|
|
+
|
|
|
+load_dotenv()
|
|
|
+
|
|
|
+# Get email settings from environment variables
|
|
|
+username = os.getenv("MAIL_USERNAME")
|
|
|
+password = os.getenv("MAIL_PASSWORD")
|
|
|
+server = os.getenv("MAIL_SERVER")
|
|
|
+port = int(os.getenv("MAIL_PORT", "587"))
|
|
|
+
|
|
|
+print(f"Testing connection to {server}:{port} with username {username}")
|
|
|
+
|
|
|
+try:
|
|
|
+ # Attempt to connect
|
|
|
+ smtp = smtplib.SMTP(server, port)
|
|
|
+ smtp.ehlo()
|
|
|
+
|
|
|
+ # Start TLS if needed
|
|
|
+ if os.getenv("MAIL_STARTTLS") == "True":
|
|
|
+ smtp.starttls()
|
|
|
+ smtp.ehlo()
|
|
|
+
|
|
|
+ # Try to login
|
|
|
+ smtp.login(username, password)
|
|
|
+ print("Login successful!")
|
|
|
+
|
|
|
+ # Send a test email
|
|
|
+ from_addr = os.getenv("MAIL_FROM")
|
|
|
+ to_addr = "example@noreply.com"
|
|
|
+ message = f"From: {from_addr}\nTo: {to_addr}\nSubject: SMTP Test\n\nThis is a test email to verify SMTP connection."
|
|
|
+
|
|
|
+ smtp.sendmail(from_addr, to_addr, message)
|
|
|
+ print("Test email sent successfully!")
|
|
|
+
|
|
|
+ smtp.quit()
|
|
|
+
|
|
|
+except Exception as e:
|
|
|
+ print(f"Error: {e}")
|
|
|
+
|