blob: 9d42ee1cdb4ef2dc220d5fb43e044ee9ccf08e63 [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}}
3\stmodindex{smtplib}
4\label{module-smtplib}
Guido van Rossum8668e8e1998-06-28 17:55:53 +00005
Fred Drake5b683621998-06-30 16:53:52 +00006The \module{smtplib} module defines an SMTP session object that can be
7used to send mail to any Internet machine with an SMTP or ESMTP
8listener daemon. For details of SMTP and ESMTP operation, consult
9\rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869}
10(\emph{SMTP Service Extensions}).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000011
12\begin{classdesc}{SMTP}{\optional{host, port}}
13A \class{SMTP} instance encapsulates an SMTP connection. It has
14methods that support a full repertoire of SMTP and ESMTP
15operations. If the optional host and port parameters are given, the
Fred Drake5b683621998-06-30 16:53:52 +000016SMTP \method{connect()} method is called with those parameters during
Guido van Rossum8668e8e1998-06-28 17:55:53 +000017initialization.
18
19For normal use, you should only require the initialization/connect,
Fred Drake5b683621998-06-30 16:53:52 +000020\method{sendmail()}, and \method{quit()} methods An example is
21included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000022\end{classdesc}
23
24\subsection{SMTP Objects}
25\label{SMTP-objects}
26
Fred Drake5b683621998-06-30 16:53:52 +000027An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000028
29\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000030Set the debug output level. A true value for \var{level} results in
31debug messages for connection and for all messages sent to and
32received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000033\end{methoddesc}
34
Fred Drake5b683621998-06-30 16:53:52 +000035\begin{methoddesc}{connect}{\optional{host='localhost'\optional{, port=0}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000036Connect to a host on a given port.
37
Fred Drake5b683621998-06-30 16:53:52 +000038If the hostname ends with a colon (\character{:}) followed by a
39number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000040the port number to use.
41
Fred Drake5b683621998-06-30 16:53:52 +000042Note: This method is automatically invoked by the constructor if a
43host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000044\end{methoddesc}
45
46\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +000047Send a command \var{cmd} to the server. The optional argument
48\var{argstring} is simply concatenated to the command, separated by a
49space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000050
Fred Drake5b683621998-06-30 16:53:52 +000051This returns a 2-tuple composed of a numeric response code and the
52actual response line (multiline responses are joined into one long
53line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +000054
55In normal operation it should not be necessary to call this method
56explicitly. It is used to implement other methods and may be useful
57for testing private extensions.
58\end{methoddesc}
59
60\begin{methoddesc}{helo}{\optional{hostname}}
61Identify yourself to the SMTP server using HELO. The hostname
62argument defaults to the FQDN of the local host.
63
64In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +000065explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000066when necessary.
67\end{methoddesc}
68
69\begin{methoddesc}{ehlo}{\optional{hostname}}
70Identify yourself to an ESMTP server using HELO. The hostname
71argument defaults to the FQDN of the local host. Examine the
Fred Drake5b683621998-06-30 16:53:52 +000072response for ESMTP option and store them for use by
73\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000074
Fred Drake5b683621998-06-30 16:53:52 +000075Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +000076mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +000077will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000078\end{methoddesc}
79
80\begin{methoddesc}{has_option}{name}
Fred Drake5b683621998-06-30 16:53:52 +000081Return \code{1} if \var{name} is in the set of ESMTP options returned
82by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000083\end{methoddesc}
84
85\begin{methoddesc}{verify}{address}
86Check the validity of an address on this server using SMTP VRFY.
Fred Drake5b683621998-06-30 16:53:52 +000087Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +000088(including human name) if the user address is valid. Otherwise returns
89an SMTP error code of 400 or greater and an error string.
90
91Note: many sites disable SMTP VRFY in order to foil spammers.
92\end{methoddesc}
93
94\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options=[]}}
Fred Drake5b683621998-06-30 16:53:52 +000095Send mail. The required arguments are an \rfc{822} from-address string,
96a list of \rfc{822} to-address strings, and a message string. The caller
Guido van Rossum8668e8e1998-06-28 17:55:53 +000097may pass a list of ESMTP options to be used in MAIL FROM commands.
98
99If there has been no previous EHLO or HELO command this session, this
100method tries ESMTP EHLO first. If the server does ESMTP, message size
101and each of the specified options will be passed to it (if the option
102is in the feature set the server advertises). If EHLO fails, HELO
103will be tried and ESMTP options suppressed.
104
105This method will return normally if the mail is accepted for at least
106one recipient. Otherwise it will throw an exception (either
Fred Drake5b683621998-06-30 16:53:52 +0000107\exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or
108\exception{SMTPDataError}).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000109That is, if this method does not throw an exception, then someone
110should get your mail. If this method does not throw an exception,
111it returns a dictionary, with one entry for each recipient that was
Fred Drake5b683621998-06-30 16:53:52 +0000112refused.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000113\end{methoddesc}
114
115\begin{methoddesc}{quit}{}
116Terminate the SMTP session and close the connection.
117\end{methoddesc}
118
119Low-level methods corresponding to the standard SMTP/ESMTP commands
120HELP, RSET, NOOP, MAIL, RCPT, and DATA are also supported. Normally
121these do not need to be called directly, so they are not documented
122here. For details, consult the module code.
123
Fred Drake5b683621998-06-30 16:53:52 +0000124
125\subsection{SMTP Example}
126\label{SMTP-example}
127
128% really need a little description here...
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000129
130\begin{verbatim}
Fred Drake5b683621998-06-30 16:53:52 +0000131import sys, rfc822
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000132
Fred Drake5b683621998-06-30 16:53:52 +0000133def prompt(prompt):
134 sys.stdout.write(prompt + ": ")
135 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000136
Fred Drake5b683621998-06-30 16:53:52 +0000137fromaddr = prompt("From")
138toaddrs = string.splitfields(prompt("To"), ',')
139print "Enter message, end with ^D:"
140msg = ''
141while 1:
142 line = sys.stdin.readline()
143 if not line:
144 break
145 msg = msg + line
146print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000147
Fred Drake5b683621998-06-30 16:53:52 +0000148server = SMTP('localhost')
149server.set_debuglevel(1)
150server.sendmail(fromaddr, toaddrs, msg)
151server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000152\end{verbatim}
153
Fred Drake5b683621998-06-30 16:53:52 +0000154
155\begin{seealso}
156\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
157online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
158
159\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
160at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
161\end{seealso}