Checking SMTP Email
When checking that your SMTP settings work properly it is most helpful to run each SMTP step individually.
I ended up down a rabbit hole, when attempting to check my SMTP settings in a Django application. An smtplib.SMTPServerDisconnected
error was occurring each time. I thought this was related to closed SMPT ports (465/587), and went so far as to open a ticket with hosting provider asking for the relevant ports to be opened for my servers.
But alas, it was a simple bloody misconfigured username! So actually, I was failing to authenticate. Compounding the issue, was that locally I the correct username, but on the server I have the username misconfigured.
When I finally did each step manually, the issue was easy to see.
Below are some step by step actions to send SMTP email with python;
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("Testing email")
msg['Subject'] = "Hello"
msg["From"] = "no-reply@example.com"
msg["To"] = "to@example.com"
s = smtplib.SMTP("smtp.example.com", 587)
s.login("postmaster@example.com", "supersecret")
s.sendmail(msg["From"], msg["To"], msg.as_string())
s.quit()