blob: 2699cc7318ea68f6d887ad0a29594189d41dcc15 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{smtplib} ---
2 SMTP protocol client.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{smtplib}
Fred Drake295da241998-08-10 19:42:37 +00004\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
6\modulesynopsis{SMTP protocol client (requires sockets).}
7
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
14\rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869}
15(\emph{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
Guido van Rossum8668e8e1998-06-28 17:55:53 +000031\subsection{SMTP Objects}
32\label{SMTP-objects}
33
Fred Drake5b683621998-06-30 16:53:52 +000034An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000035
36\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000037Set the debug output level. A true value for \var{level} results in
38debug messages for connection and for all messages sent to and
39received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000040\end{methoddesc}
41
Fred Drake3240dd21998-07-01 14:10:52 +000042\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
43Connect to a host on a given port. The defaults are to connect to the
44local host at the standard SMTP port (25).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000045
Fred Drake5b683621998-06-30 16:53:52 +000046If the hostname ends with a colon (\character{:}) followed by a
47number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000048the port number to use.
49
Fred Drake5b683621998-06-30 16:53:52 +000050Note: This method is automatically invoked by the constructor if a
51host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000052\end{methoddesc}
53
54\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +000055Send a command \var{cmd} to the server. The optional argument
56\var{argstring} is simply concatenated to the command, separated by a
57space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000058
Fred Drake5b683621998-06-30 16:53:52 +000059This returns a 2-tuple composed of a numeric response code and the
60actual response line (multiline responses are joined into one long
61line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +000062
63In normal operation it should not be necessary to call this method
64explicitly. It is used to implement other methods and may be useful
65for testing private extensions.
Guido van Rossum7969f311999-04-07 15:56:51 +000066
67If the connection to the server is lost while waiting for the reply an
68\exception{SMTPServerDisconnected} exception will be raised.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000069\end{methoddesc}
70
71\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000072Identify yourself to the SMTP server using \samp{HELO}. The hostname
73argument defaults to the fully qualified domain name of the local
74host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000075
76In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +000077explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000078when necessary.
79\end{methoddesc}
80
81\begin{methoddesc}{ehlo}{\optional{hostname}}
Guido van Rossum7969f311999-04-07 15:56:51 +000082Identify yourself to an ESMTP server using \samp{EHLO}. The hostname
Fred Drake3240dd21998-07-01 14:10:52 +000083argument defaults to the fully qualified domain name of the local
84host. Examine the response for ESMTP option and store them for use by
Fred Drake5b683621998-06-30 16:53:52 +000085\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000086
Fred Drake5b683621998-06-30 16:53:52 +000087Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +000088mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +000089will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000090\end{methoddesc}
91
Guido van Rossum7969f311999-04-07 15:56:51 +000092\begin{methoddesc}{has_extn}{name}
93Return \code{1} if \var{name} is in the set of SMTP service extensions returned
Fred Drake5b683621998-06-30 16:53:52 +000094by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000095\end{methoddesc}
96
97\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +000098Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +000099Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000100(including human name) if the user address is valid. Otherwise returns
101an SMTP error code of 400 or greater and an error string.
102
Fred Drake3240dd21998-07-01 14:10:52 +0000103Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000104\end{methoddesc}
105
Guido van Rossum7969f311999-04-07 15:56:51 +0000106\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{,
107mail_options, rcpt_options}}
Fred Drake3240dd21998-07-01 14:10:52 +0000108Send mail. The required arguments are an \rfc{822} from-address
109string, a list of \rfc{822} to-address strings, and a message string.
Guido van Rossum7969f311999-04-07 15:56:51 +0000110The caller may pass a list of ESMTP options (such as \samp{8bitmime})
111to be used in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP
112options (such as \samp{DSN} commands) that should be used with all
113\samp{RCPT} commands can be passed as \var{rcpt_options}. (If you
114need to use different ESMTP options to different recipients you have
115to use the low-level methods such as \method{mail}, \method{rcpt} and
116\method{data} to send the message.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000117
Fred Drake3240dd21998-07-01 14:10:52 +0000118If there has been no previous \samp{EHLO} or \samp{HELO} command this
119session, this method tries ESMTP \samp{EHLO} first. If the server does
120ESMTP, message size and each of the specified options will be passed
121to it (if the option is in the feature set the server advertises). If
122\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
123suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000124
Guido van Rossum7969f311999-04-07 15:56:51 +0000125This method will return normally if the mail is accepted for at least
126one recipient. Otherwise it will throw an exception. That is, if this
127method does not throw an exception, then someone should get your mail.
128If this method does not throw an exception, it returns a dictionary,
129with one entry for each recipient that was refused. Each entry
130contains a tuple of the SMTP error code and the accompanying error
131message sent by the server.
132
133This method may raise the following exceptions:
134
135\begin{itemize}
136
137\item \exception{SMTPRecipientsRefused}
138
139All recipients were refused. Nobody got the mail. The
140\var{recipients} attribute of the exception object is a dictionary
141with information about the refused recipients (like the one returned
142when at least one recipient was accepted).
143
144\item \exception{SMTPHeloError}
145
146The server didn't reply properly to
147the helo greeting. The connection has
148been closed.
149
150\item \exception{SMTPSenderRefused}
151
152The server didn't accept the from_addr.
153
154\item \exception{SMTPDataError}
155
156The server replied with an unexpected
157error code (other than a refusal of
158a recipient).
159
160\end{itemize}
161
162Unless otherwise noted the connection will be open even after
163an exception is raised.
164
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000165\end{methoddesc}
166
167\begin{methoddesc}{quit}{}
168Terminate the SMTP session and close the connection.
169\end{methoddesc}
170
171Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000172\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
173\samp{DATA} are also supported. Normally these do not need to be
174called directly, so they are not documented here. For details,
175consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000176
Fred Drake5b683621998-06-30 16:53:52 +0000177
Fred Drake60c3caf1998-08-07 16:03:32 +0000178\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000179
Fred Drake7be0cde1998-12-22 18:04:48 +0000180This example prompts the user for addresses needed in the message
181envelop (`To' and `From' addresses), and the message to be
182delivered. Note that the headers to be included with the message must
183be included in the message as entered; this example doesn't do any
184processing of the \rfc{822} headers. In particular, the `To' and
185`From' addresses must be included in the message headers explicitly.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000186
187\begin{verbatim}
Fred Drake60c3caf1998-08-07 16:03:32 +0000188import rfc822, string, sys
Fred Drakef6c59e81998-11-30 15:07:26 +0000189import smtplib
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000190
Fred Drake5b683621998-06-30 16:53:52 +0000191def prompt(prompt):
192 sys.stdout.write(prompt + ": ")
193 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000194
Fred Drake5b683621998-06-30 16:53:52 +0000195fromaddr = prompt("From")
196toaddrs = string.splitfields(prompt("To"), ',')
197print "Enter message, end with ^D:"
Fred Drake7be0cde1998-12-22 18:04:48 +0000198msg = ""
Fred Drake5b683621998-06-30 16:53:52 +0000199while 1:
200 line = sys.stdin.readline()
201 if not line:
202 break
203 msg = msg + line
204print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000205
Fred Drakef6c59e81998-11-30 15:07:26 +0000206server = smtplib.SMTP('localhost')
Fred Drake5b683621998-06-30 16:53:52 +0000207server.set_debuglevel(1)
208server.sendmail(fromaddr, toaddrs, msg)
209server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000210\end{verbatim}
211
Fred Drake5b683621998-06-30 16:53:52 +0000212
213\begin{seealso}
214\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
215online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
216
217\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
218at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
219\end{seealso}