blob: 21cadcec006e4001dc7a9848eda3cb39e4cf43ab [file] [log] [blame]
Guido van Rossum8668e8e1998-06-28 17:55:53 +00001% Documentation by ESR
Fred Drake5b683621998-06-30 16:53:52 +00002\section{Standard Module \module{smtplib}}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{smtplib}
4
5\modulesynopsis{SMTP protocol client (requires sockets).}
6
Fred Drake3240dd21998-07-01 14:10:52 +00007\indexii{SMTP}{protocol}
8\index{Simple Mail Transfer Protocol}
Guido van Rossum8668e8e1998-06-28 17:55:53 +00009
Fred Drake3240dd21998-07-01 14:10:52 +000010The \module{smtplib} module defines an SMTP client session object that
11can be used to send mail to any Internet machine with an SMTP or ESMTP
Fred Drake5b683621998-06-30 16:53:52 +000012listener daemon. For details of SMTP and ESMTP operation, consult
13\rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869}
14(\emph{SMTP Service Extensions}).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000015
Fred Drake3240dd21998-07-01 14:10:52 +000016\begin{classdesc}{SMTP}{\optional{host\optional{, port}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000017A \class{SMTP} instance encapsulates an SMTP connection. It has
18methods that support a full repertoire of SMTP and ESMTP
19operations. If the optional host and port parameters are given, the
Fred Drake5b683621998-06-30 16:53:52 +000020SMTP \method{connect()} method is called with those parameters during
Guido van Rossum8668e8e1998-06-28 17:55:53 +000021initialization.
22
23For normal use, you should only require the initialization/connect,
Fred Drake5b683621998-06-30 16:53:52 +000024\method{sendmail()}, and \method{quit()} methods An example is
25included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000026\end{classdesc}
27
Fred Drake3240dd21998-07-01 14:10:52 +000028
Guido van Rossum8668e8e1998-06-28 17:55:53 +000029\subsection{SMTP Objects}
30\label{SMTP-objects}
31
Fred Drake5b683621998-06-30 16:53:52 +000032An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000033
34\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000035Set the debug output level. A true value for \var{level} results in
36debug messages for connection and for all messages sent to and
37received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000038\end{methoddesc}
39
Fred Drake3240dd21998-07-01 14:10:52 +000040\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
41Connect to a host on a given port. The defaults are to connect to the
42local host at the standard SMTP port (25).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000043
Fred Drake5b683621998-06-30 16:53:52 +000044If the hostname ends with a colon (\character{:}) followed by a
45number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000046the port number to use.
47
Fred Drake5b683621998-06-30 16:53:52 +000048Note: This method is automatically invoked by the constructor if a
49host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000050\end{methoddesc}
51
52\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +000053Send a command \var{cmd} to the server. The optional argument
54\var{argstring} is simply concatenated to the command, separated by a
55space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000056
Fred Drake5b683621998-06-30 16:53:52 +000057This returns a 2-tuple composed of a numeric response code and the
58actual response line (multiline responses are joined into one long
59line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +000060
61In normal operation it should not be necessary to call this method
62explicitly. It is used to implement other methods and may be useful
63for testing private extensions.
64\end{methoddesc}
65
66\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000067Identify yourself to the SMTP server using \samp{HELO}. The hostname
68argument defaults to the fully qualified domain name of the local
69host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000070
71In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +000072explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000073when necessary.
74\end{methoddesc}
75
76\begin{methoddesc}{ehlo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000077Identify yourself to an ESMTP server using \samp{HELO}. The hostname
78argument defaults to the fully qualified domain name of the local
79host. Examine the response for ESMTP option and store them for use by
Fred Drake5b683621998-06-30 16:53:52 +000080\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000081
Fred Drake5b683621998-06-30 16:53:52 +000082Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +000083mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +000084will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000085\end{methoddesc}
86
87\begin{methoddesc}{has_option}{name}
Fred Drake5b683621998-06-30 16:53:52 +000088Return \code{1} if \var{name} is in the set of ESMTP options returned
89by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000090\end{methoddesc}
91
92\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +000093Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +000094Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +000095(including human name) if the user address is valid. Otherwise returns
96an SMTP error code of 400 or greater and an error string.
97
Fred Drake3240dd21998-07-01 14:10:52 +000098Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000099\end{methoddesc}
100
Fred Drake3240dd21998-07-01 14:10:52 +0000101\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options}}
102Send mail. The required arguments are an \rfc{822} from-address
103string, a list of \rfc{822} to-address strings, and a message string.
104The caller may pass a list of ESMTP options to be used in \samp{MAIL
105FROM} commands as \var{options}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000106
Fred Drake3240dd21998-07-01 14:10:52 +0000107If there has been no previous \samp{EHLO} or \samp{HELO} command this
108session, this method tries ESMTP \samp{EHLO} first. If the server does
109ESMTP, message size and each of the specified options will be passed
110to it (if the option is in the feature set the server advertises). If
111\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
112suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000113
114This method will return normally if the mail is accepted for at least
115one recipient. Otherwise it will throw an exception (either
Fred Drake5b683621998-06-30 16:53:52 +0000116\exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or
Fred Drake3240dd21998-07-01 14:10:52 +0000117\exception{SMTPDataError}). That is, if this method does not throw an
118exception, then someone should get your mail. If this method does not
119throw an exception, it returns a dictionary, with one entry for each
120recipient that was refused.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000121\end{methoddesc}
122
123\begin{methoddesc}{quit}{}
124Terminate the SMTP session and close the connection.
125\end{methoddesc}
126
127Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000128\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
129\samp{DATA} are also supported. Normally these do not need to be
130called directly, so they are not documented here. For details,
131consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000132
Fred Drake5b683621998-06-30 16:53:52 +0000133
Fred Drake60c3caf1998-08-07 16:03:32 +0000134\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000135
136% really need a little description here...
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000137
138\begin{verbatim}
Fred Drake60c3caf1998-08-07 16:03:32 +0000139import rfc822, string, sys
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000140
Fred Drake5b683621998-06-30 16:53:52 +0000141def prompt(prompt):
142 sys.stdout.write(prompt + ": ")
143 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000144
Fred Drake5b683621998-06-30 16:53:52 +0000145fromaddr = prompt("From")
146toaddrs = string.splitfields(prompt("To"), ',')
147print "Enter message, end with ^D:"
148msg = ''
149while 1:
150 line = sys.stdin.readline()
151 if not line:
152 break
153 msg = msg + line
154print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000155
Fred Drake5b683621998-06-30 16:53:52 +0000156server = SMTP('localhost')
157server.set_debuglevel(1)
158server.sendmail(fromaddr, toaddrs, msg)
159server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000160\end{verbatim}
161
Fred Drake5b683621998-06-30 16:53:52 +0000162
163\begin{seealso}
164\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
165online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
166
167\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
168at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
169\end{seealso}