blob: 77861025a3077d032aebd4deed0afac7ac6d939b [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{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000018 local_hostname\optional{, timeout}}}}}
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 Rossumd8faa362007-04-27 19:54:29 +000025The optional \var{timeout} parameter specifies a timeout in seconds for the
26connection attempt (if not specified, or passed as None, the global
27default timeout setting will be used).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000028
29For normal use, you should only require the initialization/connect,
Guido van Rossum7969f311999-04-07 15:56:51 +000030\method{sendmail()}, and \method{quit()} methods. An example is
Fred Drake5b683621998-06-30 16:53:52 +000031included below.
Guido van Rossumcd16bf62007-06-13 18:07:49 +000032
33\versionchanged[\var{timeout} was added]{2.6}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000034\end{classdesc}
35
Thomas Wouters89f507f2006-12-13 04:49:30 +000036\begin{classdesc}{SMTP_SSL}{\optional{host\optional{, port\optional{,
37 local_hostname\optional{,
38 keyfile\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000039 certfile\optional{, timeout}}}}}}}
Thomas Wouters89f507f2006-12-13 04:49:30 +000040A \class{SMTP_SSL} instance behaves exactly the same as instances of \class{SMTP}.
41\class{SMTP_SSL} should be used for situations where SSL is required from
42the beginning of the connection and using \method{starttls()} is not appropriate.
43If \var{host} is not specified, the local host is used. If \var{port} is
44omitted, the standard SMTP-over-SSL port (465) is used. \var{keyfile} and \var{certfile}
45are also optional, and can contain a PEM formatted private key and
46certificate chain file for the SSL connection.
Guido van Rossumd8faa362007-04-27 19:54:29 +000047The optional \var{timeout} parameter specifies a timeout in seconds for the
48connection attempt (if not specified, or passed as None, the global
49default timeout setting will be used).
Guido van Rossumcd16bf62007-06-13 18:07:49 +000050
51\versionchanged[\var{timeout} was added]{2.6}
Guido van Rossumd8faa362007-04-27 19:54:29 +000052\end{classdesc}
53
54\begin{classdesc}{LMTP}{\optional{host\optional{, port\optional{,
55 local_hostname}}}}
56
57The LMTP protocol, which is very similar to ESMTP, is heavily based
58on the standard SMTP client. It's common to use Unix sockets for LMTP,
59so our connect() method must support that as well as a regular
60host:port server. To specify a Unix socket, you must use an absolute
61path for \var{host}, starting with a '/'.
62
63Authentication is supported, using the regular SMTP mechanism. When
64using a Unix socket, LMTP generally don't support or require any
65authentication, but your mileage might vary.
66
67\versionadded{2.6}
68
Thomas Wouters89f507f2006-12-13 04:49:30 +000069\end{classdesc}
Fred Drake3240dd21998-07-01 14:10:52 +000070
Fred Drake79936fe1999-04-22 17:23:34 +000071A nice selection of exceptions is defined as well:
72
73\begin{excdesc}{SMTPException}
74 Base exception class for all exceptions raised by this module.
75\end{excdesc}
76
77\begin{excdesc}{SMTPServerDisconnected}
78 This exception is raised when the server unexpectedly disconnects,
79 or when an attempt is made to use the \class{SMTP} instance before
80 connecting it to a server.
81\end{excdesc}
82
83\begin{excdesc}{SMTPResponseException}
84 Base class for all exceptions that include an SMTP error code.
85 These exceptions are generated in some instances when the SMTP
86 server returns an error code. The error code is stored in the
87 \member{smtp_code} attribute of the error, and the
88 \member{smtp_error} attribute is set to the error message.
89\end{excdesc}
90
91\begin{excdesc}{SMTPSenderRefused}
Tim Peters9ca3f022003-06-15 23:08:45 +000092 Sender address refused. In addition to the attributes set by on all
Fred Drake79936fe1999-04-22 17:23:34 +000093 \exception{SMTPResponseException} exceptions, this sets `sender' to
94 the string that the SMTP server refused.
95\end{excdesc}
96
97\begin{excdesc}{SMTPRecipientsRefused}
98 All recipient addresses refused. The errors for each recipient are
Thomas Woutersf8316632000-07-16 19:01:10 +000099 accessible through the attribute \member{recipients}, which is a
Fred Drake79936fe1999-04-22 17:23:34 +0000100 dictionary of exactly the same sort as \method{SMTP.sendmail()}
101 returns.
102\end{excdesc}
103
104\begin{excdesc}{SMTPDataError}
105 The SMTP server refused to accept the message data.
106\end{excdesc}
107
108\begin{excdesc}{SMTPConnectError}
109 Error occurred during establishment of a connection with the server.
110\end{excdesc}
111
112\begin{excdesc}{SMTPHeloError}
113 The server refused our \samp{HELO} message.
114\end{excdesc}
115
Guido van Rossum992d4a32007-07-11 13:09:30 +0000116\begin{excdesc}{SMTPAuthenticationError}
117 SMTP authentication went wrong. Most probably the server didn't accept
118 the username/password combination provided.
119\end{excdesc}
Fred Drake79936fe1999-04-22 17:23:34 +0000120
Fred Drake38e5d272000-04-03 20:13:55 +0000121\begin{seealso}
Fred Drake58a2dff2000-10-03 05:56:55 +0000122 \seerfc{821}{Simple Mail Transfer Protocol}{Protocol definition for
123 SMTP. This document covers the model, operating procedure,
124 and protocol details for SMTP.}
125 \seerfc{1869}{SMTP Service Extensions}{Definition of the ESMTP
126 extensions for SMTP. This describes a framework for
127 extending SMTP with new commands, supporting dynamic
128 discovery of the commands provided by the server, and
129 defines a few additional commands.}
Fred Drake38e5d272000-04-03 20:13:55 +0000130\end{seealso}
131
132
Fred Drake79936fe1999-04-22 17:23:34 +0000133\subsection{SMTP Objects \label{SMTP-objects}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000134
Fred Drake5b683621998-06-30 16:53:52 +0000135An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000136
Guido van Rossumd8faa362007-04-27 19:54:29 +0000137\begin{methoddesc}[SMTP]{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +0000138Set the debug output level. A true value for \var{level} results in
139debug messages for connection and for all messages sent to and
140received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000141\end{methoddesc}
142
Guido van Rossumd8faa362007-04-27 19:54:29 +0000143\begin{methoddesc}[SMTP]{connect}{\optional{host\optional{, port}}}
Tim Peters9ca3f022003-06-15 23:08:45 +0000144Connect to a host on a given port. The defaults are to connect to the
Fred Drake3240dd21998-07-01 14:10:52 +0000145local host at the standard SMTP port (25).
Fred Drake5b683621998-06-30 16:53:52 +0000146If the hostname ends with a colon (\character{:}) followed by a
Tim Peters9ca3f022003-06-15 23:08:45 +0000147number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000148the port number to use.
Fred Drake0aa811c2001-10-20 04:24:09 +0000149This method is automatically invoked by the constructor if a
Fred Drake5b683621998-06-30 16:53:52 +0000150host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000151\end{methoddesc}
152
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153\begin{methoddesc}[SMTP]{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +0000154Send a command \var{cmd} to the server. The optional argument
155\var{argstring} is simply concatenated to the command, separated by a
156space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000157
Fred Drake5b683621998-06-30 16:53:52 +0000158This returns a 2-tuple composed of a numeric response code and the
159actual response line (multiline responses are joined into one long
160line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000161
162In normal operation it should not be necessary to call this method
163explicitly. It is used to implement other methods and may be useful
164for testing private extensions.
Guido van Rossum7969f311999-04-07 15:56:51 +0000165
Fred Drake79936fe1999-04-22 17:23:34 +0000166If the connection to the server is lost while waiting for the reply,
167\exception{SMTPServerDisconnected} will be raised.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000168\end{methoddesc}
169
Guido van Rossumd8faa362007-04-27 19:54:29 +0000170\begin{methoddesc}[SMTP]{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +0000171Identify yourself to the SMTP server using \samp{HELO}. The hostname
172argument defaults to the fully qualified domain name of the local
173host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000174
175In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +0000176explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000177when necessary.
178\end{methoddesc}
179
Guido van Rossumd8faa362007-04-27 19:54:29 +0000180\begin{methoddesc}[SMTP]{ehlo}{\optional{hostname}}
Guido van Rossum7969f311999-04-07 15:56:51 +0000181Identify yourself to an ESMTP server using \samp{EHLO}. The hostname
Fred Drake3240dd21998-07-01 14:10:52 +0000182argument defaults to the fully qualified domain name of the local
183host. Examine the response for ESMTP option and store them for use by
Greg Ward19b6f602003-01-08 03:04:42 +0000184\method{has_extn()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000185
Greg Ward19b6f602003-01-08 03:04:42 +0000186Unless you wish to use \method{has_extn()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000187mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +0000188will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000189\end{methoddesc}
190
Guido van Rossumd8faa362007-04-27 19:54:29 +0000191\begin{methoddesc}[SMTP]{has_extn}{name}
Raymond Hettinger9b4dab42003-12-31 18:37:28 +0000192Return \constant{True} if \var{name} is in the set of SMTP service
193extensions returned by the server, \constant{False} otherwise.
194Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000195\end{methoddesc}
196
Guido van Rossumd8faa362007-04-27 19:54:29 +0000197\begin{methoddesc}[SMTP]{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +0000198Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +0000199Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000200(including human name) if the user address is valid. Otherwise returns
201an SMTP error code of 400 or greater and an error string.
202
Fred Drake0aa811c2001-10-20 04:24:09 +0000203\note{Many sites disable SMTP \samp{VRFY} in order to foil spammers.}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000204\end{methoddesc}
205
Guido van Rossumd8faa362007-04-27 19:54:29 +0000206\begin{methoddesc}[SMTP]{login}{user, password}
Fred Drakeaae8da12001-09-11 16:58:00 +0000207Log in on an SMTP server that requires authentication.
208The arguments are the username and the password to authenticate with.
209If there has been no previous \samp{EHLO} or \samp{HELO} command this
210session, this method tries ESMTP \samp{EHLO} first.
211This method will return normally if the authentication was successful,
212or may raise the following exceptions:
213
214\begin{description}
215 \item[\exception{SMTPHeloError}]
216 The server didn't reply properly to the \samp{HELO} greeting.
217 \item[\exception{SMTPAuthenticationError}]
218 The server didn't accept the username/password combination.
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000219 \item[\exception{SMTPException}]
Fred Drakeaae8da12001-09-11 16:58:00 +0000220 No suitable authentication method was found.
221\end{description}
222\end{methoddesc}
223
Guido van Rossumd8faa362007-04-27 19:54:29 +0000224\begin{methoddesc}[SMTP]{starttls}{\optional{keyfile\optional{, certfile}}}
Fred Drakef2a5f3f2001-09-14 17:48:41 +0000225Put the SMTP connection in TLS (Transport Layer Security) mode. All
226SMTP commands that follow will be encrypted. You should then call
227\method{ehlo()} again.
Guido van Rossumf7fcf5e2001-09-14 16:08:44 +0000228
Fred Drakef2a5f3f2001-09-14 17:48:41 +0000229If \var{keyfile} and \var{certfile} are provided, these are passed to
230the \refmodule{socket} module's \function{ssl()} function.
Guido van Rossumf7fcf5e2001-09-14 16:08:44 +0000231\end{methoddesc}
232
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233\begin{methoddesc}[SMTP]{sendmail}{from_addr, to_addrs, msg\optional{,
234 mail_options, rcpt_options}}
Fred Drake3240dd21998-07-01 14:10:52 +0000235Send mail. The required arguments are an \rfc{822} from-address
Georg Brandl9020a212005-06-25 18:24:03 +0000236string, a list of \rfc{822} to-address strings (a bare string will be
237treated as a list with 1 address), and a message string. The caller
238may pass a list of ESMTP options (such as \samp{8bitmime}) to be used
239in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP options
240(such as \samp{DSN} commands) that should be used with all \samp{RCPT}
241commands can be passed as \var{rcpt_options}. (If you need to use
242different ESMTP options to different recipients you have to use the
243low-level methods such as \method{mail}, \method{rcpt} and
Guido van Rossum7969f311999-04-07 15:56:51 +0000244\method{data} to send the message.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000245
Fred Drake0aa811c2001-10-20 04:24:09 +0000246\note{The \var{from_addr} and \var{to_addrs} parameters are
Fred Drake38e5d272000-04-03 20:13:55 +0000247used to construct the message envelope used by the transport agents.
Fred Drake0aa811c2001-10-20 04:24:09 +0000248The \class{SMTP} does not modify the message headers in any way.}
Fred Drake38e5d272000-04-03 20:13:55 +0000249
Fred Drake3240dd21998-07-01 14:10:52 +0000250If there has been no previous \samp{EHLO} or \samp{HELO} command this
251session, this method tries ESMTP \samp{EHLO} first. If the server does
252ESMTP, message size and each of the specified options will be passed
253to it (if the option is in the feature set the server advertises). If
254\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
255suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000256
Guido van Rossum7969f311999-04-07 15:56:51 +0000257This method will return normally if the mail is accepted for at least
258one recipient. Otherwise it will throw an exception. That is, if this
259method does not throw an exception, then someone should get your mail.
260If this method does not throw an exception, it returns a dictionary,
261with one entry for each recipient that was refused. Each entry
262contains a tuple of the SMTP error code and the accompanying error
263message sent by the server.
264
265This method may raise the following exceptions:
266
Fred Drake38e5d272000-04-03 20:13:55 +0000267\begin{description}
Fred Drake79936fe1999-04-22 17:23:34 +0000268\item[\exception{SMTPRecipientsRefused}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000269All recipients were refused. Nobody got the mail. The
Fred Drake38e5d272000-04-03 20:13:55 +0000270\member{recipients} attribute of the exception object is a dictionary
Guido van Rossum7969f311999-04-07 15:56:51 +0000271with information about the refused recipients (like the one returned
272when at least one recipient was accepted).
273
Fred Drake79936fe1999-04-22 17:23:34 +0000274\item[\exception{SMTPHeloError}]
Fred Drake38e5d272000-04-03 20:13:55 +0000275The server didn't reply properly to the \samp{HELO} greeting.
Guido van Rossum7969f311999-04-07 15:56:51 +0000276
Fred Drake79936fe1999-04-22 17:23:34 +0000277\item[\exception{SMTPSenderRefused}]
Fred Drake38e5d272000-04-03 20:13:55 +0000278The server didn't accept the \var{from_addr}.
Guido van Rossum7969f311999-04-07 15:56:51 +0000279
Fred Drake79936fe1999-04-22 17:23:34 +0000280\item[\exception{SMTPDataError}]
Fred Drake38e5d272000-04-03 20:13:55 +0000281The server replied with an unexpected error code (other than a refusal
282of a recipient).
283\end{description}
Guido van Rossum7969f311999-04-07 15:56:51 +0000284
Fred Drake38e5d272000-04-03 20:13:55 +0000285Unless otherwise noted, the connection will be open even after
Guido van Rossum7969f311999-04-07 15:56:51 +0000286an exception is raised.
287
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000288\end{methoddesc}
289
Guido van Rossumd8faa362007-04-27 19:54:29 +0000290\begin{methoddesc}[SMTP]{quit}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000291Terminate the SMTP session and close the connection.
292\end{methoddesc}
293
294Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000295\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
296\samp{DATA} are also supported. Normally these do not need to be
297called directly, so they are not documented here. For details,
298consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000299
Fred Drake5b683621998-06-30 16:53:52 +0000300
Fred Drake60c3caf1998-08-07 16:03:32 +0000301\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000302
Fred Drake7be0cde1998-12-22 18:04:48 +0000303This example prompts the user for addresses needed in the message
Fred Drake38e5d272000-04-03 20:13:55 +0000304envelope (`To' and `From' addresses), and the message to be
Tim Peters9ca3f022003-06-15 23:08:45 +0000305delivered. Note that the headers to be included with the message must
Fred Drake7be0cde1998-12-22 18:04:48 +0000306be included in the message as entered; this example doesn't do any
307processing of the \rfc{822} headers. In particular, the `To' and
308`From' addresses must be included in the message headers explicitly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000309
310\begin{verbatim}
Fred Drakef6c59e81998-11-30 15:07:26 +0000311import smtplib
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000312
Neal Norwitzce96f692006-03-17 06:49:51 +0000313def raw_input(prompt):
314 import sys
315 sys.stdout.write(prompt)
316 sys.stdout.flush()
317 return sys.stdin.readline()
318
Fred Drake5b683621998-06-30 16:53:52 +0000319def prompt(prompt):
Fred Drakee9719fe2001-05-20 12:26:04 +0000320 return raw_input(prompt).strip()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000321
Fred Drake38e5d272000-04-03 20:13:55 +0000322fromaddr = prompt("From: ")
Fred Drakee9719fe2001-05-20 12:26:04 +0000323toaddrs = prompt("To: ").split()
Tim Peters9ca3f022003-06-15 23:08:45 +0000324print "Enter message, end with ^D (Unix) or ^Z (Windows):"
Fred Drake38e5d272000-04-03 20:13:55 +0000325
326# Add the From: and To: headers at the start!
327msg = ("From: %s\r\nTo: %s\r\n\r\n"
Raymond Hettinger1e803592003-09-22 06:25:10 +0000328 % (fromaddr, ", ".join(toaddrs)))
Fred Drake5b683621998-06-30 16:53:52 +0000329while 1:
Fred Drakee9719fe2001-05-20 12:26:04 +0000330 try:
331 line = raw_input()
332 except EOFError:
333 break
Fred Drake5b683621998-06-30 16:53:52 +0000334 if not line:
335 break
336 msg = msg + line
Fred Drake38e5d272000-04-03 20:13:55 +0000337
Raymond Hettinger1e803592003-09-22 06:25:10 +0000338print "Message length is " + repr(len(msg))
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000339
Fred Drakef6c59e81998-11-30 15:07:26 +0000340server = smtplib.SMTP('localhost')
Fred Drake5b683621998-06-30 16:53:52 +0000341server.set_debuglevel(1)
342server.sendmail(fromaddr, toaddrs, msg)
343server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000344\end{verbatim}