/
usr
/
share
/
doc
/
python3-docs
/
html
/
library
/
/usr/share/doc/python3-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
61709
0644
edit
dl
rm
abc.html
41394
0644
edit
dl
rm
aifc.html
28114
0644
edit
dl
rm
allos.html
32236
0644
edit
dl
rm
archiving.html
11992
0644
edit
dl
rm
argparse.html
274984
0644
edit
dl
rm
array.html
33547
0644
edit
dl
rm
ast.html
39595
0644
edit
dl
rm
asynchat.html
32546
0644
edit
dl
rm
asyncio-dev.html
51869
0644
edit
dl
rm
asyncio-eventloop.html
111215
0644
edit
dl
rm
asyncio-eventloops.html
33026
0644
edit
dl
rm
asyncio-protocol.html
87431
0644
edit
dl
rm
asyncio-queue.html
23957
0644
edit
dl
rm
asyncio-stream.html
61005
0644
edit
dl
rm
asyncio-subprocess.html
58015
0644
edit
dl
rm
asyncio-sync.html
35678
0644
edit
dl
rm
asyncio-task.html
83968
0644
edit
dl
rm
asyncio.html
28026
0644
edit
dl
rm
asyncore.html
42706
0644
edit
dl
rm
atexit.html
18433
0644
edit
dl
rm
audioop.html
35442
0644
edit
dl
rm
base64.html
36454
0644
edit
dl
rm
bdb.html
43423
0644
edit
dl
rm
binary.html
12723
0644
edit
dl
rm
binascii.html
24745
0644
edit
dl
rm
binhex.html
11669
0644
edit
dl
rm
bisect.html
25602
0644
edit
dl
rm
builtins.html
11604
0644
edit
dl
rm
bz2.html
33358
0644
edit
dl
rm
calendar.html
39539
0644
edit
dl
rm
cgi.html
55881
0644
edit
dl
rm
cgitb.html
12765
0644
edit
dl
rm
chunk.html
16872
0644
edit
dl
rm
cmath.html
34171
0644
edit
dl
rm
cmd.html
43811
0644
edit
dl
rm
code.html
27886
0644
edit
dl
rm
codecs.html
134071
0644
edit
dl
rm
codeop.html
14304
0644
edit
dl
rm
collections.abc.html
60912
0644
edit
dl
rm
collections.html
154946
0644
edit
dl
rm
colorsys.html
12627
0644
edit
dl
rm
compileall.html
28263
0644
edit
dl
rm
concurrency.html
21203
0644
edit
dl
rm
concurrent.futures.html
60660
0644
edit
dl
rm
concurrent.html
7959
0644
edit
dl
rm
configparser.html
141052
0644
edit
dl
rm
constants.html
17500
0644
edit
dl
rm
contextlib.html
88801
0644
edit
dl
rm
copy.html
13841
0644
edit
dl
rm
copyreg.html
14725
0644
edit
dl
rm
crypt.html
20331
0644
edit
dl
rm
crypto.html
10914
0644
edit
dl
rm
csv.html
69043
0644
edit
dl
rm
ctypes.html
264529
0644
edit
dl
rm
curses.ascii.html
25197
0644
edit
dl
rm
curses.html
175551
0644
edit
dl
rm
curses.panel.html
16706
0644
edit
dl
rm
custominterp.html
8734
0644
edit
dl
rm
datatypes.html
23950
0644
edit
dl
rm
datetime.html
306435
0644
edit
dl
rm
dbm.html
43119
0644
edit
dl
rm
debug.html
13983
0644
edit
dl
rm
decimal.html
233486
0644
edit
dl
rm
development.html
25123
0644
edit
dl
rm
difflib.html
96156
0644
edit
dl
rm
dis.html
106191
0644
edit
dl
rm
distribution.html
10506
0644
edit
dl
rm
distutils.html
10884
0644
edit
dl
rm
doctest.html
179752
0644
edit
dl
rm
dummy_threading.html
9316
0644
edit
dl
rm
email.charset.html
26431
0644
edit
dl
rm
email.compat32-message.html
86150
0644
edit
dl
rm
email.contentmanager.html
30028
0644
edit
dl
rm
email.encoders.html
14250
0644
edit
dl
rm
email.errors.html
19704
0644
edit
dl
rm
email.examples.html
57872
0644
edit
dl
rm
email.generator.html
41798
0644
edit
dl
rm
email.header.html
31219
0644
edit
dl
rm
email.headerregistry.html
57670
0644
edit
dl
rm
email.html
27741
0644
edit
dl
rm
email.iterators.html
13532
0644
edit
dl
rm
email.message.html
87210
0644
edit
dl
rm
email.mime.html
36081
0644
edit
dl
rm
email.parser.html
47430
0644
edit
dl
rm
email.policy.html
75241
0644
edit
dl
rm
email.util.html
33853
0644
edit
dl
rm
ensurepip.html
18700
0644
edit
dl
rm
enum.html
133949
0644
edit
dl
rm
errno.html
41331
0644
edit
dl
rm
exceptions.html
85151
0644
edit
dl
rm
faulthandler.html
23396
0644
edit
dl
rm
fcntl.html
27321
0644
edit
dl
rm
filecmp.html
25036
0644
edit
dl
rm
fileformats.html
11129
0644
edit
dl
rm
fileinput.html
30030
0644
edit
dl
rm
filesys.html
13418
0644
edit
dl
rm
fnmatch.html
16987
0644
edit
dl
rm
formatter.html
37666
0644
edit
dl
rm
fpectl.html
17538
0644
edit
dl
rm
fractions.html
29918
0644
edit
dl
rm
frameworks.html
13471
0644
edit
dl
rm
ftplib.html
54429
0644
edit
dl
rm
functional.html
9002
0644
edit
dl
rm
functions.html
201486
0644
edit
dl
rm
functools.html
64951
0644
edit
dl
rm
gc.html
29524
0644
edit
dl
rm
getopt.html
25353
0644
edit
dl
rm
getpass.html
12061
0644
edit
dl
rm
gettext.html
76641
0644
edit
dl
rm
glob.html
18432
0644
edit
dl
rm
grp.html
12383
0644
edit
dl
rm
gzip.html
32669
0644
edit
dl
rm
hashlib.html
79554
0644
edit
dl
rm
heapq.html
35529
0644
edit
dl
rm
hmac.html
17827
0644
edit
dl
rm
html.entities.html
11361
0644
edit
dl
rm
html.html
11108
0644
edit
dl
rm
html.parser.html
41842
0644
edit
dl
rm
http.client.html
65250
0644
edit
dl
rm
http.cookiejar.html
96195
0644
edit
dl
rm
http.cookies.html
39329
0644
edit
dl
rm
http.html
37602
0644
edit
dl
rm
http.server.html
58191
0644
edit
dl
rm
i18n.html
10464
0644
edit
dl
rm
idle.html
48412
0644
edit
dl
rm
imaplib.html
67339
0644
edit
dl
rm
imghdr.html
13456
0644
edit
dl
rm
imp.html
48153
0644
edit
dl
rm
importlib.html
173109
0644
edit
dl
rm
index.html
67768
0644
edit
dl
rm
inspect.html
130355
0644
edit
dl
rm
internet.html
31087
0644
edit
dl
rm
intro.html
9904
0644
edit
dl
rm
io.html
122685
0644
edit
dl
rm
ipaddress.html
105873
0644
edit
dl
rm
ipc.html
33743
0644
edit
dl
rm
itertools.html
132824
0644
edit
dl
rm
json.html
81078
0644
edit
dl
rm
keyword.html
8918
0644
edit
dl
rm
language.html
13648
0644
edit
dl
rm
linecache.html
13492
0644
edit
dl
rm
locale.html
61540
0644
edit
dl
rm
logging.config.html
85489
0644
edit
dl
rm
logging.handlers.html
112397
0644
edit
dl
rm
logging.html
134850
0644
edit
dl
rm
lzma.html
56484
0644
edit
dl
rm
macpath.html
9257
0644
edit
dl
rm
mailbox.html
157315
0644
edit
dl
rm
mailcap.html
14676
0644
edit
dl
rm
markup.html
19356
0644
edit
dl
rm
marshal.html
18317
0644
edit
dl
rm
math.html
53661
0644
edit
dl
rm
mimetypes.html
31410
0644
edit
dl
rm
misc.html
8123
0644
edit
dl
rm
mm.html
10022
0644
edit
dl
rm
mmap.html
34294
0644
edit
dl
rm
modulefinder.html
18559
0644
edit
dl
rm
modules.html
10914
0644
edit
dl
rm
msilib.html
57205
0644
edit
dl
rm
msvcrt.html
21989
0644
edit
dl
rm
multiprocessing.html
365487
0644
edit
dl
rm
netdata.html
17470
0644
edit
dl
rm
netrc.html
14568
0644
edit
dl
rm
nis.html
11659
0644
edit
dl
rm
nntplib.html
69601
0644
edit
dl
rm
numbers.html
41432
0644
edit
dl
rm
numeric.html
15535
0644
edit
dl
rm
operator.html
81460
0644
edit
dl
rm
optparse.html
249942
0644
edit
dl
rm
os.html
398039
0644
edit
dl
rm
os.path.html
53818
0644
edit
dl
rm
ossaudiodev.html
47947
0644
edit
dl
rm
othergui.html
10256
0644
edit
dl
rm
parser.html
42408
0644
edit
dl
rm
pathlib.html
116726
0644
edit
dl
rm
pdb.html
52661
0644
edit
dl
rm
persistence.html
15281
0644
edit
dl
rm
pickle.html
115052
0644
edit
dl
rm
pickletools.html
17094
0644
edit
dl
rm
pipes.html
15991
0644
edit
dl
rm
pkgutil.html
31878
0644
edit
dl
rm
platform.html
33728
0644
edit
dl
rm
plistlib.html
29290
0644
edit
dl
rm
poplib.html
30940
0644
edit
dl
rm
posix.html
17608
0644
edit
dl
rm
pprint.html
41969
0644
edit
dl
rm
profile.html
71848
0644
edit
dl
rm
pty.html
17594
0644
edit
dl
rm
pwd.html
13098
0644
edit
dl
rm
pyclbr.html
16520
0644
edit
dl
rm
pydoc.html
15991
0644
edit
dl
rm
pyexpat.html
88938
0644
edit
dl
rm
python.html
17133
0644
edit
dl
rm
py_compile.html
15508
0644
edit
dl
rm
queue.html
28199
0644
edit
dl
rm
quopri.html
13858
0644
edit
dl
rm
random.html
60646
0644
edit
dl
rm
re.html
191176
0644
edit
dl
rm
readline.html
42133
0644
edit
dl
rm
reprlib.html
24964
0644
edit
dl
rm
resource.html
36552
0644
edit
dl
rm
rlcompleter.html
14392
0644
edit
dl
rm
runpy.html
26607
0644
edit
dl
rm
sched.html
20034
0644
edit
dl
rm
secrets.html
24734
0644
edit
dl
rm
select.html
62819
0644
edit
dl
rm
selectors.html
35845
0644
edit
dl
rm
shelve.html
28777
0644
edit
dl
rm
shlex.html
48979
0644
edit
dl
rm
shutil.html
74207
0644
edit
dl
rm
signal.html
55064
0644
edit
dl
rm
site.html
32242
0644
edit
dl
rm
smtpd.html
35892
0644
edit
dl
rm
smtplib.html
68433
0644
edit
dl
rm
sndhdr.html
12439
0644
edit
dl
rm
socket.html
196077
0644
edit
dl
rm
socketserver.html
81918
0644
edit
dl
rm
spwd.html
12328
0644
edit
dl
rm
sqlite3.html
161872
0644
edit
dl
rm
ssl.html
266327
0644
edit
dl
rm
stat.html
44722
0644
edit
dl
rm
statistics.html
50050
0644
edit
dl
rm
stdtypes.html
465658
0644
edit
dl
rm
string.html
95262
0644
edit
dl
rm
stringprep.html
18654
0644
edit
dl
rm
struct.html
51100
0644
edit
dl
rm
subprocess.html
141926
0644
edit
dl
rm
sunau.html
31757
0644
edit
dl
rm
superseded.html
13781
0644
edit
dl
rm
symbol.html
8848
0644
edit
dl
rm
symtable.html
26112
0644
edit
dl
rm
sys.html
141670
0644
edit
dl
rm
sysconfig.html
30259
0644
edit
dl
rm
syslog.html
21026
0644
edit
dl
rm
tabnanny.html
12269
0644
edit
dl
rm
tarfile.html
96758
0644
edit
dl
rm
telnetlib.html
30409
0644
edit
dl
rm
tempfile.html
41394
0644
edit
dl
rm
termios.html
18181
0644
edit
dl
rm
test.html
78555
0644
edit
dl
rm
text.html
13786
0644
edit
dl
rm
textwrap.html
37998
0644
edit
dl
rm
threading.html
103464
0644
edit
dl
rm
time.html
79629
0644
edit
dl
rm
timeit.html
45249
0644
edit
dl
rm
tk.html
23299
0644
edit
dl
rm
tkinter.html
82932
0644
edit
dl
rm
tkinter.scrolledtext.html
9957
0644
edit
dl
rm
tkinter.tix.html
51858
0644
edit
dl
rm
tkinter.ttk.html
110673
0644
edit
dl
rm
token.html
22660
0644
edit
dl
rm
tokenize.html
32149
0644
edit
dl
rm
trace.html
29145
0644
edit
dl
rm
traceback.html
57588
0644
edit
dl
rm
tracemalloc.html
101144
0644
edit
dl
rm
tty.html
10540
0644
edit
dl
rm
turtle.html
250367
0644
edit
dl
rm
types.html
37060
0644
edit
dl
rm
typing.html
139175
0644
edit
dl
rm
undoc.html
9181
0644
edit
dl
rm
unicodedata.html
21108
0644
edit
dl
rm
unittest.html
250689
0644
edit
dl
rm
unittest.mock-examples.html
168144
0644
edit
dl
rm
unittest.mock.html
300089
0644
edit
dl
rm
unix.html
11780
0644
edit
dl
rm
urllib.error.html
14474
0644
edit
dl
rm
urllib.html
9566
0644
edit
dl
rm
urllib.parse.html
76334
0644
edit
dl
rm
urllib.request.html
177199
0644
edit
dl
rm
urllib.robotparser.html
17156
0644
edit
dl
rm
uu.html
12209
0644
edit
dl
rm
uuid.html
31957
0644
edit
dl
rm
venv.html
73909
0644
edit
dl
rm
warnings.html
50320
0644
edit
dl
rm
wave.html
29868
0644
edit
dl
rm
weakref.html
65292
0644
edit
dl
rm
webbrowser.html
26901
0644
edit
dl
rm
windows.html
10626
0644
edit
dl
rm
winreg.html
66730
0644
edit
dl
rm
winsound.html
21268
0644
edit
dl
rm
wsgiref.html
94492
0644
edit
dl
rm
xdrlib.html
33751
0644
edit
dl
rm
xml.dom.html
97570
0644
edit
dl
rm
xml.dom.minidom.html
41481
0644
edit
dl
rm
xml.dom.pulldom.html
24214
0644
edit
dl
rm
xml.etree.elementtree.html
135491
0644
edit
dl
rm
xml.html
19261
0644
edit
dl
rm
xml.sax.handler.html
42659
0644
edit
dl
rm
xml.sax.html
25515
0644
edit
dl
rm
xml.sax.reader.html
45869
0644
edit
dl
rm
xml.sax.utils.html
16928
0644
edit
dl
rm
xmlrpc.client.html
71947
0644
edit
dl
rm
xmlrpc.html
8535
0644
edit
dl
rm
xmlrpc.server.html
49195
0644
edit
dl
rm
zipapp.html
45105
0644
edit
dl
rm
zipfile.html
80039
0644
edit
dl
rm
zipimport.html
23052
0644
edit
dl
rm
zlib.html
34557
0644
edit
dl
rm
_dummy_thread.html
9193
0644
edit
dl
rm
_thread.html
22588
0644
edit
dl
rm
__future__.html
15792
0644
edit
dl
rm
__main__.html
9019
0644
edit
dl
rm
Edit:
/usr/share/doc/python3-docs/html/library/smtplib.html
(68433B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>21.17. smtplib — SMTP protocol client — Python 3.6.7 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.6.7 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="21.18. smtpd — SMTP Server" href="smtpd.html" /> <link rel="prev" title="21.16. nntplib — NNTP protocol client" href="nntplib.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/3/library/smtplib.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="smtpd.html" title="21.18. smtpd — SMTP Server" accesskey="N">next</a> |</li> <li class="right" > <a href="nntplib.html" title="21.16. nntplib — NNTP protocol client" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-smtplib"> <span id="smtplib-smtp-protocol-client"></span><h1>21.17. <a class="reference internal" href="#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> — SMTP protocol client<a class="headerlink" href="#module-smtplib" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/smtplib.py">Lib/smtplib.py</a></p> <hr class="docutils" id="index-0" /> <p>The <a class="reference internal" href="#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc821.html"><strong>RFC 821</strong></a> (Simple Mail Transfer Protocol) and <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1869.html"><strong>RFC 1869</strong></a> (SMTP Service Extensions).</p> <dl class="class"> <dt id="smtplib.SMTP"> <em class="property">class </em><code class="descclassname">smtplib.</code><code class="descname">SMTP</code><span class="sig-paren">(</span><em>host=''</em>, <em>port=0</em>, <em>local_hostname=None</em>, <span class="optional">[</span><em>timeout</em>, <span class="optional">]</span><em>source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP" title="Permalink to this definition">¶</a></dt> <dd><p>An <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the optional host and port parameters are given, the SMTP <a class="reference internal" href="#smtplib.SMTP.connect" title="smtplib.SMTP.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> method is called with those parameters during initialization. If specified, <em>local_hostname</em> is used as the FQDN of the local host in the HELO/EHLO command. Otherwise, the local hostname is found using <a class="reference internal" href="socket.html#socket.getfqdn" title="socket.getfqdn"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.getfqdn()</span></code></a>. If the <a class="reference internal" href="#smtplib.SMTP.connect" title="smtplib.SMTP.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> call returns anything other than a success code, an <a class="reference internal" href="#smtplib.SMTPConnectError" title="smtplib.SMTPConnectError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPConnectError</span></code></a> is raised. The optional <em>timeout</em> parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). If the timeout expires, <a class="reference internal" href="socket.html#socket.timeout" title="socket.timeout"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.timeout</span></code></a> is raised. The optional source_address parameter allows binding to some specific source address in a machine with multiple network interfaces, and/or to some specific source TCP port. It takes a 2-tuple (host, port), for the socket to bind to as its source address before connecting. If omitted (or if host or port are <code class="docutils literal notranslate"><span class="pre">''</span></code> and/or 0 respectively) the OS default behavior will be used.</p> <p>For normal use, you should only require the initialization/connect, <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">quit()</span></code> methods. An example is included below.</p> <p>The <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> class supports the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. When used like this, the SMTP <code class="docutils literal notranslate"><span class="pre">QUIT</span></code> command is issued automatically when the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement exits. E.g.:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">smtplib</span> <span class="k">import</span> <span class="n">SMTP</span> <span class="gp">>>> </span><span class="k">with</span> <span class="n">SMTP</span><span class="p">(</span><span class="s2">"domain.org"</span><span class="p">)</span> <span class="k">as</span> <span class="n">smtp</span><span class="p">:</span> <span class="gp">... </span> <span class="n">smtp</span><span class="o">.</span><span class="n">noop</span><span class="p">()</span> <span class="gp">...</span> <span class="go">(250, b'Ok')</span> <span class="go">>>></span> </pre></div> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>Support for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement was added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>source_address argument was added.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5: </span>The SMTPUTF8 extension (<span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6531.html"><strong>RFC 6531</strong></a>) is now supported.</p> </div> </dd></dl> <dl class="class"> <dt id="smtplib.SMTP_SSL"> <em class="property">class </em><code class="descclassname">smtplib.</code><code class="descname">SMTP_SSL</code><span class="sig-paren">(</span><em>host=''</em>, <em>port=0</em>, <em>local_hostname=None</em>, <em>keyfile=None</em>, <em>certfile=None</em>, <span class="optional">[</span><em>timeout</em>, <span class="optional">]</span><em>context=None</em>, <em>source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP_SSL" title="Permalink to this definition">¶</a></dt> <dd><p>An <a class="reference internal" href="#smtplib.SMTP_SSL" title="smtplib.SMTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP_SSL</span></code></a> instance behaves exactly the same as instances of <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a>. <a class="reference internal" href="#smtplib.SMTP_SSL" title="smtplib.SMTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP_SSL</span></code></a> should be used for situations where SSL is required from the beginning of the connection and using <code class="xref py py-meth docutils literal notranslate"><span class="pre">starttls()</span></code> is not appropriate. If <em>host</em> is not specified, the local host is used. If <em>port</em> is zero, the standard SMTP-over-SSL port (465) is used. The optional arguments <em>local_hostname</em>, <em>timeout</em> and <em>source_address</em> have the same meaning as they do in the <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> class. <em>context</em>, also optional, can contain a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> and allows configuring various aspects of the secure connection. Please read <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">Security considerations</span></a> for best practices.</p> <p><em>keyfile</em> and <em>certfile</em> are a legacy alternative to <em>context</em>, and can point to a PEM formatted private key and certificate chain file for the SSL connection.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span><em>context</em> was added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>source_address argument was added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>The class now supports hostname check with <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> and <em>Server Name Indication</em> (see <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a>).</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.6: </span><em>keyfile</em> and <em>certfile</em> are deprecated in favor of <em>context</em>. Please use <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> instead, or let <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> select the system’s trusted CA certificates for you.</p> </div> </dd></dl> <dl class="class"> <dt id="smtplib.LMTP"> <em class="property">class </em><code class="descclassname">smtplib.</code><code class="descname">LMTP</code><span class="sig-paren">(</span><em>host=''</em>, <em>port=LMTP_PORT</em>, <em>local_hostname=None</em>, <em>source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.LMTP" title="Permalink to this definition">¶</a></dt> <dd><p>The LMTP protocol, which is very similar to ESMTP, is heavily based on the standard SMTP client. It’s common to use Unix sockets for LMTP, so our <code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code> method must support that as well as a regular host:port server. The optional arguments local_hostname and source_address have the same meaning as they do in the <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> class. To specify a Unix socket, you must use an absolute path for <em>host</em>, starting with a ‘/’.</p> <p>Authentication is supported, using the regular SMTP mechanism. When using a Unix socket, LMTP generally don’t support or require any authentication, but your mileage might vary.</p> </dd></dl> <p>A nice selection of exceptions is defined as well:</p> <dl class="exception"> <dt id="smtplib.SMTPException"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPException</code><a class="headerlink" href="#smtplib.SMTPException" title="Permalink to this definition">¶</a></dt> <dd><p>Subclass of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> that is the base exception class for all the other exceptions provided by this module.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>SMTPException became subclass of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a></p> </div> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPServerDisconnected"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPServerDisconnected</code><a class="headerlink" href="#smtplib.SMTPServerDisconnected" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is raised when the server unexpectedly disconnects, or when an attempt is made to use the <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> instance before connecting it to a server.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPResponseException"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPResponseException</code><a class="headerlink" href="#smtplib.SMTPResponseException" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for all exceptions that include an SMTP error code. These exceptions are generated in some instances when the SMTP server returns an error code. The error code is stored in the <code class="xref py py-attr docutils literal notranslate"><span class="pre">smtp_code</span></code> attribute of the error, and the <code class="xref py py-attr docutils literal notranslate"><span class="pre">smtp_error</span></code> attribute is set to the error message.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPSenderRefused"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPSenderRefused</code><a class="headerlink" href="#smtplib.SMTPSenderRefused" title="Permalink to this definition">¶</a></dt> <dd><p>Sender address refused. In addition to the attributes set by on all <a class="reference internal" href="#smtplib.SMTPResponseException" title="smtplib.SMTPResponseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPResponseException</span></code></a> exceptions, this sets ‘sender’ to the string that the SMTP server refused.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPRecipientsRefused"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPRecipientsRefused</code><a class="headerlink" href="#smtplib.SMTPRecipientsRefused" title="Permalink to this definition">¶</a></dt> <dd><p>All recipient addresses refused. The errors for each recipient are accessible through the attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">recipients</span></code>, which is a dictionary of exactly the same sort as <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTP.sendmail()</span></code></a> returns.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPDataError"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPDataError</code><a class="headerlink" href="#smtplib.SMTPDataError" title="Permalink to this definition">¶</a></dt> <dd><p>The SMTP server refused to accept the message data.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPConnectError"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPConnectError</code><a class="headerlink" href="#smtplib.SMTPConnectError" title="Permalink to this definition">¶</a></dt> <dd><p>Error occurred during establishment of a connection with the server.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPHeloError"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPHeloError</code><a class="headerlink" href="#smtplib.SMTPHeloError" title="Permalink to this definition">¶</a></dt> <dd><p>The server refused our <code class="docutils literal notranslate"><span class="pre">HELO</span></code> message.</p> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPNotSupportedError"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPNotSupportedError</code><a class="headerlink" href="#smtplib.SMTPNotSupportedError" title="Permalink to this definition">¶</a></dt> <dd><p>The command or option attempted is not supported by the server.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="exception"> <dt id="smtplib.SMTPAuthenticationError"> <em class="property">exception </em><code class="descclassname">smtplib.</code><code class="descname">SMTPAuthenticationError</code><a class="headerlink" href="#smtplib.SMTPAuthenticationError" title="Permalink to this definition">¶</a></dt> <dd><p>SMTP authentication went wrong. Most probably the server didn’t accept the username/password combination provided.</p> </dd></dl> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc821.html"><strong>RFC 821</strong></a> - Simple Mail Transfer Protocol</dt> <dd>Protocol definition for SMTP. This document covers the model, operating procedure, and protocol details for SMTP.</dd> <dt><span class="target" id="index-5"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1869.html"><strong>RFC 1869</strong></a> - SMTP Service Extensions</dt> <dd>Definition of the ESMTP extensions for SMTP. This describes a framework for extending SMTP with new commands, supporting dynamic discovery of the commands provided by the server, and defines a few additional commands.</dd> </dl> </div> <div class="section" id="smtp-objects"> <span id="id1"></span><h2>21.17.1. SMTP Objects<a class="headerlink" href="#smtp-objects" title="Permalink to this headline">¶</a></h2> <p>An <a class="reference internal" href="#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> instance has the following methods:</p> <dl class="method"> <dt id="smtplib.SMTP.set_debuglevel"> <code class="descclassname">SMTP.</code><code class="descname">set_debuglevel</code><span class="sig-paren">(</span><em>level</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.set_debuglevel" title="Permalink to this definition">¶</a></dt> <dd><p>Set the debug output level. A value of 1 or <code class="docutils literal notranslate"><span class="pre">True</span></code> for <em>level</em> results in debug messages for connection and for all messages sent to and received from the server. A value of 2 for <em>level</em> results in these messages being timestamped.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Added debuglevel 2.</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.docmd"> <code class="descclassname">SMTP.</code><code class="descname">docmd</code><span class="sig-paren">(</span><em>cmd</em>, <em>args=''</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.docmd" title="Permalink to this definition">¶</a></dt> <dd><p>Send a command <em>cmd</em> to the server. The optional argument <em>args</em> is simply concatenated to the command, separated by a space.</p> <p>This returns a 2-tuple composed of a numeric response code and the actual response line (multiline responses are joined into one long line.)</p> <p>In normal operation it should not be necessary to call this method explicitly. It is used to implement other methods and may be useful for testing private extensions.</p> <p>If the connection to the server is lost while waiting for the reply, <a class="reference internal" href="#smtplib.SMTPServerDisconnected" title="smtplib.SMTPServerDisconnected"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPServerDisconnected</span></code></a> will be raised.</p> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.connect"> <code class="descclassname">SMTP.</code><code class="descname">connect</code><span class="sig-paren">(</span><em>host='localhost'</em>, <em>port=0</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.connect" title="Permalink to this definition">¶</a></dt> <dd><p>Connect to a host on a given port. The defaults are to connect to the local host at the standard SMTP port (25). If the hostname ends with a colon (<code class="docutils literal notranslate"><span class="pre">':'</span></code>) followed by a number, that suffix will be stripped off and the number interpreted as the port number to use. This method is automatically invoked by the constructor if a host is specified during instantiation. Returns a 2-tuple of the response code and message sent by the server in its connection response.</p> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.helo"> <code class="descclassname">SMTP.</code><code class="descname">helo</code><span class="sig-paren">(</span><em>name=''</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.helo" title="Permalink to this definition">¶</a></dt> <dd><p>Identify yourself to the SMTP server using <code class="docutils literal notranslate"><span class="pre">HELO</span></code>. The hostname argument defaults to the fully qualified domain name of the local host. The message returned by the server is stored as the <code class="xref py py-attr docutils literal notranslate"><span class="pre">helo_resp</span></code> attribute of the object.</p> <p>In normal operation it should not be necessary to call this method explicitly. It will be implicitly called by the <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> when necessary.</p> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.ehlo"> <code class="descclassname">SMTP.</code><code class="descname">ehlo</code><span class="sig-paren">(</span><em>name=''</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.ehlo" title="Permalink to this definition">¶</a></dt> <dd><p>Identify yourself to an ESMTP server using <code class="docutils literal notranslate"><span class="pre">EHLO</span></code>. The hostname argument defaults to the fully qualified domain name of the local host. Examine the response for ESMTP option and store them for use by <a class="reference internal" href="#smtplib.SMTP.has_extn" title="smtplib.SMTP.has_extn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_extn()</span></code></a>. Also sets several informational attributes: the message returned by the server is stored as the <code class="xref py py-attr docutils literal notranslate"><span class="pre">ehlo_resp</span></code> attribute, <code class="xref py py-attr docutils literal notranslate"><span class="pre">does_esmtp</span></code> is set to true or false depending on whether the server supports ESMTP, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">esmtp_features</span></code> will be a dictionary containing the names of the SMTP service extensions this server supports, and their parameters (if any).</p> <p>Unless you wish to use <a class="reference internal" href="#smtplib.SMTP.has_extn" title="smtplib.SMTP.has_extn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_extn()</span></code></a> before sending mail, it should not be necessary to call this method explicitly. It will be implicitly called by <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> when necessary.</p> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.ehlo_or_helo_if_needed"> <code class="descclassname">SMTP.</code><code class="descname">ehlo_or_helo_if_needed</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.ehlo_or_helo_if_needed" title="Permalink to this definition">¶</a></dt> <dd><p>This method calls <a class="reference internal" href="#smtplib.SMTP.ehlo" title="smtplib.SMTP.ehlo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ehlo()</span></code></a> and/or <a class="reference internal" href="#smtplib.SMTP.helo" title="smtplib.SMTP.helo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">helo()</span></code></a> if there has been no previous <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> or <code class="docutils literal notranslate"><span class="pre">HELO</span></code> command this session. It tries ESMTP <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> first.</p> <dl class="docutils"> <dt><a class="reference internal" href="#smtplib.SMTPHeloError" title="smtplib.SMTPHeloError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPHeloError</span></code></a></dt> <dd>The server didn’t reply properly to the <code class="docutils literal notranslate"><span class="pre">HELO</span></code> greeting.</dd> </dl> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.has_extn"> <code class="descclassname">SMTP.</code><code class="descname">has_extn</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.has_extn" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if <em>name</em> is in the set of SMTP service extensions returned by the server, <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> otherwise. Case is ignored.</p> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.verify"> <code class="descclassname">SMTP.</code><code class="descname">verify</code><span class="sig-paren">(</span><em>address</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.verify" title="Permalink to this definition">¶</a></dt> <dd><p>Check the validity of an address on this server using SMTP <code class="docutils literal notranslate"><span class="pre">VRFY</span></code>. Returns a tuple consisting of code 250 and a full <span class="target" id="index-6"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> address (including human name) if the user address is valid. Otherwise returns an SMTP error code of 400 or greater and an error string.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Many sites disable SMTP <code class="docutils literal notranslate"><span class="pre">VRFY</span></code> in order to foil spammers.</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.login"> <code class="descclassname">SMTP.</code><code class="descname">login</code><span class="sig-paren">(</span><em>user</em>, <em>password</em>, <em>*</em>, <em>initial_response_ok=True</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.login" title="Permalink to this definition">¶</a></dt> <dd><p>Log in on an SMTP server that requires authentication. The arguments are the username and the password to authenticate with. If there has been no previous <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> or <code class="docutils literal notranslate"><span class="pre">HELO</span></code> command this session, this method tries ESMTP <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> first. This method will return normally if the authentication was successful, or may raise the following exceptions:</p> <dl class="docutils"> <dt><a class="reference internal" href="#smtplib.SMTPHeloError" title="smtplib.SMTPHeloError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPHeloError</span></code></a></dt> <dd>The server didn’t reply properly to the <code class="docutils literal notranslate"><span class="pre">HELO</span></code> greeting.</dd> <dt><a class="reference internal" href="#smtplib.SMTPAuthenticationError" title="smtplib.SMTPAuthenticationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPAuthenticationError</span></code></a></dt> <dd>The server didn’t accept the username/password combination.</dd> <dt><a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a></dt> <dd>The <code class="docutils literal notranslate"><span class="pre">AUTH</span></code> command is not supported by the server.</dd> <dt><a class="reference internal" href="#smtplib.SMTPException" title="smtplib.SMTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPException</span></code></a></dt> <dd>No suitable authentication method was found.</dd> </dl> <p>Each of the authentication methods supported by <a class="reference internal" href="#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> are tried in turn if they are advertised as supported by the server. See <a class="reference internal" href="#smtplib.SMTP.auth" title="smtplib.SMTP.auth"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auth()</span></code></a> for a list of supported authentication methods. <em>initial_response_ok</em> is passed through to <a class="reference internal" href="#smtplib.SMTP.auth" title="smtplib.SMTP.auth"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auth()</span></code></a>.</p> <p>Optional keyword argument <em>initial_response_ok</em> specifies whether, for authentication methods that support it, an “initial response” as specified in <span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4954.html"><strong>RFC 4954</strong></a> can be sent along with the <code class="docutils literal notranslate"><span class="pre">AUTH</span></code> command, rather than requiring a challenge/response.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span><a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a> may be raised, and the <em>initial_response_ok</em> parameter was added.</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.auth"> <code class="descclassname">SMTP.</code><code class="descname">auth</code><span class="sig-paren">(</span><em>mechanism</em>, <em>authobject</em>, <em>*</em>, <em>initial_response_ok=True</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.auth" title="Permalink to this definition">¶</a></dt> <dd><p>Issue an <code class="docutils literal notranslate"><span class="pre">SMTP</span></code> <code class="docutils literal notranslate"><span class="pre">AUTH</span></code> command for the specified authentication <em>mechanism</em>, and handle the challenge response via <em>authobject</em>.</p> <p><em>mechanism</em> specifies which authentication mechanism is to be used as argument to the <code class="docutils literal notranslate"><span class="pre">AUTH</span></code> command; the valid values are those listed in the <code class="docutils literal notranslate"><span class="pre">auth</span></code> element of <code class="xref py py-attr docutils literal notranslate"><span class="pre">esmtp_features</span></code>.</p> <p><em>authobject</em> must be a callable object taking an optional single argument:</p> <blockquote> <div>data = authobject(challenge=None)</div></blockquote> <p>If optional keyword argument <em>initial_response_ok</em> is true, <code class="docutils literal notranslate"><span class="pre">authobject()</span></code> will be called first with no argument. It can return the <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4954.html"><strong>RFC 4954</strong></a> “initial response” ASCII <code class="docutils literal notranslate"><span class="pre">str</span></code> which will be encoded and sent with the <code class="docutils literal notranslate"><span class="pre">AUTH</span></code> command as below. If the <code class="docutils literal notranslate"><span class="pre">authobject()</span></code> does not support an initial response (e.g. because it requires a challenge), it should return <code class="docutils literal notranslate"><span class="pre">None</span></code> when called with <code class="docutils literal notranslate"><span class="pre">challenge=None</span></code>. If <em>initial_response_ok</em> is false, then <code class="docutils literal notranslate"><span class="pre">authobject()</span></code> will not be called first with <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <p>If the initial response check returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, or if <em>initial_response_ok</em> is false, <code class="docutils literal notranslate"><span class="pre">authobject()</span></code> will be called to process the server’s challenge response; the <em>challenge</em> argument it is passed will be a <code class="docutils literal notranslate"><span class="pre">bytes</span></code>. It should return ASCII <code class="docutils literal notranslate"><span class="pre">str</span></code> <em>data</em> that will be base64 encoded and sent to the server.</p> <p>The <code class="docutils literal notranslate"><span class="pre">SMTP</span></code> class provides <code class="docutils literal notranslate"><span class="pre">authobjects</span></code> for the <code class="docutils literal notranslate"><span class="pre">CRAM-MD5</span></code>, <code class="docutils literal notranslate"><span class="pre">PLAIN</span></code>, and <code class="docutils literal notranslate"><span class="pre">LOGIN</span></code> mechanisms; they are named <code class="docutils literal notranslate"><span class="pre">SMTP.auth_cram_md5</span></code>, <code class="docutils literal notranslate"><span class="pre">SMTP.auth_plain</span></code>, and <code class="docutils literal notranslate"><span class="pre">SMTP.auth_login</span></code> respectively. They all require that the <code class="docutils literal notranslate"><span class="pre">user</span></code> and <code class="docutils literal notranslate"><span class="pre">password</span></code> properties of the <code class="docutils literal notranslate"><span class="pre">SMTP</span></code> instance are set to appropriate values.</p> <p>User code does not normally need to call <code class="docutils literal notranslate"><span class="pre">auth</span></code> directly, but can instead call the <a class="reference internal" href="#smtplib.SMTP.login" title="smtplib.SMTP.login"><code class="xref py py-meth docutils literal notranslate"><span class="pre">login()</span></code></a> method, which will try each of the above mechanisms in turn, in the order listed. <code class="docutils literal notranslate"><span class="pre">auth</span></code> is exposed to facilitate the implementation of authentication methods not (or not yet) supported directly by <a class="reference internal" href="#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.starttls"> <code class="descclassname">SMTP.</code><code class="descname">starttls</code><span class="sig-paren">(</span><em>keyfile=None</em>, <em>certfile=None</em>, <em>context=None</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.starttls" title="Permalink to this definition">¶</a></dt> <dd><p>Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted. You should then call <a class="reference internal" href="#smtplib.SMTP.ehlo" title="smtplib.SMTP.ehlo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ehlo()</span></code></a> again.</p> <p>If <em>keyfile</em> and <em>certfile</em> are provided, they are used to create an <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a>.</p> <p>Optional <em>context</em> parameter is an <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object; This is an alternative to using a keyfile and a certfile and if specified both <em>keyfile</em> and <em>certfile</em> should be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <p>If there has been no previous <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> or <code class="docutils literal notranslate"><span class="pre">HELO</span></code> command this session, this method tries ESMTP <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> first.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.6: </span><em>keyfile</em> and <em>certfile</em> are deprecated in favor of <em>context</em>. Please use <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> instead, or let <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> select the system’s trusted CA certificates for you.</p> </div> <dl class="docutils"> <dt><a class="reference internal" href="#smtplib.SMTPHeloError" title="smtplib.SMTPHeloError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPHeloError</span></code></a></dt> <dd>The server didn’t reply properly to the <code class="docutils literal notranslate"><span class="pre">HELO</span></code> greeting.</dd> <dt><a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a></dt> <dd>The server does not support the STARTTLS extension.</dd> <dt><a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a></dt> <dd>SSL/TLS support is not available to your Python interpreter.</dd> </dl> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span><em>context</em> was added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>The method now supports hostname check with <code class="xref py py-attr docutils literal notranslate"><span class="pre">SSLContext.check_hostname</span></code> and <em>Server Name Indicator</em> (see <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">HAS_SNI</span></code></a>).</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>The error raised for lack of STARTTLS support is now the <a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a> subclass instead of the base <a class="reference internal" href="#smtplib.SMTPException" title="smtplib.SMTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPException</span></code></a>.</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.sendmail"> <code class="descclassname">SMTP.</code><code class="descname">sendmail</code><span class="sig-paren">(</span><em>from_addr</em>, <em>to_addrs</em>, <em>msg</em>, <em>mail_options=()</em>, <em>rcpt_options=()</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.sendmail" title="Permalink to this definition">¶</a></dt> <dd><p>Send mail. The required arguments are an <span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> from-address string, a list of <span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> to-address strings (a bare string will be treated as a list with 1 address), and a message string. The caller may pass a list of ESMTP options (such as <code class="docutils literal notranslate"><span class="pre">8bitmime</span></code>) to be used in <code class="docutils literal notranslate"><span class="pre">MAIL</span> <span class="pre">FROM</span></code> commands as <em>mail_options</em>. ESMTP options (such as <code class="docutils literal notranslate"><span class="pre">DSN</span></code> commands) that should be used with all <code class="docutils literal notranslate"><span class="pre">RCPT</span></code> commands can be passed as <em>rcpt_options</em>. (If you need to use different ESMTP options to different recipients you have to use the low-level methods such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">mail()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rcpt()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">data()</span></code> to send the message.)</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <em>from_addr</em> and <em>to_addrs</em> parameters are used to construct the message envelope used by the transport agents. <code class="docutils literal notranslate"><span class="pre">sendmail</span></code> does not modify the message headers in any way.</p> </div> <p><em>msg</em> may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone <code class="docutils literal notranslate"><span class="pre">\r</span></code> and <code class="docutils literal notranslate"><span class="pre">\n</span></code> characters are converted to <code class="docutils literal notranslate"><span class="pre">\r\n</span></code> characters. A byte string is not modified.</p> <p>If there has been no previous <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> or <code class="docutils literal notranslate"><span class="pre">HELO</span></code> command this session, this method tries ESMTP <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> first. If the server does ESMTP, message size and each of the specified options will be passed to it (if the option is in the feature set the server advertises). If <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> fails, <code class="docutils literal notranslate"><span class="pre">HELO</span></code> will be tried and ESMTP options suppressed.</p> <p>This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. That is, if this method does not raise an exception, then someone should get your mail. If this method does not raise an exception, it returns a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.</p> <p>If <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> is included in <em>mail_options</em>, and the server supports it, <em>from_addr</em> and <em>to_addrs</em> may contain non-ASCII characters.</p> <p>This method may raise the following exceptions:</p> <dl class="docutils"> <dt><a class="reference internal" href="#smtplib.SMTPRecipientsRefused" title="smtplib.SMTPRecipientsRefused"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPRecipientsRefused</span></code></a></dt> <dd>All recipients were refused. Nobody got the mail. The <code class="xref py py-attr docutils literal notranslate"><span class="pre">recipients</span></code> attribute of the exception object is a dictionary with information about the refused recipients (like the one returned when at least one recipient was accepted).</dd> <dt><a class="reference internal" href="#smtplib.SMTPHeloError" title="smtplib.SMTPHeloError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPHeloError</span></code></a></dt> <dd>The server didn’t reply properly to the <code class="docutils literal notranslate"><span class="pre">HELO</span></code> greeting.</dd> <dt><a class="reference internal" href="#smtplib.SMTPSenderRefused" title="smtplib.SMTPSenderRefused"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPSenderRefused</span></code></a></dt> <dd>The server didn’t accept the <em>from_addr</em>.</dd> <dt><a class="reference internal" href="#smtplib.SMTPDataError" title="smtplib.SMTPDataError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPDataError</span></code></a></dt> <dd>The server replied with an unexpected error code (other than a refusal of a recipient).</dd> <dt><a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a></dt> <dd><code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> was given in the <em>mail_options</em> but is not supported by the server.</dd> </dl> <p>Unless otherwise noted, the connection will be open even after an exception is raised.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span><em>msg</em> may be a byte string.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span><code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> support added, and <a class="reference internal" href="#smtplib.SMTPNotSupportedError" title="smtplib.SMTPNotSupportedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupportedError</span></code></a> may be raised if <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> is specified but the server does not support it.</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.send_message"> <code class="descclassname">SMTP.</code><code class="descname">send_message</code><span class="sig-paren">(</span><em>msg</em>, <em>from_addr=None</em>, <em>to_addrs=None</em>, <em>mail_options=()</em>, <em>rcpt_options=()</em><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.send_message" title="Permalink to this definition">¶</a></dt> <dd><p>This is a convenience method for calling <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> with the message represented by an <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.Message</span></code></a> object. The arguments have the same meaning as for <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a>, except that <em>msg</em> is a <code class="docutils literal notranslate"><span class="pre">Message</span></code> object.</p> <p>If <em>from_addr</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or <em>to_addrs</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">send_message</span></code> fills those arguments with addresses extracted from the headers of <em>msg</em> as specified in <span class="target" id="index-11"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5322.html"><strong>RFC 5322</strong></a>: <em>from_addr</em> is set to the <em class="mailheader">Sender</em> field if it is present, and otherwise to the <em class="mailheader">From</em> field. <em>to_addrs</em> combines the values (if any) of the <em class="mailheader">To</em>, <em class="mailheader">Cc</em>, and <em class="mailheader">Bcc</em> fields from <em>msg</em>. If exactly one set of <em class="mailheader">Resent-*</em> headers appear in the message, the regular headers are ignored and the <em class="mailheader">Resent-*</em> headers are used instead. If the message contains more than one set of <em class="mailheader">Resent-*</em> headers, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised, since there is no way to unambiguously detect the most recent set of <em class="mailheader">Resent-</em> headers.</p> <p><code class="docutils literal notranslate"><span class="pre">send_message</span></code> serializes <em>msg</em> using <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> with <code class="docutils literal notranslate"><span class="pre">\r\n</span></code> as the <em>linesep</em>, and calls <a class="reference internal" href="#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> to transmit the resulting message. Regardless of the values of <em>from_addr</em> and <em>to_addrs</em>, <code class="docutils literal notranslate"><span class="pre">send_message</span></code> does not transmit any <em class="mailheader">Bcc</em> or <em class="mailheader">Resent-Bcc</em> headers that may appear in <em>msg</em>. If any of the addresses in <em>from_addr</em> and <em>to_addrs</em> contain non-ASCII characters and the server does not advertise <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> support, an <code class="xref py py-exc docutils literal notranslate"><span class="pre">SMTPNotSupported</span></code> error is raised. Otherwise the <code class="docutils literal notranslate"><span class="pre">Message</span></code> is serialized with a clone of its <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">policy</span></code></a> with the <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.utf8" title="email.policy.EmailPolicy.utf8"><code class="xref py py-attr docutils literal notranslate"><span class="pre">utf8</span></code></a> attribute set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, and <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> and <code class="docutils literal notranslate"><span class="pre">BODY=8BITMIME</span></code> are added to <em>mail_options</em>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.2.</span></p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5: </span>Support for internationalized addresses (<code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code>).</p> </div> </dd></dl> <dl class="method"> <dt id="smtplib.SMTP.quit"> <code class="descclassname">SMTP.</code><code class="descname">quit</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#smtplib.SMTP.quit" title="Permalink to this definition">¶</a></dt> <dd><p>Terminate the SMTP session and close the connection. Return the result of the SMTP <code class="docutils literal notranslate"><span class="pre">QUIT</span></code> command.</p> </dd></dl> <p>Low-level methods corresponding to the standard SMTP/ESMTP commands <code class="docutils literal notranslate"><span class="pre">HELP</span></code>, <code class="docutils literal notranslate"><span class="pre">RSET</span></code>, <code class="docutils literal notranslate"><span class="pre">NOOP</span></code>, <code class="docutils literal notranslate"><span class="pre">MAIL</span></code>, <code class="docutils literal notranslate"><span class="pre">RCPT</span></code>, and <code class="docutils literal notranslate"><span class="pre">DATA</span></code> are also supported. Normally these do not need to be called directly, so they are not documented here. For details, consult the module code.</p> </div> <div class="section" id="smtp-example"> <span id="id2"></span><h2>21.17.2. SMTP Example<a class="headerlink" href="#smtp-example" title="Permalink to this headline">¶</a></h2> <p>This example prompts the user for addresses needed in the message envelope (‘To’ and ‘From’ addresses), and the message to be delivered. Note that the headers to be included with the message must be included in the message as entered; this example doesn’t do any processing of the <span class="target" id="index-12"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> headers. In particular, the ‘To’ and ‘From’ addresses must be included in the message headers explicitly.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">smtplib</span> <span class="k">def</span> <span class="nf">prompt</span><span class="p">(</span><span class="n">prompt</span><span class="p">):</span> <span class="k">return</span> <span class="nb">input</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="n">fromaddr</span> <span class="o">=</span> <span class="n">prompt</span><span class="p">(</span><span class="s2">"From: "</span><span class="p">)</span> <span class="n">toaddrs</span> <span class="o">=</span> <span class="n">prompt</span><span class="p">(</span><span class="s2">"To: "</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Enter message, end with ^D (Unix) or ^Z (Windows):"</span><span class="p">)</span> <span class="c1"># Add the From: and To: headers at the start!</span> <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"From: </span><span class="si">%s</span><span class="se">\r\n</span><span class="s2">To: </span><span class="si">%s</span><span class="se">\r\n\r\n</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">fromaddr</span><span class="p">,</span> <span class="s2">", "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">toaddrs</span><span class="p">)))</span> <span class="k">while</span> <span class="kc">True</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span> <span class="n">line</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span> <span class="k">except</span> <span class="ne">EOFError</span><span class="p">:</span> <span class="k">break</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span> <span class="k">break</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">msg</span> <span class="o">+</span> <span class="n">line</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Message length is"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span> <span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span> <span class="n">server</span><span class="o">.</span><span class="n">set_debuglevel</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">fromaddr</span><span class="p">,</span> <span class="n">toaddrs</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">In general, you will want to use the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package’s features to construct an email message, which you can then send via <a class="reference internal" href="#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_message()</span></code></a>; see <a class="reference internal" href="email.examples.html#email-examples"><span class="std std-ref">email: Examples</span></a>.</p> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">21.17. <code class="docutils literal notranslate"><span class="pre">smtplib</span></code> — SMTP protocol client</a><ul> <li><a class="reference internal" href="#smtp-objects">21.17.1. SMTP Objects</a></li> <li><a class="reference internal" href="#smtp-example">21.17.2. SMTP Example</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="nntplib.html" title="previous chapter">21.16. <code class="docutils literal notranslate"><span class="pre">nntplib</span></code> — NNTP protocol client</a></p> <h4>Next topic</h4> <p class="topless"><a href="smtpd.html" title="next chapter">21.18. <code class="docutils literal notranslate"><span class="pre">smtpd</span></code> — SMTP Server</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.6/Doc/library/smtplib.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="smtpd.html" title="21.18. smtpd — SMTP Server" >next</a> |</li> <li class="right" > <a href="nntplib.html" title="21.16. nntplib — NNTP protocol client" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Dec 18, 2023. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run