Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

Python smtplib raises SMTPServerDisconnected: please run connect() first

Errorsmtplib.SMTPServerDisconnected: please run connect() first

Short 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.

  • Applies to Python 3.12, Python 3.13
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.py
  2. 2Match what you see, then jump to its fix

    1. 1server = smtplib.SMTP() or SMTP('') followed directly by starttls() or login()SMTP() was created without a host and connect() never ranFix
    2. 2The first message sends, then a second send or a call outside the block failsThe object is used after its with block endedFix
    3. 3The exception appears right at ehlo() or starttls() with port 465Plain SMTP is used on a port that expects SSL from the first byteFix

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.py

2Fix

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.py

Confirmed by docs.python.org

Did this fix it?

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.py

2Fix

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.py

Confirmed by docs.python.org

Did this fix it?

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.py

2Fix

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.py

Confirmed by docs.python.org

Did this fix it?

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:

Related fixes

Email

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.