"""Send a test email to any temp address via the local SMTP receiver.

This is how you'll TRIGGER mail from your own project too — just point
smtplib (or any mailer) at the SMTP host/port and send to the temp address.

Usage:
    python send_test.py  abc123@tempmail.local  "Hello"  "Body text here"
"""
import smtplib
import sys
from email.message import EmailMessage

import config


def send(to_addr, subject="Test email", body="This is a test.", html=None,
         from_addr="tester@example.com"):
    msg = EmailMessage()
    msg["From"] = from_addr
    msg["To"] = to_addr
    msg["Subject"] = subject
    msg.set_content(body)
    if html:
        msg.add_alternative(html, subtype="html")

    with smtplib.SMTP(config.SMTP_HOST if config.SMTP_HOST != "0.0.0.0"
                      else "127.0.0.1", config.SMTP_PORT) as s:
        s.send_message(msg)
    print(f"sent -> {to_addr}  (subject: {subject!r})")


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python send_test.py <to_addr> [subject] [body]")
        sys.exit(1)
    to = sys.argv[1]
    subject = sys.argv[2] if len(sys.argv) > 2 else "Test email"
    body = sys.argv[3] if len(sys.argv) > 3 else "Hello from send_test.py"
    send(to, subject, body,
         html=f"<h2>{subject}</h2><p>{body}</p><p><b>It works! 🎉</b></p>")
