#!/usr/bin/env python3
"""Quick email sending test script"""
import sys
from datetime import datetime
# Add src to path
sys.path.insert(0, "src")
from src.config import get_config
from src.logger import setup_logger
from src.email.sender import EmailSender
def test_email():
"""Send a test email"""
setup_logger()
config = get_config()
print("\n" + "=" * 60)
print("Testing Email Configuration")
print("=" * 60)
print(f"\nSMTP Server: {config.email.smtp.host}:{config.email.smtp.port}")
print(f"TLS: {config.email.smtp.use_tls}")
print(f"SSL: {config.email.smtp.use_ssl}")
print(f"From: {config.email.from_}")
print(f"To: {config.email.to}")
print(f"Username: {config.env.smtp_username or '(not set)'}")
print(f"Password: {'***' if config.env.smtp_password else '(not set)'}")
print("\nSending test email...")
# Create test email content
subject = "News Agent Test Email"
html_content = (
"""
Congratulations! Your email configuration is working correctly.
Test Details:
- Date: """
+ datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+ """
- SMTP Server: """
+ f"{config.email.smtp.host}:{config.email.smtp.port}"
+ """
- Encryption: """
+ ("TLS" if config.email.smtp.use_tls else "SSL" if config.email.smtp.use_ssl else "None")
+ """
Your News Agent is now ready to send daily digests!
This is a test email from News Agent. If you received this,
your SMTP configuration is correct and you should start receiving
daily news digests.
"""
)
text_content = f"""
News Agent Email Test
=====================
✓ Congratulations! Your email configuration is working correctly.
Test Details:
- Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
- SMTP Server: {config.email.smtp.host}:{config.email.smtp.port}
- Encryption: {"TLS" if config.email.smtp.use_tls else "SSL" if config.email.smtp.use_ssl else "None"}
Your News Agent is now ready to send daily digests!
---
This is a test email from News Agent. If you received this,
your SMTP configuration is correct and you should start receiving
daily news digests.
"""
# Send email
sender = EmailSender()
success = sender.send(subject, html_content, text_content)
print("\n" + "=" * 60)
if success:
print("✓ SUCCESS! Test email sent successfully!")
print(f"✓ Check your inbox: {config.email.to}")
else:
print("✗ FAILED! Email could not be sent.")
print("✗ Check the error messages above.")
print("\nTroubleshooting steps:")
print("1. Verify SMTP credentials in .env file")
print("2. Check SMTP server settings in config.yaml")
print("3. Review logs: cat data/logs/news-agent.log")
print("4. See SMTP_CONFIG.md for detailed troubleshooting")
print("=" * 60 + "\n")
return success
if __name__ == "__main__":
try:
success = test_email()
sys.exit(0 if success else 1)
except Exception as e:
print(f"\n✗ ERROR: {e}")
import traceback
traceback.print_exc()
sys.exit(1)