blob: d67578ad11a26cfa54459f047431f5ed04a55572 [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}
Fred Drake3240dd21998-07-01 14:10:52 +00005\indexii{SMTP}{protocol}
6\index{Simple Mail Transfer Protocol}
Guido van Rossum8668e8e1998-06-28 17:55:53 +00007
Fred Drake3240dd21998-07-01 14:10:52 +00008The \module{smtplib} module defines an SMTP client session object that
9can be used to send mail to any Internet machine with an SMTP or ESMTP
Fred Drake5b683621998-06-30 16:53:52 +000010listener daemon. For details of SMTP and ESMTP operation, consult
11\rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869}
12(\emph{SMTP Service Extensions}).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000013
Fred Drake3240dd21998-07-01 14:10:52 +000014\begin{classdesc}{SMTP}{\optional{host\optional{, port}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000015A \class{SMTP} instance encapsulates an SMTP connection. It has
16methods that support a full repertoire of SMTP and ESMTP
17operations. If the optional host and port parameters are given, the
Fred Drake5b683621998-06-30 16:53:52 +000018SMTP \method{connect()} method is called with those parameters during
Guido van Rossum8668e8e1998-06-28 17:55:53 +000019initialization.
20
21For normal use, you should only require the initialization/connect,
Fred Drake5b683621998-06-30 16:53:52 +000022\method{sendmail()}, and \method{quit()} methods An example is
23included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000024\end{classdesc}
25
Fred Drake3240dd21998-07-01 14:10:52 +000026
Guido van Rossum8668e8e1998-06-28 17:55:53 +000027\subsection{SMTP Objects}
28\label{SMTP-objects}
29
Fred Drake5b683621998-06-30 16:53:52 +000030An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000031
32\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000033Set the debug output level. A true value for \var{level} results in
34debug messages for connection and for all messages sent to and
35received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000036\end{methoddesc}
37
Fred Drake3240dd21998-07-01 14:10:52 +000038\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
39Connect to a host on a given port. The defaults are to connect to the
40local host at the standard SMTP port (25).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000041
Fred Drake5b683621998-06-30 16:53:52 +000042If the hostname ends with a colon (\character{:}) followed by a
43number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000044the port number to use.
45
Fred Drake5b683621998-06-30 16:53:52 +000046Note: This method is automatically invoked by the constructor if a
47host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000048\end{methoddesc}
49
50\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +000051Send a command \var{cmd} to the server. The optional argument
52\var{argstring} is simply concatenated to the command, separated by a
53space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000054
Fred Drake5b683621998-06-30 16:53:52 +000055This returns a 2-tuple composed of a numeric response code and the
56actual response line (multiline responses are joined into one long
57line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +000058
59In normal operation it should not be necessary to call this method
60explicitly. It is used to implement other methods and may be useful
61for testing private extensions.
62\end{methoddesc}
63
64\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000065Identify yourself to the SMTP server using \samp{HELO}. The hostname
66argument defaults to the fully qualified domain name of the local
67host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000068
69In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +000070explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000071when necessary.
72\end{methoddesc}
73
74\begin{methoddesc}{ehlo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000075Identify yourself to an ESMTP server using \samp{HELO}. The hostname
76argument defaults to the fully qualified domain name of the local
77host. Examine the response for ESMTP option and store them for use by
Fred Drake5b683621998-06-30 16:53:52 +000078\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000079
Fred Drake5b683621998-06-30 16:53:52 +000080Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +000081mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +000082will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000083\end{methoddesc}
84
85\begin{methoddesc}{has_option}{name}
Fred Drake5b683621998-06-30 16:53:52 +000086Return \code{1} if \var{name} is in the set of ESMTP options returned
87by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000088\end{methoddesc}
89
90\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +000091Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +000092Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +000093(including human name) if the user address is valid. Otherwise returns
94an SMTP error code of 400 or greater and an error string.
95
Fred Drake3240dd21998-07-01 14:10:52 +000096Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000097\end{methoddesc}
98
Fred Drake3240dd21998-07-01 14:10:52 +000099\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options}}
100Send mail. The required arguments are an \rfc{822} from-address
101string, a list of \rfc{822} to-address strings, and a message string.
102The caller may pass a list of ESMTP options to be used in \samp{MAIL
103FROM} commands as \var{options}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000104
Fred Drake3240dd21998-07-01 14:10:52 +0000105If there has been no previous \samp{EHLO} or \samp{HELO} command this
106session, this method tries ESMTP \samp{EHLO} first. If the server does
107ESMTP, message size and each of the specified options will be passed
108to it (if the option is in the feature set the server advertises). If
109\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
110suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000111
112This method will return normally if the mail is accepted for at least
113one recipient. Otherwise it will throw an exception (either
Fred Drake5b683621998-06-30 16:53:52 +0000114\exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or
Fred Drake3240dd21998-07-01 14:10:52 +0000115\exception{SMTPDataError}). That is, if this method does not throw an
116exception, then someone should get your mail. If this method does not
117throw an exception, it returns a dictionary, with one entry for each
118recipient that was refused.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000119\end{methoddesc}
120
121\begin{methoddesc}{quit}{}
122Terminate the SMTP session and close the connection.
123\end{methoddesc}
124
125Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000126\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
127\samp{DATA} are also supported. Normally these do not need to be
128called directly, so they are not documented here. For details,
129consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000130
Fred Drake5b683621998-06-30 16:53:52 +0000131
132\subsection{SMTP Example}
133\label{SMTP-example}
134
135% really need a little description here...
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000136
137\begin{verbatim}
Fred Drake5b683621998-06-30 16:53:52 +0000138import sys, rfc822
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000139
Fred Drake5b683621998-06-30 16:53:52 +0000140def prompt(prompt):
141 sys.stdout.write(prompt + ": ")
142 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000143
Fred Drake5b683621998-06-30 16:53:52 +0000144fromaddr = prompt("From")
145toaddrs = string.splitfields(prompt("To"), ',')
146print "Enter message, end with ^D:"
147msg = ''
148while 1:
149 line = sys.stdin.readline()
150 if not line:
151 break
152 msg = msg + line
153print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000154
Fred Drake5b683621998-06-30 16:53:52 +0000155server = SMTP('localhost')
156server.set_debuglevel(1)
157server.sendmail(fromaddr, toaddrs, msg)
158server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000159\end{verbatim}
160
Fred Drake5b683621998-06-30 16:53:52 +0000161
162\begin{seealso}
163\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
164online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
165
166\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
167at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
168\end{seealso}