1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{smtplib} ---
|
|
|
|
SMTP protocol client.}
|
1998-07-23 14:59:49 -03:00
|
|
|
\declaremodule{standard}{smtplib}
|
1998-08-10 16:42:37 -03:00
|
|
|
\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
|
|
|
\modulesynopsis{SMTP protocol client (requires sockets).}
|
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
\indexii{SMTP}{protocol}
|
|
|
|
\index{Simple Mail Transfer Protocol}
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
The \module{smtplib} module defines an SMTP client session object that
|
|
|
|
can be used to send mail to any Internet machine with an SMTP or ESMTP
|
1998-06-30 13:53:52 -03:00
|
|
|
listener daemon. For details of SMTP and ESMTP operation, consult
|
|
|
|
\rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869}
|
|
|
|
(\emph{SMTP Service Extensions}).
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
\begin{classdesc}{SMTP}{\optional{host\optional{, port}}}
|
1998-06-28 14:55:53 -03:00
|
|
|
A \class{SMTP} instance encapsulates an SMTP connection. It has
|
|
|
|
methods that support a full repertoire of SMTP and ESMTP
|
|
|
|
operations. If the optional host and port parameters are given, the
|
1998-06-30 13:53:52 -03:00
|
|
|
SMTP \method{connect()} method is called with those parameters during
|
1998-06-28 14:55:53 -03:00
|
|
|
initialization.
|
|
|
|
|
|
|
|
For normal use, you should only require the initialization/connect,
|
1998-06-30 13:53:52 -03:00
|
|
|
\method{sendmail()}, and \method{quit()} methods An example is
|
|
|
|
included below.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{classdesc}
|
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
|
1998-06-28 14:55:53 -03:00
|
|
|
\subsection{SMTP Objects}
|
|
|
|
\label{SMTP-objects}
|
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
An \class{SMTP} instance has the following methods:
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
\begin{methoddesc}{set_debuglevel}{level}
|
1998-06-30 13:53:52 -03:00
|
|
|
Set the debug output level. A true value for \var{level} results in
|
|
|
|
debug messages for connection and for all messages sent to and
|
|
|
|
received from the server.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
\begin{methoddesc}{connect}{\optional{host\optional{, port}}}
|
|
|
|
Connect to a host on a given port. The defaults are to connect to the
|
|
|
|
local host at the standard SMTP port (25).
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
If the hostname ends with a colon (\character{:}) followed by a
|
|
|
|
number, that suffix will be stripped off and the number interpreted as
|
1998-06-28 14:55:53 -03:00
|
|
|
the port number to use.
|
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
Note: This method is automatically invoked by the constructor if a
|
|
|
|
host is specified during instantiation.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
|
1998-06-30 13:53:52 -03:00
|
|
|
Send a command \var{cmd} to the server. The optional argument
|
|
|
|
\var{argstring} is simply concatenated to the command, separated by a
|
|
|
|
space.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
This returns a 2-tuple composed of a numeric response code and the
|
|
|
|
actual response line (multiline responses are joined into one long
|
|
|
|
line.)
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
In normal operation it should not be necessary to call this method
|
|
|
|
explicitly. It is used to implement other methods and may be useful
|
|
|
|
for testing private extensions.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{helo}{\optional{hostname}}
|
1998-07-01 11:10:52 -03:00
|
|
|
Identify yourself to the SMTP server using \samp{HELO}. The hostname
|
|
|
|
argument defaults to the fully qualified domain name of the local
|
|
|
|
host.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
In normal operation it should not be necessary to call this method
|
1998-06-30 13:53:52 -03:00
|
|
|
explicitly. It will be implicitly called by the \method{sendmail()}
|
1998-06-28 14:55:53 -03:00
|
|
|
when necessary.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{ehlo}{\optional{hostname}}
|
1998-07-01 11:10:52 -03:00
|
|
|
Identify yourself to an ESMTP server using \samp{HELO}. The hostname
|
|
|
|
argument defaults to the fully qualified domain name of the local
|
|
|
|
host. Examine the response for ESMTP option and store them for use by
|
1998-06-30 13:53:52 -03:00
|
|
|
\method{has_option()}.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
Unless you wish to use \method{has_option()} before sending
|
1998-06-28 14:55:53 -03:00
|
|
|
mail, it should not be necessary to call this method explicitly. It
|
1998-06-30 13:53:52 -03:00
|
|
|
will be implicitly called by \method{sendmail()} when necessary.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{has_option}{name}
|
1998-06-30 13:53:52 -03:00
|
|
|
Return \code{1} if \var{name} is in the set of ESMTP options returned
|
|
|
|
by the server, \code{0} otherwise. Case is ignored.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{verify}{address}
|
1998-07-01 11:10:52 -03:00
|
|
|
Check the validity of an address on this server using SMTP \samp{VRFY}.
|
1998-06-30 13:53:52 -03:00
|
|
|
Returns a tuple consisting of code 250 and a full \rfc{822} address
|
1998-06-28 14:55:53 -03:00
|
|
|
(including human name) if the user address is valid. Otherwise returns
|
|
|
|
an SMTP error code of 400 or greater and an error string.
|
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
Note: many sites disable SMTP \samp{VRFY} in order to foil spammers.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options}}
|
|
|
|
Send mail. The required arguments are an \rfc{822} from-address
|
|
|
|
string, a list of \rfc{822} to-address strings, and a message string.
|
|
|
|
The caller may pass a list of ESMTP options to be used in \samp{MAIL
|
|
|
|
FROM} commands as \var{options}.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-07-01 11:10:52 -03:00
|
|
|
If there has been no previous \samp{EHLO} or \samp{HELO} command this
|
|
|
|
session, this method tries ESMTP \samp{EHLO} first. If the server does
|
|
|
|
ESMTP, message size and each of the specified options will be passed
|
|
|
|
to it (if the option is in the feature set the server advertises). If
|
|
|
|
\samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options
|
|
|
|
suppressed.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
This method will return normally if the mail is accepted for at least
|
|
|
|
one recipient. Otherwise it will throw an exception (either
|
1998-06-30 13:53:52 -03:00
|
|
|
\exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or
|
1998-07-01 11:10:52 -03:00
|
|
|
\exception{SMTPDataError}). That is, if this method does not throw an
|
|
|
|
exception, then someone should get your mail. If this method does not
|
|
|
|
throw an exception, it returns a dictionary, with one entry for each
|
|
|
|
recipient that was refused.
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{quit}{}
|
|
|
|
Terminate the SMTP session and close the connection.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
Low-level methods corresponding to the standard SMTP/ESMTP commands
|
1998-07-01 11:10:52 -03:00
|
|
|
\samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and
|
|
|
|
\samp{DATA} are also supported. Normally these do not need to be
|
|
|
|
called directly, so they are not documented here. For details,
|
|
|
|
consult the module code.
|
1998-06-28 14:55:53 -03:00
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
|
1998-08-07 13:03:32 -03:00
|
|
|
\subsection{SMTP Example \label{SMTP-example}}
|
1998-06-30 13:53:52 -03:00
|
|
|
|
|
|
|
% really need a little description here...
|
1998-06-28 14:55:53 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1998-08-07 13:03:32 -03:00
|
|
|
import rfc822, string, sys
|
1998-11-30 11:07:26 -04:00
|
|
|
import smtplib
|
1998-06-30 13:53:52 -03:00
|
|
|
|
|
|
|
def prompt(prompt):
|
|
|
|
sys.stdout.write(prompt + ": ")
|
|
|
|
return string.strip(sys.stdin.readline())
|
|
|
|
|
|
|
|
fromaddr = prompt("From")
|
|
|
|
toaddrs = string.splitfields(prompt("To"), ',')
|
|
|
|
print "Enter message, end with ^D:"
|
|
|
|
msg = ''
|
|
|
|
while 1:
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
msg = msg + line
|
|
|
|
print "Message length is " + `len(msg)`
|
|
|
|
|
1998-11-30 11:07:26 -04:00
|
|
|
server = smtplib.SMTP('localhost')
|
1998-06-30 13:53:52 -03:00
|
|
|
server.set_debuglevel(1)
|
|
|
|
server.sendmail(fromaddr, toaddrs, msg)
|
|
|
|
server.quit()
|
1998-06-28 14:55:53 -03:00
|
|
|
\end{verbatim}
|
|
|
|
|
1998-06-30 13:53:52 -03:00
|
|
|
|
|
|
|
\begin{seealso}
|
|
|
|
\seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available
|
|
|
|
online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.}
|
|
|
|
|
|
|
|
\seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online
|
|
|
|
at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.}
|
|
|
|
\end{seealso}
|