blob: d366ccae7432e9e3474b7995d9a54acfdc0c8908 [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
Fred Drake3240dd21998-07-01 14:10:52 +000017\begin{classdesc}{SMTP}{\optional{host\optional{, port}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000018A \class{SMTP} instance encapsulates an SMTP connection. It has
19methods that support a full repertoire of SMTP and ESMTP
20operations. If the optional host and port parameters are given, the
Fred Drake5b683621998-06-30 16:53:52 +000021SMTP \method{connect()} method is called with those parameters during
Guido van Rossum7969f311999-04-07 15:56:51 +000022initialization. An \exception{SMTPConnectError} is raised if the
23specified host doesn't respond correctly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000024
25For normal use, you should only require the initialization/connect,
Guido van Rossum7969f311999-04-07 15:56:51 +000026\method{sendmail()}, and \method{quit()} methods. An example is
Fred Drake5b683621998-06-30 16:53:52 +000027included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000028\end{classdesc}
29
Fred Drake3240dd21998-07-01 14:10:52 +000030
Fred Drake79936fe1999-04-22 17:23:34 +000031A nice selection of exceptions is defined as well:
32
33\begin{excdesc}{SMTPException}
34 Base exception class for all exceptions raised by this module.
35\end{excdesc}
36
37\begin{excdesc}{SMTPServerDisconnected}
38 This exception is raised when the server unexpectedly disconnects,
39 or when an attempt is made to use the \class{SMTP} instance before
40 connecting it to a server.
41\end{excdesc}
42
43\begin{excdesc}{SMTPResponseException}
44 Base class for all exceptions that include an SMTP error code.
45 These exceptions are generated in some instances when the SMTP
46 server returns an error code. The error code is stored in the
47 \member{smtp_code} attribute of the error, and the
48 \member{smtp_error} attribute is set to the error message.
49\end{excdesc}
50
51\begin{excdesc}{SMTPSenderRefused}
52 Sender address refused. In addition to the attributes set by on all
53 \exception{SMTPResponseException} exceptions, this sets `sender' to
54 the string that the SMTP server refused.
55\end{excdesc}
56
57\begin{excdesc}{SMTPRecipientsRefused}
58 All recipient addresses refused. The errors for each recipient are
59 accessable through the attribute \member{recipients}, which is a
60 dictionary of exactly the same sort as \method{SMTP.sendmail()}
61 returns.
62\end{excdesc}
63
64\begin{excdesc}{SMTPDataError}
65 The SMTP server refused to accept the message data.
66\end{excdesc}
67
68\begin{excdesc}{SMTPConnectError}
69 Error occurred during establishment of a connection with the server.
70\end{excdesc}
71
72\begin{excdesc}{SMTPHeloError}
73 The server refused our \samp{HELO} message.
74\end{excdesc}
75
76
77\subsection{SMTP Objects \label{SMTP-objects}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000078
Fred Drake5b683621998-06-30 16:53:52 +000079An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000080
81\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000082Set the debug output level. A true value for \var{level} results in
83debug messages for connection and for all messages sent to and
84received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000085\end{methoddesc}
86
Fred Drake3240dd21998-07-01 14:10:52 +000087\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
88Connect to a host on a given port. The defaults are to connect to the
89local host at the standard SMTP port (25).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000090
Fred Drake5b683621998-06-30 16:53:52 +000091If the hostname ends with a colon (\character{:}) followed by a
92number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000093the port number to use.
94
Fred Drake5b683621998-06-30 16:53:52 +000095Note: This method is automatically invoked by the constructor if a
96host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000097\end{methoddesc}
98
99\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +0000100Send a command \var{cmd} to the server. The optional argument
101\var{argstring} is simply concatenated to the command, separated by a
102space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000103
Fred Drake5b683621998-06-30 16:53:52 +0000104This returns a 2-tuple composed of a numeric response code and the
105actual response line (multiline responses are joined into one long
106line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000107
108In normal operation it should not be necessary to call this method
109explicitly. It is used to implement other methods and may be useful
110for testing private extensions.
Guido van Rossum7969f311999-04-07 15:56:51 +0000111
Fred Drake79936fe1999-04-22 17:23:34 +0000112If the connection to the server is lost while waiting for the reply,
113\exception{SMTPServerDisconnected} will be raised.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000114\end{methoddesc}
115
116\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +0000117Identify yourself to the SMTP server using \samp{HELO}. The hostname
118argument defaults to the fully qualified domain name of the local
119host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000120
121In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +0000122explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000123when necessary.
124\end{methoddesc}
125
126\begin{methoddesc}{ehlo}{\optional{hostname}}
Guido van Rossum7969f311999-04-07 15:56:51 +0000127Identify yourself to an ESMTP server using \samp{EHLO}. The hostname
Fred Drake3240dd21998-07-01 14:10:52 +0000128argument defaults to the fully qualified domain name of the local
129host. Examine the response for ESMTP option and store them for use by
Fred Drake5b683621998-06-30 16:53:52 +0000130\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000131
Fred Drake5b683621998-06-30 16:53:52 +0000132Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000133mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +0000134will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000135\end{methoddesc}
136
Guido van Rossum7969f311999-04-07 15:56:51 +0000137\begin{methoddesc}{has_extn}{name}
Fred Drake79936fe1999-04-22 17:23:34 +0000138Return \code{1} if \var{name} is in the set of SMTP service extensions
139returned by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000140\end{methoddesc}
141
142\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +0000143Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +0000144Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000145(including human name) if the user address is valid. Otherwise returns
146an SMTP error code of 400 or greater and an error string.
147
Fred Drake3240dd21998-07-01 14:10:52 +0000148Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000149\end{methoddesc}
150
Guido van Rossum7969f311999-04-07 15:56:51 +0000151\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{,
Fred Drake79936fe1999-04-22 17:23:34 +0000152 mail_options, rcpt_options}}
Fred Drake3240dd21998-07-01 14:10:52 +0000153Send mail. The required arguments are an \rfc{822} from-address
154string, a list of \rfc{822} to-address strings, and a message string.
Guido van Rossum7969f311999-04-07 15:56:51 +0000155The caller may pass a list of ESMTP options (such as \samp{8bitmime})
156to be used in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP
157options (such as \samp{DSN} commands) that should be used with all
158\samp{RCPT} commands can be passed as \var{rcpt_options}. (If you
159need to use different ESMTP options to different recipients you have
160to use the low-level methods such as \method{mail}, \method{rcpt} and
161\method{data} to send the message.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000162
Fred Drake3240dd21998-07-01 14:10:52 +0000163If there has been no previous \samp{EHLO} or \samp{HELO} command this
164session, this method tries ESMTP \samp{EHLO} first. If the server does
165ESMTP, message size and each of the specified options will be passed
166to it (if the option is in the feature set the server advertises). If
167\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
168suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000169
Guido van Rossum7969f311999-04-07 15:56:51 +0000170This method will return normally if the mail is accepted for at least
171one recipient. Otherwise it will throw an exception. That is, if this
172method does not throw an exception, then someone should get your mail.
173If this method does not throw an exception, it returns a dictionary,
174with one entry for each recipient that was refused. Each entry
175contains a tuple of the SMTP error code and the accompanying error
176message sent by the server.
177
178This method may raise the following exceptions:
179
180\begin{itemize}
Fred Drake79936fe1999-04-22 17:23:34 +0000181\item[\exception{SMTPRecipientsRefused}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000182All recipients were refused. Nobody got the mail. The
183\var{recipients} attribute of the exception object is a dictionary
184with information about the refused recipients (like the one returned
185when at least one recipient was accepted).
186
Fred Drake79936fe1999-04-22 17:23:34 +0000187\item[\exception{SMTPHeloError}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000188The server didn't reply properly to
Guido van Rossum6ed122a1999-04-21 16:19:16 +0000189the helo greeting.
Guido van Rossum7969f311999-04-07 15:56:51 +0000190
Fred Drake79936fe1999-04-22 17:23:34 +0000191\item[\exception{SMTPSenderRefused}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000192The server didn't accept the from_addr.
193
Fred Drake79936fe1999-04-22 17:23:34 +0000194\item[\exception{SMTPDataError}]
Guido van Rossum7969f311999-04-07 15:56:51 +0000195The server replied with an unexpected
196error code (other than a refusal of
197a recipient).
Guido van Rossum7969f311999-04-07 15:56:51 +0000198\end{itemize}
199
200Unless otherwise noted the connection will be open even after
201an exception is raised.
202
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000203\end{methoddesc}
204
205\begin{methoddesc}{quit}{}
206Terminate the SMTP session and close the connection.
207\end{methoddesc}
208
209Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000210\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
211\samp{DATA} are also supported. Normally these do not need to be
212called directly, so they are not documented here. For details,
213consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000214
Fred Drake5b683621998-06-30 16:53:52 +0000215
Fred Drake60c3caf1998-08-07 16:03:32 +0000216\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000217
Fred Drake7be0cde1998-12-22 18:04:48 +0000218This example prompts the user for addresses needed in the message
219envelop (`To' and `From' addresses), and the message to be
220delivered. Note that the headers to be included with the message must
221be included in the message as entered; this example doesn't do any
222processing of the \rfc{822} headers. In particular, the `To' and
223`From' addresses must be included in the message headers explicitly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000224
225\begin{verbatim}
Fred Drake60c3caf1998-08-07 16:03:32 +0000226import rfc822, string, sys
Fred Drakef6c59e81998-11-30 15:07:26 +0000227import smtplib
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000228
Fred Drake5b683621998-06-30 16:53:52 +0000229def prompt(prompt):
230 sys.stdout.write(prompt + ": ")
231 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000232
Fred Drake5b683621998-06-30 16:53:52 +0000233fromaddr = prompt("From")
234toaddrs = string.splitfields(prompt("To"), ',')
235print "Enter message, end with ^D:"
Fred Drake7be0cde1998-12-22 18:04:48 +0000236msg = ""
Fred Drake5b683621998-06-30 16:53:52 +0000237while 1:
238 line = sys.stdin.readline()
239 if not line:
240 break
241 msg = msg + line
242print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000243
Fred Drakef6c59e81998-11-30 15:07:26 +0000244server = smtplib.SMTP('localhost')
Fred Drake5b683621998-06-30 16:53:52 +0000245server.set_debuglevel(1)
246server.sendmail(fromaddr, toaddrs, msg)
247server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000248\end{verbatim}
249
Fred Drake5b683621998-06-30 16:53:52 +0000250
251\begin{seealso}
Fred Drakeebe03411999-11-09 20:11:17 +0000252 \seetext{Internet \rfc{821}, \citetitle{Simple Mail Transfer Protocol}.
Fred Drake79936fe1999-04-22 17:23:34 +0000253 Available online at
254 \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
Fred Drake5b683621998-06-30 16:53:52 +0000255
Fred Drakeebe03411999-11-09 20:11:17 +0000256 \seetext{Internet \rfc{1869}, \citetitle{SMTP Service Extensions}.
Fred Drake79936fe1999-04-22 17:23:34 +0000257 Available online at
258 \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
Fred Drake5b683621998-06-30 16:53:52 +0000259\end{seealso}