Any success with SMTP?

In the send_email function around line 490-530 in gramps_webapi/api/util.py.

When EMAIL_USE_TLS=True is configured, the code incorrectly uses smtplib.SMTP_SSL() for port 587:

  • Port 465 needs SMTP_SSL (implicit SSL - connection is encrypted from the start)

  • Port 587 needs SMTP + starttls() (STARTTLS - starts plain, then upgrades to TLS)

The current code does this:

if use_tls:
    smtp = smtplib.SMTP_SSL(host=host, port=port, timeout=10)  # ← WRONG for port 587!
else:
    smtp = smtplib.SMTP(host=host, port=port, timeout=10)

The logic should be based on the port number, not just the use_tls flag:

if port == 465:
    # Implicit SSL (port 465)
    smtp = smtplib.SMTP_SSL(host=host, port=port, timeout=10)
    smtp.ehlo()
else:
    # Plain connection with optional STARTTLS (port 587 or 25)
    smtp = smtplib.SMTP(host=host, port=port, timeout=10)
    smtp.ehlo()
    if use_tls:
        smtp.starttls()
        smtp.ehlo()