blob: de295a15ecb3e30af47b10b56ae76a1a917e9a64 [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 Rossum8668e8e1998-06-28 17:55:53 +000022initialization.
23
24For normal use, you should only require the initialization/connect,
Fred Drake5b683621998-06-30 16:53:52 +000025\method{sendmail()}, and \method{quit()} methods An example is
26included below.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000027\end{classdesc}
28
Fred Drake3240dd21998-07-01 14:10:52 +000029
Guido van Rossum8668e8e1998-06-28 17:55:53 +000030\subsection{SMTP Objects}
31\label{SMTP-objects}
32
Fred Drake5b683621998-06-30 16:53:52 +000033An \class{SMTP} instance has the following methods:
Guido van Rossum8668e8e1998-06-28 17:55:53 +000034
35\begin{methoddesc}{set_debuglevel}{level}
Fred Drake5b683621998-06-30 16:53:52 +000036Set the debug output level. A true value for \var{level} results in
37debug messages for connection and for all messages sent to and
38received from the server.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000039\end{methoddesc}
40
Fred Drake3240dd21998-07-01 14:10:52 +000041\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
42Connect to a host on a given port. The defaults are to connect to the
43local host at the standard SMTP port (25).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000044
Fred Drake5b683621998-06-30 16:53:52 +000045If the hostname ends with a colon (\character{:}) followed by a
46number, that suffix will be stripped off and the number interpreted as
Guido van Rossum8668e8e1998-06-28 17:55:53 +000047the port number to use.
48
Fred Drake5b683621998-06-30 16:53:52 +000049Note: This method is automatically invoked by the constructor if a
50host is specified during instantiation.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000051\end{methoddesc}
52
53\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
Fred Drake5b683621998-06-30 16:53:52 +000054Send a command \var{cmd} to the server. The optional argument
55\var{argstring} is simply concatenated to the command, separated by a
56space.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000057
Fred Drake5b683621998-06-30 16:53:52 +000058This returns a 2-tuple composed of a numeric response code and the
59actual response line (multiline responses are joined into one long
60line.)
Guido van Rossum8668e8e1998-06-28 17:55:53 +000061
62In normal operation it should not be necessary to call this method
63explicitly. It is used to implement other methods and may be useful
64for testing private extensions.
65\end{methoddesc}
66
67\begin{methoddesc}{helo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000068Identify yourself to the SMTP server using \samp{HELO}. The hostname
69argument defaults to the fully qualified domain name of the local
70host.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000071
72In normal operation it should not be necessary to call this method
Fred Drake5b683621998-06-30 16:53:52 +000073explicitly. It will be implicitly called by the \method{sendmail()}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000074when necessary.
75\end{methoddesc}
76
77\begin{methoddesc}{ehlo}{\optional{hostname}}
Fred Drake3240dd21998-07-01 14:10:52 +000078Identify yourself to an ESMTP server using \samp{HELO}. The hostname
79argument defaults to the fully qualified domain name of the local
80host. Examine the response for ESMTP option and store them for use by
Fred Drake5b683621998-06-30 16:53:52 +000081\method{has_option()}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000082
Fred Drake5b683621998-06-30 16:53:52 +000083Unless you wish to use \method{has_option()} before sending
Guido van Rossum8668e8e1998-06-28 17:55:53 +000084mail, it should not be necessary to call this method explicitly. It
Fred Drake5b683621998-06-30 16:53:52 +000085will be implicitly called by \method{sendmail()} when necessary.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000086\end{methoddesc}
87
88\begin{methoddesc}{has_option}{name}
Fred Drake5b683621998-06-30 16:53:52 +000089Return \code{1} if \var{name} is in the set of ESMTP options returned
90by the server, \code{0} otherwise. Case is ignored.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000091\end{methoddesc}
92
93\begin{methoddesc}{verify}{address}
Fred Drake3240dd21998-07-01 14:10:52 +000094Check the validity of an address on this server using SMTP \samp{VRFY}.
Fred Drake5b683621998-06-30 16:53:52 +000095Returns a tuple consisting of code 250 and a full \rfc{822} address
Guido van Rossum8668e8e1998-06-28 17:55:53 +000096(including human name) if the user address is valid. Otherwise returns
97an SMTP error code of 400 or greater and an error string.
98
Fred Drake3240dd21998-07-01 14:10:52 +000099Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000100\end{methoddesc}
101
Fred Drake3240dd21998-07-01 14:10:52 +0000102\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options}}
103Send mail. The required arguments are an \rfc{822} from-address
104string, a list of \rfc{822} to-address strings, and a message string.
105The caller may pass a list of ESMTP options to be used in \samp{MAIL
106FROM} commands as \var{options}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000107
Fred Drake3240dd21998-07-01 14:10:52 +0000108If there has been no previous \samp{EHLO} or \samp{HELO} command this
109session, this method tries ESMTP \samp{EHLO} first. If the server does
110ESMTP, message size and each of the specified options will be passed
111to it (if the option is in the feature set the server advertises). If
112\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
113suppressed.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000114
115This method will return normally if the mail is accepted for at least
116one recipient. Otherwise it will throw an exception (either
Fred Drake5b683621998-06-30 16:53:52 +0000117\exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or
Fred Drake3240dd21998-07-01 14:10:52 +0000118\exception{SMTPDataError}). That is, if this method does not throw an
119exception, then someone should get your mail. If this method does not
120throw an exception, it returns a dictionary, with one entry for each
121recipient that was refused.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000122\end{methoddesc}
123
124\begin{methoddesc}{quit}{}
125Terminate the SMTP session and close the connection.
126\end{methoddesc}
127
128Low-level methods corresponding to the standard SMTP/ESMTP commands
Fred Drake3240dd21998-07-01 14:10:52 +0000129\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
130\samp{DATA} are also supported. Normally these do not need to be
131called directly, so they are not documented here. For details,
132consult the module code.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000133
Fred Drake5b683621998-06-30 16:53:52 +0000134
Fred Drake60c3caf1998-08-07 16:03:32 +0000135\subsection{SMTP Example \label{SMTP-example}}
Fred Drake5b683621998-06-30 16:53:52 +0000136
137% really need a little description here...
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000138
139\begin{verbatim}
Fred Drake60c3caf1998-08-07 16:03:32 +0000140import rfc822, string, sys
Fred Drakef6c59e81998-11-30 15:07:26 +0000141import smtplib
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000142
Fred Drake5b683621998-06-30 16:53:52 +0000143def prompt(prompt):
144 sys.stdout.write(prompt + ": ")
145 return string.strip(sys.stdin.readline())
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000146
Fred Drake5b683621998-06-30 16:53:52 +0000147fromaddr = prompt("From")
148toaddrs = string.splitfields(prompt("To"), ',')
149print "Enter message, end with ^D:"
150msg = ''
151while 1:
152 line = sys.stdin.readline()
153 if not line:
154 break
155 msg = msg + line
156print "Message length is " + `len(msg)`
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000157
Fred Drakef6c59e81998-11-30 15:07:26 +0000158server = smtplib.SMTP('localhost')
Fred Drake5b683621998-06-30 16:53:52 +0000159server.set_debuglevel(1)
160server.sendmail(fromaddr, toaddrs, msg)
161server.quit()
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000162\end{verbatim}
163
Fred Drake5b683621998-06-30 16:53:52 +0000164
165\begin{seealso}
166\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
167online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
168
169\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
170at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
171\end{seealso}