#!/usr/bin/env python3 """ Send a test email to yourself to verify SMTP and template rendering. Usage: python3 test_send.py your@email.com """ import sys, os, smtplib from pathlib import Path from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText BASE = Path(__file__).parent ENV_FILE = BASE / '.env' def load_env(): if ENV_FILE.exists(): with open(ENV_FILE) as f: for line in f: line = line.strip() if line and not line.startswith('#') and '=' in line: k, v = line.split('=', 1) os.environ.setdefault(k.strip(), v.strip()) load_env() SMTP_SERVER = os.environ.get('SMTP_SERVER', 'smtp.zoho.eu') SMTP_PORT = int(os.environ.get('SMTP_PORT', '587')) SMTP_USER = os.environ.get('SMTP_USERNAME', '') SMTP_PASS = os.environ.get('SMTP_PASSWORD', '') FROM_NAME = os.environ.get('FROM_NAME', 'Hermithire') FROM_EMAIL = os.environ.get('FROM_EMAIL', SMTP_USER) if len(sys.argv) < 2: print('Usage: python3 test_send.py recipient@email.com [1|2]') print(' 1 = initial email (default)') print(' 2 = follow-up email') sys.exit(1) TO_EMAIL = sys.argv[1] TOUCH = int(sys.argv[2]) if len(sys.argv) > 2 else 1 template_file = BASE / 'templates' / f'email_{TOUCH}_{"initial" if TOUCH == 1 else "followup"}.html' with open(template_file, encoding='utf-8') as f: html = f.read() # Fill in test values html = html.replace('{{first_name}}', 'Archie') html = html.replace('{{full_name}}', 'Archie Test') html = html.replace('{{organisation}}', 'Test Organisation') html = html.replace('{{segment}}', 'press') html = html.replace('{{unsubscribe_url}}', 'https://hermithire.com/unsubscribe?test=1') html = html.replace('{{email}}', TO_EMAIL) subject = ( 'TEST: An Unusual Invitation — Hermithire' if TOUCH == 1 else 'TEST: We Understand. Hermits Are Patient. — Hermithire' ) msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = f'{FROM_NAME} <{FROM_EMAIL}>' msg['To'] = TO_EMAIL msg['Reply-To'] = FROM_EMAIL msg.attach(MIMEText('Test plain text version. View HTML version for full experience.', 'plain')) msg.attach(MIMEText(html, 'html')) print(f'Connecting to {SMTP_SERVER}:{SMTP_PORT}...') try: server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) server.ehlo() server.starttls() server.ehlo() server.login(SMTP_USER, SMTP_PASS) print(f'Logged in as {SMTP_USER}') server.sendmail(FROM_EMAIL, [TO_EMAIL], msg.as_string()) server.quit() print(f'✓ Test email (touch {TOUCH}) sent to {TO_EMAIL}') print(f' Check your inbox and verify the HTML renders correctly.') print(f' Then run: python3 campaign.py send --touch 1 --dry-run') except Exception as e: print(f'✗ Error: {e}') print() print('Common fixes:') print(' 1. Generate an App Password in Zoho Mail Settings → Security → App Passwords') print(' 2. Make sure SMTP_USERNAME matches your Zoho sending address exactly') print(' 3. Check SMTP_SERVER: use smtp.zoho.eu (EU) or smtp.zoho.com (US)')