SMTPLIB

Name "smtplib" is not defined

Python raises NameError: name 'smtplib' is not defined when smtplib is used without importing it. Add import smtplib — it's in the standard library.

Updated Jul 1, 2026

The short answer

NameError: name 'smtplib' is not defined is a Python error, not an SMTP server reply. Python raises it when your code references smtplib but that name was never bound in the current scope — almost always a missing import smtplib at the top of the file. Add the import (or fix a typo/scope issue) and the error disappears.

NameError: name 'smtplib' is not defined is raised by the Python interpreter, not by your SMTP server or email provider. It has nothing to do with SMTP reply codes, authentication, or deliverability — it fires before any network connection is even attempted. Python raises a NameError whenever you use a name that isn't bound in the local or global scope, and smtplib becomes a bound name only after you import it.

What causes NameError: name 'smtplib' is not defined?

  • The module was never imported (the usual cause). Your script calls smtplib.SMTP(...) but there is no import smtplib statement.
  • You imported a class, not the module. If you wrote from smtplib import SMTP, that binds the name SMTP only — smtplib itself is still undefined, so smtplib.SMTP(...) raises NameError. (Compare this to the different module 'smtplib' has no attribute 'SMTP' error, which means the module name resolved but the attribute did not.)
  • A typo or wrong casing. import smtpLib, import smtplb, or referencing Smtplib won't bind the name you actually use later. Python names are case-sensitive.
  • The import is in a narrower scope than the use. Importing inside a function or an if branch (e.g. if x: import smtplib) binds the name only there; referencing it elsewhere — or before that branch runs — raises NameError.
  • The import line failed silently. If the import sits inside a try/except that swallowed an ImportError (for example because a local file named smtplib.py is shadowing the standard library and broke the import), execution continues and the later reference to smtplib is undefined.

How do I fix NameError: name 'smtplib' is not defined?

  1. Add the import at the top of your module, before any use of the name:
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls() # upgrade to TLS on port 587 (STARTTLS)
server.ehlo() # re-identify over the now-encrypted channel
server.login("you@example.com", "app_password")
server.sendmail("you@example.com", ["to@example.com"], "Subject: Hi\n\nBody")
server.quit()
  1. If you only need the class, import it consistently and reference it directly — don't prefix it with smtplib:
from smtplib import SMTP, SMTP_SSL
server = SMTP("smtp.gmail.com", 587) # NOT smtplib.SMTP(...)
  1. Make sure the import lives in the same (or an enclosing) scope as every place you use smtplib. Module-level (top of file) is safest.

  2. Check for a name clash: if you have your own file called smtplib.py in the project, rename it. It can shadow the standard-library module and break the import, which downstream surfaces as this NameError.

smtplib is part of the Python standard library, so there is nothing to pip install — a clean import smtplib at module scope is all that's required. Per the official docs, smtplib.SMTP defaults to port 25, SMTP_SSL to port 465 (implicit TLS), and SMTP(...).starttls() upgrades a plaintext connection (typically port 587) — all three are valid; pick the port your provider documents. The docs also note you should call ehlo() again after starttls() so the client re-negotiates ESMTP capabilities over the now-encrypted channel.

FAQ

Common questions

No. It is a Python interpreter error raised before any SMTP connection is made. It means the name smtplib was never bound in your code's scope — it is unrelated to SMTP reply codes, authentication, or deliverability.

One API, every provider

Stop debugging raw provider errors

Courier connects to your email, SMS, and push providers, handles retries and failover, and surfaces delivery errors in plain language.

Last reviewed Jul 1, 2026. Courier is not affiliated with third-party providers; error behavior may vary by implementation.