blob: 962383f723b233e0ca95e52a65dce909d0edb8a6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{smtplib} ---
Fred Drake79936fe1999-04-22 17:23:34 +00002 SMTP protocol client}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake79936fe1999-04-22 17:23:34 +00004\declaremodule{standard}{smtplib}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{SMTP protocol client (requires sockets).}
Fred Drake79936fe1999-04-22 17:23:34 +00006\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake3240dd21998-07-01 14:10:52 +00008\indexii{SMTP}{protocol}
9\index{Simple Mail Transfer Protocol}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000010
Fred Drake3240dd21998-07-01 14:10:52 +000011The \module{smtplib} module defines an SMTP client session object that
12can be used to send mail to any Internet machine with an SMTP or ESMTP
Fred Drake5b683621998-06-30 16:53:52 +000013listener daemon. For details of SMTP and ESMTP operation, consult
Fred Drakeebe03411999-11-09 20:11:17 +000014\rfc{821} (\citetitle{Simple Mail Transfer Protocol}) and \rfc{1869}
15(\citetitle{SMTP Service Extensions}).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000016
Neil Schemenauerccbb0ed2002-03-24 15:41:40 +000017\begin{classdesc}{SMTP}{\optional{host\optional{, port\optional{,
18 local_hostname}}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000019A \class{SMTP} instance encapsulates an SMTP connection. It has
20methods that support a full repertoire of SMTP and ESMTP
21operations. If the optional host and port parameters are given, the
Fred Drake5b683621998-06-30 16:53:52 +000022SMTP \method{connect()} method is called with those parameters during
Guido van Rossum7969f311999-04-07 15:56:51 +000023initialization. An \exception{SMTPConnectError} is raised if the
24specified host doesn't respond correctly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000025
26For normal use, you should only require the initialization/connect,
Guido van Rossum7969f311999-04-07 15:56:51 +000027\method{sendmail()}, and \method{quit()} methods. An example is
Fred Drake5b683621998-06-30 16:53:52 +000028included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000029\end{classdesc}
30
Thomas Wouters89f507f2006-12-13 04:49:30 +000031\begin{classdesc}{SMTP_SSL}{\optional{host\optional{, port\optional{,
32 local_hostname\optional{,
33 keyfile\optional{,
34 certfile}}}}}}
35A \class{SMTP_SSL} instance behaves exactly the same as instances of \class{SMTP}.
36\class{SMTP_SSL} should be used for situations where SSL is required from
37the beginning of the connection and using \method{starttls()} is not appropriate.
38If \var{host} is not specified, the local host is used. If \var{port} is
39omitted, the standard SMTP-over-SSL port (465) is used. \var{keyfile} and \var{certfile}
40are also optional, and can contain a PEM formatted private key and
41certificate chain file for the SSL connection.
42\end{classdesc}
Fred Drake3240dd21998-07-01 14:10:52 +000043
Fred Drake79936fe1999-04-22 17:23:34 +000044A nice selection of exceptions is defined as well:
45
46\begin{excdesc}{SMTPException}
47 Base exception class for all exceptions raised by this module.
48\end{excdesc}
49
50\begin{excdesc}{SMTPServerDisconnected}
51 This exception is raised when the server unexpectedly disconnects,
52 or when an attempt is made to use the \class{SMTP} instance before
53 connecting it to a server.
54\end{excdesc}
55
56\begin{excdesc}{SMTPResponseException}
57 Base class for all exceptions that include an SMTP error code.
58 These exceptions are generated in some instances when the SMTP
59 server returns an error code. The error code is stored in the
60 \member{smtp_code} attribute of the error, and the
61 \member{smtp_error} attribute is set to the error message.
62\end{excdesc}
63
64\begin{excdesc}{SMTPSenderRefused}
Tim Peters9ca3f022003-06-15 23:08:45 +000065 Sender address refused. In addition to the attributes set by on all
Fred Drake79936fe1999-04-22 17:23:34 +000066 \exception{SMTPResponseException} exceptions, this sets `sender' to
67 the string that the SMTP server refused.
68\end{excdesc}
69
70\begin{excdesc}{SMTPRecipientsRefused}
71 All recipient addresses refused. The errors for each recipient are
Thomas Woutersf8316632000-07-16 19:01:10 +000072 accessible through the attribute \member{recipients}, which is a
Fred Drake79936fe1999-04-22 17:23:34 +000073 dictionary of exactly the same sort as \method{SMTP.sendmail()}
74 returns.
75\end{excdesc}
76
77\begin{excdesc}{SMTPDataError}
78 The SMTP server refused to accept the message data.
79\end{excdesc}
80
81\begin{excdesc}{SMTPConnectError}
82 Error occurred during establishment of a connection with the server.
83\end{excdesc}
84
85\begin{excdesc}{SMTPHeloError}
86 The server refused our \samp{HELO} message.
87\end{excdesc}
88
89
Fred Drake38e5d272000-04-03 20:13:55 +000090\begin{seealso}
Fred Drake58a2dff2000-10-03 05:56:55 +000091 \seerfc{821}{Simple Mail Transfer Protocol}{Protocol definition for
92 SMTP. This document covers the model, operating procedure,
93 and protocol details for SMTP.}
94 \seerfc{1869}{SMTP Service Extensions}{Definition of the ESMTP
95 extensions for SMTP. This describes a framework for
96 extending SMTP with new commands, supporting dynamic
97 discovery of the commands provided by the server, and
98 defines a few additional commands.}
Fred Drake38e5d272000-04-03 20:13:55 +000099\end{seealso}
100
101
Fred Drake79936fe1999-04-22 17:23:34 +0000102\subsection{SMTP Objects \label{SMTP-objects}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000103
Fred Drake5b683621998-06-30 16:53:52 +0000104An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000105
106\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +0000107Set the debug output level. A true value for \var{level} results in
108debug messages for connection and for all messages sent to and
109received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000110\end{methoddesc}
111
Fred Drake3240dd21998-07-01 14:10:52 +0000112\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
Tim Peters9ca3f022003-06-15 23:08:45 +0000113Connect to a host on a given port. The defaults are to connect to the
Fred Drake3240dd21998-07-01 14:10:52 +0000114local host at the standard SMTP port (25).
Fred Drake5b683621998-06-30 16:53:52 +0000115If the hostname ends with a colon (\character{:}) followed by a
Tim Peters9ca3f022003-06-15 23:08:45 +0000116number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000117the port number to use.
Fred Drake0aa811c2001-10-20 04:24:09 +0000118This method is automatically invoked by the constructor if a
Fred Drake5b683621998-06-30 16:53:52 +0000119host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000120\end{methoddesc}
121
122\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +0000123Send a command \var{cmd} to the server. The optional argument
124\var{argstring} is simply concatenated to the command, separated by a
125space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000126
Fred Drake5b683621998-06-30 16:53:52 +0000127This returns a 2-tuple composed of a numeric response code and the
128actual response line (multiline responses are joined into one long
129line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000130
131In normal operation it should not be necessary to call this method
132explicitly. It is used to implement other methods and may be useful
133for testing private extensions.
Guido van Rossum7969f311999-04-07 15:56:51 +0000134
Fred Drake79936fe1999-04-22 17:23:34 +0000135If the connection to the server is lost while waiting for the reply,
136\exception{SMTPServerDisconnected} will be raised.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000137\end{methoddesc}
138
139\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +0000140Identify yourself to the SMTP server using \samp{HELO}. The hostname
141argument defaults to the fully qualified domain name of the local
142host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000143
144In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +0000145explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000146when necessary.
147\end{methoddesc}
148
149\begin{methoddesc}{ehlo}{\optional{hostname}}
Guido van Rossum7969f311999-04-07 15:56:51 +0000150Identify yourself to an ESMTP server using \samp{EHLO}. The hostname
Fred Drake3240dd21998-07-01 14:10:52 +0000151argument defaults to the fully qualified domain name of the local
152host. Examine the response for ESMTP option and store them for use by
Greg Ward19b6f602003-01-08 03:04:42 +0000153\method{has_extn()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000154
Greg Ward19b6f602003-01-08 03:04:42 +0000155Unless you wish to use \method{has_extn()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000156mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +0000157will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000158\end{methoddesc}
159
Guido van Rossum7969f311999-04-07 15:56:51 +0000160\begin{methoddesc}{has_extn}{name}
Raymond Hettinger9b4dab42003-12-31 18:37:28 +0000161Return \constant{True} if \var{name} is in the set of SMTP service
162extensions returned by the server, \constant{False} otherwise.
163Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000164\end{methoddesc}
165
166\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +0000167Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +0000168Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000169(including human name) if the user address is valid. Otherwise returns
170an SMTP error code of 400 or greater and an error string.
171
Fred Drake0aa811c2001-10-20 04:24:09 +0000172\note{Many sites disable SMTP \samp{VRFY} in order to foil spammers.}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000173\end{methoddesc}
174
Fred Drakeaae8da12001-09-11 16:58:00 +0000175\begin{methoddesc}{login}{user, password}
176Log in on an SMTP server that requires authentication.
177The arguments are the username and the password to authenticate with.
178If there has been no previous \samp{EHLO} or \samp{HELO} command this
179session, this method tries ESMTP \samp{EHLO} first.
180This method will return normally if the authentication was successful,
181or may raise the following exceptions:
182
183\begin{description}
184 \item[\exception{SMTPHeloError}]
185 The server didn't reply properly to the \samp{HELO} greeting.
186 \item[\exception{SMTPAuthenticationError}]
187 The server didn't accept the username/password combination.
188 \item[\exception{SMTPError}]
189 No suitable authentication method was found.
190\end{description}
191\end{methoddesc}
192
Fred Drakef2a5f3f2001-09-14 17:48:41 +0000193\begin{methoddesc}{starttls}{\optional{keyfile\optional{, certfile}}}
194Put the SMTP connection in TLS (Transport Layer Security) mode. All
195SMTP commands that follow will be encrypted. You should then call
196\method{ehlo()} again.
Guido van Rossumf7fcf5e2001-09-14 16:08:44 +0000197
Fred Drakef2a5f3f2001-09-14 17:48:41 +0000198If \var{keyfile} and \var{certfile} are provided, these are passed to
199the \refmodule{socket} module's \function{ssl()} function.
Guido van Rossumf7fcf5e2001-09-14 16:08:44 +0000200\end{methoddesc}
201
Guido van Rossum7969f311999-04-07 15:56:51 +0000202\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{,
Fred Drake79936fe1999-04-22 17:23:34 +0000203 mail_options, rcpt_options}}
Fred Drake3240dd21998-07-01 14:10:52 +0000204Send mail. The required arguments are an \rfc{822} from-address
Georg Brandl9020a212005-06-25 18:24:03 +0000205string, a list of \rfc{822} to-address strings (a bare string will be
206treated as a list with 1 address), and a message string. The caller
207may pass a list of ESMTP options (such as \samp{8bitmime}) to be used
208in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP options
209(such as \samp{DSN} commands) that should be used with all \samp{RCPT}
210commands can be passed as \var{rcpt_options}. (If you need to use
211different ESMTP options to different recipients you have to use the
212low-level methods such as \method{mail}, \method{rcpt} and
Guido van Rossum7969f311999-04-07 15:56:51 +0000213\method{data} to send the message.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000214
Fred Drake0aa811c2001-10-20 04:24:09 +0000215\note{The \var{from_addr} and \var{to_addrs} parameters are
Fred Drake38e5d272000-04-03 20:13:55 +0000216used to construct the message envelope used by the transport agents.
Fred Drake0aa811c2001-10-20 04:24:09 +0000217The \class{SMTP} does not modify the message headers in any way.}
Fred Drake38e5d272000-04-03 20:13:55 +0000218
Fred Drake3240dd21998-07-01 14:10:52 +0000219If there has been no previous \samp{EHLO} or \samp{HELO} command this
220session, this method tries ESMTP \samp{EHLO} first. If the server does
221ESMTP, message size and each of the specified options will be passed
222to it (if the option is in the feature set the server advertises). If
223\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
224suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000225
Guido van Rossum7969f311999-04-07 15:56:51 +0000226This method will return normally if the mail is accepted for at least
227one recipient. Otherwise it will throw an exception. That is, if this
228method does not throw an exception, then someone should get your mail.
229If this method does not throw an exception, it returns a dictionary,
230with one entry for each recipient that was refused. Each entry
231contains a tuple of the SMTP error code and the accompanying error
232message sent by the server.
233
234This method may raise the following exceptions:
235
Fred Drake38e5d272000-04-03 20:13:55 +0000236\begin{description}
Fred Drake79936fe1999-04-22 17:23:34 +0000237\item[\exception{SMTPRecipientsRefused}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000238All recipients were refused. Nobody got the mail. The
Fred Drake38e5d272000-04-03 20:13:55 +0000239\member{recipients} attribute of the exception object is a dictionary
Guido van Rossum7969f311999-04-07 15:56:51 +0000240with information about the refused recipients (like the one returned
241when at least one recipient was accepted).
242
Fred Drake79936fe1999-04-22 17:23:34 +0000243\item[\exception{SMTPHeloError}]
Fred Drake38e5d272000-04-03 20:13:55 +0000244The server didn't reply properly to the \samp{HELO} greeting.
Guido van Rossum7969f311999-04-07 15:56:51 +0000245
Fred Drake79936fe1999-04-22 17:23:34 +0000246\item[\exception{SMTPSenderRefused}]
Fred Drake38e5d272000-04-03 20:13:55 +0000247The server didn't accept the \var{from_addr}.
Guido van Rossum7969f311999-04-07 15:56:51 +0000248
Fred Drake79936fe1999-04-22 17:23:34 +0000249\item[\exception{SMTPDataError}]
Fred Drake38e5d272000-04-03 20:13:55 +0000250The server replied with an unexpected error code (other than a refusal
251of a recipient).
252\end{description}
Guido van Rossum7969f311999-04-07 15:56:51 +0000253
Fred Drake38e5d272000-04-03 20:13:55 +0000254Unless otherwise noted, the connection will be open even after
Guido van Rossum7969f311999-04-07 15:56:51 +0000255an exception is raised.
256
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000257\end{methoddesc}
258
259\begin{methoddesc}{quit}{}
260Terminate the SMTP session and close the connection.
261\end{methoddesc}
262
263Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000264\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
265\samp{DATA} are also supported. Normally these do not need to be
266called directly, so they are not documented here. For details,
267consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000268
Fred Drake5b683621998-06-30 16:53:52 +0000269
Fred Drake60c3caf1998-08-07 16:03:32 +0000270\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000271
Fred Drake7be0cde1998-12-22 18:04:48 +0000272This example prompts the user for addresses needed in the message
Fred Drake38e5d272000-04-03 20:13:55 +0000273envelope (`To' and `From' addresses), and the message to be
Tim Peters9ca3f022003-06-15 23:08:45 +0000274delivered. Note that the headers to be included with the message must
Fred Drake7be0cde1998-12-22 18:04:48 +0000275be included in the message as entered; this example doesn't do any
276processing of the \rfc{822} headers. In particular, the `To' and
277`From' addresses must be included in the message headers explicitly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000278
279\begin{verbatim}
Fred Drakef6c59e81998-11-30 15:07:26 +0000280import smtplib
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000281
Neal Norwitzce96f692006-03-17 06:49:51 +0000282def raw_input(prompt):
283 import sys
284 sys.stdout.write(prompt)
285 sys.stdout.flush()
286 return sys.stdin.readline()
287
Fred Drake5b683621998-06-30 16:53:52 +0000288def prompt(prompt):
Fred Drakee9719fe2001-05-20 12:26:04 +0000289 return raw_input(prompt).strip()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000290
Fred Drake38e5d272000-04-03 20:13:55 +0000291fromaddr = prompt("From: ")
Fred Drakee9719fe2001-05-20 12:26:04 +0000292toaddrs = prompt("To: ").split()
Tim Peters9ca3f022003-06-15 23:08:45 +0000293print "Enter message, end with ^D (Unix) or ^Z (Windows):"
Fred Drake38e5d272000-04-03 20:13:55 +0000294
295# Add the From: and To: headers at the start!
296msg = ("From: %s\r\nTo: %s\r\n\r\n"
Raymond Hettinger1e803592003-09-22 06:25:10 +0000297 % (fromaddr, ", ".join(toaddrs)))
Fred Drake5b683621998-06-30 16:53:52 +0000298while 1:
Fred Drakee9719fe2001-05-20 12:26:04 +0000299 try:
300 line = raw_input()
301 except EOFError:
302 break
Fred Drake5b683621998-06-30 16:53:52 +0000303 if not line:
304 break
305 msg = msg + line
Fred Drake38e5d272000-04-03 20:13:55 +0000306
Raymond Hettinger1e803592003-09-22 06:25:10 +0000307print "Message length is " + repr(len(msg))
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000308
Fred Drakef6c59e81998-11-30 15:07:26 +0000309server = smtplib.SMTP('localhost')
Fred Drake5b683621998-06-30 16:53:52 +0000310server.set_debuglevel(1)
311server.sendmail(fromaddr, toaddrs, msg)
312server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000313\end{verbatim}