add
This commit is contained in:
85
scripts/test_mail.py
Normal file
85
scripts/test_mail.py
Normal file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Single test file for mail service - does everything in one place
|
||||
"""
|
||||
from datetime import datetime, date
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
print("=" * 60)
|
||||
print("📧 MAIL SERVICE TEST")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from app_core.services.mailer_service import MailerService
|
||||
from app_core.config.settings import AppSettings
|
||||
|
||||
# Initialize services
|
||||
settings = AppSettings()
|
||||
service = MailerService(settings)
|
||||
|
||||
# Get most recent date with data
|
||||
chosen_date = service.select_report_date()
|
||||
if not chosen_date:
|
||||
print("❌ No data available")
|
||||
exit(1)
|
||||
|
||||
print(f"✅ Using date: {chosen_date}")
|
||||
|
||||
# Fetch data
|
||||
df = service.fetch_daily_rows(chosen_date)
|
||||
print(f"✅ Found {len(df)} records")
|
||||
|
||||
# Build email
|
||||
row = df.iloc[0].to_dict()
|
||||
html = service.build_email_html(row, df)
|
||||
print(f"✅ Email HTML generated ({len(html)} characters)")
|
||||
|
||||
# Show what would be logged
|
||||
ist = ZoneInfo("Asia/Kolkata")
|
||||
now_ist = datetime.now(ist)
|
||||
|
||||
print(f"\n📝 Data that would be inserted in email_logs:")
|
||||
print(f" sent_at: {now_ist}")
|
||||
print(f" recipients: loyalydigital@gmail.com")
|
||||
print(f" subject: Daily Digest - {chosen_date}")
|
||||
print(f" status: sent")
|
||||
print(f" date_for: {chosen_date}")
|
||||
print(f" error: null")
|
||||
|
||||
# Ask user
|
||||
print(f"\n🚀 Send email to loyalydigital@gmail.com? (y/n):")
|
||||
send_confirm = input(" Send? ").strip().lower()
|
||||
|
||||
if send_confirm == 'y':
|
||||
print(f"\n📤 Sending email...")
|
||||
|
||||
recipients = ["loyalydigital@gmail.com"]
|
||||
subject = f"Daily Digest - {chosen_date}"
|
||||
|
||||
ok, msg = service.send_email(recipients, subject, html)
|
||||
|
||||
if ok:
|
||||
print(f"✅ Email sent successfully!")
|
||||
service.log_email(recipients, subject, str(chosen_date), "sent", None)
|
||||
print(f"✅ Logged in database")
|
||||
else:
|
||||
print(f"❌ Email failed: {msg}")
|
||||
service.log_email(recipients, subject, str(chosen_date), "failed", msg)
|
||||
print(f"✅ Failed attempt logged")
|
||||
else:
|
||||
print(f"\n⏭️ Email not sent (test mode)")
|
||||
|
||||
# Show recent logs
|
||||
print(f"\n📋 Recent email logs:")
|
||||
logs = service.recent_logs(limit=5)
|
||||
for log in logs:
|
||||
print(f" {log['status']} - {log['subject']} at {log['sent_at']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print(f"\n" + "=" * 60)
|
||||
print("🏁 Done!")
|
||||
print("=" * 60)
|
||||
Reference in New Issue
Block a user