SMTPLIB
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.
NameError: name 'smtplib' is not defined?smtplib.SMTP(...) but there is no import smtplib statement.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.)import smtpLib, import smtplb, or referencing Smtplib won't bind the name you actually use later. Python names are case-sensitive.if branch (e.g. if x: import smtplib) binds the name only there; referencing it elsewhere — or before that branch runs — raises NameError.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.NameError: name 'smtplib' is not defined?import smtplibserver = 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 channelserver.login("you@example.com", "app_password")server.sendmail("you@example.com", ["to@example.com"], "Subject: Hi\n\nBody")server.quit()
smtplib:from smtplib import SMTP, SMTP_SSLserver = SMTP("smtp.gmail.com", 587) # NOT smtplib.SMTP(...)
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.
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.
References
FAQ
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
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.
© 2026 Courier. All rights reserved.