Python smtplib raises SMTPServerDisconnected: please run connect() first
smtplib.SMTPServerDisconnected: please run connect() firstShort answer
smtplib raises SMTPServerDisconnected when you use an SMTP object that is not connected. Either the object was created with no host and connect() was never called, or a with block already sent QUIT, or the server hung up because you spoke plain SMTP on a port that expects SSL.
Fix it fast
1Find out which cause you have
Print how the object is created and where the first call happens. The traceback line that names ehlo, starttls, login or sendmail shows the first command that ran without a connection.
grep -n -E 'smtplib|SMTP(_SSL)?\(|starttls|login|sendmail|send_message' your_script.py2Match what you see, then jump to its fix
- 1
server = smtplib.SMTP() or SMTP('') followed directly by starttls() or login()SMTP() was created without a host and connect() never ranFix - 2
The first message sends, then a second send or a call outside the block failsThe object is used after its with block endedFix - 3
The exception appears right at ehlo() or starttls() with port 465Plain SMTP is used on a port that expects SSL from the first byteFix
- 1
What does the error look like?
smtplib.SMTPServerDisconnected: please run connect() first
Cause 1 of 3
SMTP() was created without a host and connect() never ran
1Check
The docs say the constructor calls connect() automatically only if host is set to a true value. Otherwise connect() must be called by hand.
grep -n 'SMTP(' your_script.py2Fix
Pass the host and port to the constructor, or call connect() before any other command.
import smtplib
server = smtplib.SMTP('smtp.example.com', 587)
# or
server = smtplib.SMTP()
server.connect('smtp.example.com', 587)3Verify
Run the script. The traceback no longer starts at the first command.
python your_script.pyConfirmed by docs.python.org
Cause 2 of 3
The object is used after its with block ended
1Check
The SMTP class works as a context manager and issues QUIT when the with block exits. Any call after that line uses a closed session.
grep -n -B2 -A8 'with smtplib' your_script.py2Fix
Keep every command inside the block, or open a new connection for the next batch.
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(user, password)
server.send_message(msg)
server.send_message(msg2)3Verify
Both messages go out in one run with no exception.
python your_script.pyConfirmed by docs.python.org
Cause 3 of 3
Plain SMTP is used on a port that expects SSL from the first byte
1Check
The Python docs say SMTP_SSL is for servers where SSL is required from the start of the connection, and its default port is 465. RFC 8314 says a TLS handshake begins immediately on port 465, while port 587 uses STARTTLS.
grep -n -E 'SMTP(_SSL)?\(' your_script.py2Fix
Use SMTP_SSL for an SSL-first port, or use SMTP plus starttls() on the port your provider documents for STARTTLS.
import smtplib
with smtplib.SMTP_SSL('smtp.example.com', 465) as server:
server.login(user, password)
server.send_message(msg)3Verify
Run again. If the server answers, the next error will be about login or addresses, not the connection.
python your_script.pyConfirmed by docs.python.org
How do I stop it coming back?
- Pass host and port in the constructor and use the with statement so QUIT happens once.
- Take the port and mode (STARTTLS or SSL) from your provider's page and keep both in one config file.
- Set a timeout on the constructor so a bad network fails fast with a clear error.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Django send_mail fails or hangs with an SSL error
smtplib.SMTPException raised from send_mail()
Django has two secure modes that must not be mixed: EMAIL_USE_TLS for explicit TLS (STARTTLS, usually port 587) and EMAIL_USE_SSL for implicit TLS (usually port 465).
4 causes, each with a fixGmail SMTP blocks you with 454 4.7.0 Too many login attempts
SMTP Error: 454 4.7.0 Too many login attempts
454 4.7.0 is a temporary block.
2 causes, each with a fixGmail rejects your mail with 530 5.5.1 Authentication Required
530 5.5.1 Authentication Required
Gmail refuses to accept a message from a connection that has not signed in.
3 causes, each with a fixGmail SMTP login fails with 535 or 534 5.7.14
534-5.7.14 Please log in via your web browser and then try again.
Gmail turned your sign-in down.
3 causes, each with a fixPython IMAP OAuth login to a shared mailbox in Microsoft 365 fails
imaplib.IMAP4.error: AUTHENTICATE failed.
Exchange Online accepts an OAuth token for IMAP only if the SASL user name is right and the token's identity has access to that mailbox.
4 causes, each with a fixPython says module smtplib has no attribute SMTP
print(smtplib.__file__) shows a path inside your own project
Python found a different smtplib than the standard one.
2 causes, each with a fix