mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r53304 | vinay.sajip | 2007-01-09 15:50:28 +0100 (Tue, 09 Jan 2007) | 1 line Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding". ........ r53305 | vinay.sajip | 2007-01-09 15:51:36 +0100 (Tue, 09 Jan 2007) | 1 line Added entry about addition of _open() method to logging.FileHandler. ........ r53306 | vinay.sajip | 2007-01-09 15:54:56 +0100 (Tue, 09 Jan 2007) | 1 line Added a docstring ........ r53316 | thomas.heller | 2007-01-09 20:19:33 +0100 (Tue, 09 Jan 2007) | 4 lines Verify the sizes of the basic ctypes data types against the struct module. Will backport to release25-maint. ........ r53340 | gustavo.niemeyer | 2007-01-10 17:13:40 +0100 (Wed, 10 Jan 2007) | 3 lines Mention in the int() docstring that a base zero has meaning, as stated in http://docs.python.org/lib/built-in-funcs.html as well. ........ r53341 | gustavo.niemeyer | 2007-01-10 17:15:48 +0100 (Wed, 10 Jan 2007) | 2 lines Minor change in int() docstring for proper spacing. ........ r53358 | thomas.heller | 2007-01-10 21:12:13 +0100 (Wed, 10 Jan 2007) | 1 line Change the ctypes version number to "1.1.0". ........ r53361 | thomas.heller | 2007-01-10 21:51:19 +0100 (Wed, 10 Jan 2007) | 1 line Must change the version number in the _ctypes extension as well. ........ r53362 | guido.van.rossum | 2007-01-11 00:12:56 +0100 (Thu, 11 Jan 2007) | 3 lines Fix the signature of log_error(). (A subclass that did the right thing was getting complaints from pychecker.) ........ r53370 | matthias.klose | 2007-01-11 11:26:31 +0100 (Thu, 11 Jan 2007) | 2 lines - Make the documentation match the code and the docstring ........ r53375 | matthias.klose | 2007-01-11 12:44:04 +0100 (Thu, 11 Jan 2007) | 2 lines - idle: Honor the "Cancel" action in the save dialog (Debian bug #299092). ........ r53381 | raymond.hettinger | 2007-01-11 19:22:55 +0100 (Thu, 11 Jan 2007) | 1 line SF #1486663 -- Allow keyword args in subclasses of set() and frozenset(). ........ r53388 | thomas.heller | 2007-01-11 22:18:56 +0100 (Thu, 11 Jan 2007) | 4 lines Fixes for 64-bit Windows: In ctypes.wintypes, correct the definitions of HANDLE, WPARAM, LPARAM data types. Make parameterless foreign function calls work. ........ r53390 | thomas.heller | 2007-01-11 22:23:12 +0100 (Thu, 11 Jan 2007) | 2 lines Correct the comments: the code is right. ........ r53393 | brett.cannon | 2007-01-12 08:27:52 +0100 (Fri, 12 Jan 2007) | 3 lines Fix error where the end of a funcdesc environment was accidentally moved too far down. ........ r53397 | anthony.baxter | 2007-01-12 10:35:56 +0100 (Fri, 12 Jan 2007) | 3 lines add parsetok.h as a dependency - previously, changing this file doesn't cause the right files to be rebuilt. ........ r53401 | thomas.heller | 2007-01-12 21:08:19 +0100 (Fri, 12 Jan 2007) | 3 lines Avoid warnings in the test suite because ctypes.wintypes cannot be imported on non-windows systems. ........ r53402 | thomas.heller | 2007-01-12 21:17:34 +0100 (Fri, 12 Jan 2007) | 6 lines patch #1610795: BSD version of ctypes.util.find_library, by Martin Kammerhofer. release25-maint backport candidate, but the release manager has to decide. ........ r53403 | thomas.heller | 2007-01-12 21:21:53 +0100 (Fri, 12 Jan 2007) | 3 lines patch #1610795: BSD version of ctypes.util.find_library, by Martin Kammerhofer. ........ r53406 | brett.cannon | 2007-01-13 01:29:49 +0100 (Sat, 13 Jan 2007) | 2 lines Deprecate the sets module. ........ r53407 | georg.brandl | 2007-01-13 13:31:51 +0100 (Sat, 13 Jan 2007) | 3 lines Fix typo. ........ r53409 | marc-andre.lemburg | 2007-01-13 22:00:08 +0100 (Sat, 13 Jan 2007) | 16 lines Bump version number and change copyright year. Add new API linux_distribution() which supports reading the full distribution name and also knows how to parse LSB-style release files. Redirect the old dist() API to the new API (using the short distribution name taken from the release file filename). Add branch and revision to _sys_version(). Add work-around for Cygwin to libc_ver(). Add support for IronPython (thanks for Anthony Baxter) and make Jython support more robust. ........ r53410 | neal.norwitz | 2007-01-13 22:22:37 +0100 (Sat, 13 Jan 2007) | 1 line Fix grammar in docstrings ........ r53411 | marc-andre.lemburg | 2007-01-13 23:32:21 +0100 (Sat, 13 Jan 2007) | 9 lines Add parameter sys_version to _sys_version(). Change the cache for _sys_version() to take the parameter into account. Add support for parsing the IronPython 1.0.1 sys.version value - even though it still returns '1.0.0'; the version string no longer includes the patch level. ........ r53412 | peter.astrand | 2007-01-13 23:35:35 +0100 (Sat, 13 Jan 2007) | 1 line Fix for bug #1634343: allow specifying empty arguments on Windows ........ r53414 | marc-andre.lemburg | 2007-01-13 23:59:36 +0100 (Sat, 13 Jan 2007) | 14 lines Add Python implementation to the machine details. Pretty-print the Python version used for running PyBench. Let the user know when calibration has finished. [ 1563844 ] pybench support for IronPython: Simplify Unicode version detection. Make garbage collection and check interval settings optional if the Python implementation doesn't support thess (e.g. IronPython). ........ r53415 | marc-andre.lemburg | 2007-01-14 00:13:54 +0100 (Sun, 14 Jan 2007) | 5 lines Use defaults if sys.executable isn't set (e.g. on Jython). This change allows running PyBench under Jython. ........ r53416 | marc-andre.lemburg | 2007-01-14 00:15:33 +0100 (Sun, 14 Jan 2007) | 3 lines Jython doesn't have sys.setcheckinterval() - ignore it in that case. ........ r53420 | gerhard.haering | 2007-01-14 02:43:50 +0100 (Sun, 14 Jan 2007) | 29 lines Merged changes from standalone version 2.3.3. This should probably all be merged into the 2.5 maintenance branch: - self->statement was not checked while fetching data, which could lead to crashes if you used the pysqlite API in unusual ways. Closing the cursor and continuing to fetch data was enough. - Converters are stored in a converters dictionary. The converter name is uppercased first. The old upper-casing algorithm was wrong and was replaced by a simple call to the Python string's upper() method instead. -Applied patch by Glyph Lefkowitz that fixes the problem with subsequent SQLITE_SCHEMA errors. - Improvement to the row type: rows can now be iterated over and have a keys() method. This improves compatibility with both tuple and dict a lot. - A bugfix for the subsecond resolution in timestamps. - Corrected the way the flags PARSE_DECLTYPES and PARSE_COLNAMES are checked for. Now they work as documented. - gcc on Linux sucks. It exports all symbols by default in shared libraries, so if symbols are not unique it can lead to problems with symbol lookup. pysqlite used to crash under Apache when mod_cache was enabled because both modules had the symbol cache_init. I fixed this by applying the prefix pysqlite_ almost everywhere. Sigh. ........ r53423 | guido.van.rossum | 2007-01-14 04:46:33 +0100 (Sun, 14 Jan 2007) | 2 lines Remove a dependency of this test on $COLUMNS. ........ r53425 | ka-ping.yee | 2007-01-14 05:25:15 +0100 (Sun, 14 Jan 2007) | 3 lines Handle old-style instances more gracefully (display documentation on the relevant class instead of documentation on <type 'instance'>). ........ r53440 | vinay.sajip | 2007-01-14 22:49:59 +0100 (Sun, 14 Jan 2007) | 1 line Added WatchedFileHandler (based on SF patch #1598415) ........ r53441 | vinay.sajip | 2007-01-14 22:50:50 +0100 (Sun, 14 Jan 2007) | 1 line Added documentation for WatchedFileHandler (based on SF patch #1598415) ........ r53442 | guido.van.rossum | 2007-01-15 01:02:35 +0100 (Mon, 15 Jan 2007) | 2 lines Doc patch matching r53434 (htonl etc. now always take/return positive ints). ........
313 lines
12 KiB
TeX
313 lines
12 KiB
TeX
\section{\module{smtplib} ---
|
|
SMTP protocol client}
|
|
|
|
\declaremodule{standard}{smtplib}
|
|
\modulesynopsis{SMTP protocol client (requires sockets).}
|
|
\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
|
|
|
|
\indexii{SMTP}{protocol}
|
|
\index{Simple Mail Transfer Protocol}
|
|
|
|
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
|
|
listener daemon. For details of SMTP and ESMTP operation, consult
|
|
\rfc{821} (\citetitle{Simple Mail Transfer Protocol}) and \rfc{1869}
|
|
(\citetitle{SMTP Service Extensions}).
|
|
|
|
\begin{classdesc}{SMTP}{\optional{host\optional{, port\optional{,
|
|
local_hostname}}}}
|
|
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
|
|
SMTP \method{connect()} method is called with those parameters during
|
|
initialization. An \exception{SMTPConnectError} is raised if the
|
|
specified host doesn't respond correctly.
|
|
|
|
For normal use, you should only require the initialization/connect,
|
|
\method{sendmail()}, and \method{quit()} methods. An example is
|
|
included below.
|
|
\end{classdesc}
|
|
|
|
\begin{classdesc}{SMTP_SSL}{\optional{host\optional{, port\optional{,
|
|
local_hostname\optional{,
|
|
keyfile\optional{,
|
|
certfile}}}}}}
|
|
A \class{SMTP_SSL} instance behaves exactly the same as instances of \class{SMTP}.
|
|
\class{SMTP_SSL} should be used for situations where SSL is required from
|
|
the beginning of the connection and using \method{starttls()} is not appropriate.
|
|
If \var{host} is not specified, the local host is used. If \var{port} is
|
|
omitted, the standard SMTP-over-SSL port (465) is used. \var{keyfile} and \var{certfile}
|
|
are also optional, and can contain a PEM formatted private key and
|
|
certificate chain file for the SSL connection.
|
|
\end{classdesc}
|
|
|
|
A nice selection of exceptions is defined as well:
|
|
|
|
\begin{excdesc}{SMTPException}
|
|
Base exception class for all exceptions raised by this module.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPServerDisconnected}
|
|
This exception is raised when the server unexpectedly disconnects,
|
|
or when an attempt is made to use the \class{SMTP} instance before
|
|
connecting it to a server.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPResponseException}
|
|
Base class for all exceptions that include an SMTP error code.
|
|
These exceptions are generated in some instances when the SMTP
|
|
server returns an error code. The error code is stored in the
|
|
\member{smtp_code} attribute of the error, and the
|
|
\member{smtp_error} attribute is set to the error message.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPSenderRefused}
|
|
Sender address refused. In addition to the attributes set by on all
|
|
\exception{SMTPResponseException} exceptions, this sets `sender' to
|
|
the string that the SMTP server refused.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPRecipientsRefused}
|
|
All recipient addresses refused. The errors for each recipient are
|
|
accessible through the attribute \member{recipients}, which is a
|
|
dictionary of exactly the same sort as \method{SMTP.sendmail()}
|
|
returns.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPDataError}
|
|
The SMTP server refused to accept the message data.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPConnectError}
|
|
Error occurred during establishment of a connection with the server.
|
|
\end{excdesc}
|
|
|
|
\begin{excdesc}{SMTPHeloError}
|
|
The server refused our \samp{HELO} message.
|
|
\end{excdesc}
|
|
|
|
|
|
\begin{seealso}
|
|
\seerfc{821}{Simple Mail Transfer Protocol}{Protocol definition for
|
|
SMTP. This document covers the model, operating procedure,
|
|
and protocol details for SMTP.}
|
|
\seerfc{1869}{SMTP Service Extensions}{Definition of the ESMTP
|
|
extensions for SMTP. This describes a framework for
|
|
extending SMTP with new commands, supporting dynamic
|
|
discovery of the commands provided by the server, and
|
|
defines a few additional commands.}
|
|
\end{seealso}
|
|
|
|
|
|
\subsection{SMTP Objects \label{SMTP-objects}}
|
|
|
|
An \class{SMTP} instance has the following methods:
|
|
|
|
\begin{methoddesc}{set_debuglevel}{level}
|
|
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.
|
|
\end{methoddesc}
|
|
|
|
\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).
|
|
If the hostname ends with a colon (\character{:}) followed by a
|
|
number, that suffix will be stripped off and the number interpreted as
|
|
the port number to use.
|
|
This method is automatically invoked by the constructor if a
|
|
host is specified during instantiation.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{docmd}{cmd, \optional{, argstring}}
|
|
Send a command \var{cmd} to the server. The optional argument
|
|
\var{argstring} is simply concatenated to the command, separated by a
|
|
space.
|
|
|
|
This returns a 2-tuple composed of a numeric response code and the
|
|
actual response line (multiline responses are joined into one long
|
|
line.)
|
|
|
|
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.
|
|
|
|
If the connection to the server is lost while waiting for the reply,
|
|
\exception{SMTPServerDisconnected} will be raised.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{helo}{\optional{hostname}}
|
|
Identify yourself to the SMTP server using \samp{HELO}. The hostname
|
|
argument defaults to the fully qualified domain name of the local
|
|
host.
|
|
|
|
In normal operation it should not be necessary to call this method
|
|
explicitly. It will be implicitly called by the \method{sendmail()}
|
|
when necessary.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{ehlo}{\optional{hostname}}
|
|
Identify yourself to an ESMTP server using \samp{EHLO}. 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
|
|
\method{has_extn()}.
|
|
|
|
Unless you wish to use \method{has_extn()} before sending
|
|
mail, it should not be necessary to call this method explicitly. It
|
|
will be implicitly called by \method{sendmail()} when necessary.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{has_extn}{name}
|
|
Return \constant{True} if \var{name} is in the set of SMTP service
|
|
extensions returned by the server, \constant{False} otherwise.
|
|
Case is ignored.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{verify}{address}
|
|
Check the validity of an address on this server using SMTP \samp{VRFY}.
|
|
Returns a tuple consisting of code 250 and a full \rfc{822} address
|
|
(including human name) if the user address is valid. Otherwise returns
|
|
an SMTP error code of 400 or greater and an error string.
|
|
|
|
\note{Many sites disable SMTP \samp{VRFY} in order to foil spammers.}
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{login}{user, password}
|
|
Log in on an SMTP server that requires authentication.
|
|
The arguments are the username and the password to authenticate with.
|
|
If there has been no previous \samp{EHLO} or \samp{HELO} command this
|
|
session, this method tries ESMTP \samp{EHLO} first.
|
|
This method will return normally if the authentication was successful,
|
|
or may raise the following exceptions:
|
|
|
|
\begin{description}
|
|
\item[\exception{SMTPHeloError}]
|
|
The server didn't reply properly to the \samp{HELO} greeting.
|
|
\item[\exception{SMTPAuthenticationError}]
|
|
The server didn't accept the username/password combination.
|
|
\item[\exception{SMTPException}]
|
|
No suitable authentication method was found.
|
|
\end{description}
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{starttls}{\optional{keyfile\optional{, certfile}}}
|
|
Put the SMTP connection in TLS (Transport Layer Security) mode. All
|
|
SMTP commands that follow will be encrypted. You should then call
|
|
\method{ehlo()} again.
|
|
|
|
If \var{keyfile} and \var{certfile} are provided, these are passed to
|
|
the \refmodule{socket} module's \function{ssl()} function.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{,
|
|
mail_options, rcpt_options}}
|
|
Send mail. The required arguments are an \rfc{822} from-address
|
|
string, a list of \rfc{822} to-address strings (a bare string will be
|
|
treated as a list with 1 address), and a message string. The caller
|
|
may pass a list of ESMTP options (such as \samp{8bitmime}) to be used
|
|
in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP options
|
|
(such as \samp{DSN} commands) that should be used with all \samp{RCPT}
|
|
commands can be passed as \var{rcpt_options}. (If you need to use
|
|
different ESMTP options to different recipients you have to use the
|
|
low-level methods such as \method{mail}, \method{rcpt} and
|
|
\method{data} to send the message.)
|
|
|
|
\note{The \var{from_addr} and \var{to_addrs} parameters are
|
|
used to construct the message envelope used by the transport agents.
|
|
The \class{SMTP} does not modify the message headers in any way.}
|
|
|
|
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.
|
|
|
|
This method will return normally if the mail is accepted for at least
|
|
one recipient. Otherwise it will throw an exception. 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. Each entry
|
|
contains a tuple of the SMTP error code and the accompanying error
|
|
message sent by the server.
|
|
|
|
This method may raise the following exceptions:
|
|
|
|
\begin{description}
|
|
\item[\exception{SMTPRecipientsRefused}]
|
|
All recipients were refused. Nobody got the mail. The
|
|
\member{recipients} attribute of the exception object is a dictionary
|
|
with information about the refused recipients (like the one returned
|
|
when at least one recipient was accepted).
|
|
|
|
\item[\exception{SMTPHeloError}]
|
|
The server didn't reply properly to the \samp{HELO} greeting.
|
|
|
|
\item[\exception{SMTPSenderRefused}]
|
|
The server didn't accept the \var{from_addr}.
|
|
|
|
\item[\exception{SMTPDataError}]
|
|
The server replied with an unexpected error code (other than a refusal
|
|
of a recipient).
|
|
\end{description}
|
|
|
|
Unless otherwise noted, the connection will be open even after
|
|
an exception is raised.
|
|
|
|
\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
|
|
\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.
|
|
|
|
|
|
\subsection{SMTP Example \label{SMTP-example}}
|
|
|
|
This example prompts the user for addresses needed in the message
|
|
envelope (`To' and `From' addresses), and the message to be
|
|
delivered. Note that the headers to be included with the message must
|
|
be included in the message as entered; this example doesn't do any
|
|
processing of the \rfc{822} headers. In particular, the `To' and
|
|
`From' addresses must be included in the message headers explicitly.
|
|
|
|
\begin{verbatim}
|
|
import smtplib
|
|
|
|
def raw_input(prompt):
|
|
import sys
|
|
sys.stdout.write(prompt)
|
|
sys.stdout.flush()
|
|
return sys.stdin.readline()
|
|
|
|
def prompt(prompt):
|
|
return raw_input(prompt).strip()
|
|
|
|
fromaddr = prompt("From: ")
|
|
toaddrs = prompt("To: ").split()
|
|
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
|
|
|
|
# Add the From: and To: headers at the start!
|
|
msg = ("From: %s\r\nTo: %s\r\n\r\n"
|
|
% (fromaddr, ", ".join(toaddrs)))
|
|
while 1:
|
|
try:
|
|
line = raw_input()
|
|
except EOFError:
|
|
break
|
|
if not line:
|
|
break
|
|
msg = msg + line
|
|
|
|
print "Message length is " + repr(len(msg))
|
|
|
|
server = smtplib.SMTP('localhost')
|
|
server.set_debuglevel(1)
|
|
server.sendmail(fromaddr, toaddrs, msg)
|
|
server.quit()
|
|
\end{verbatim}
|