2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
:mod:`smtplib` --- SMTP protocol client
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
.. module:: smtplib
|
|
|
|
:synopsis: SMTP protocol client (requires sockets).
|
|
|
|
.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: SMTP; protocol
|
|
|
|
single: Simple Mail Transfer Protocol
|
|
|
|
|
|
|
|
The :mod:`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` (Simple Mail Transfer
|
|
|
|
Protocol) and :rfc:`1869` (SMTP Service Extensions).
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: SMTP([host[, port[, local_hostname[, timeout]]]])
|
|
|
|
|
2008-05-11 08:09:35 -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 SMTP :meth:`connect` method is called
|
|
|
|
with those parameters during initialization. An :exc:`SMTPConnectError` is
|
|
|
|
raised if the specified host doesn't respond correctly. The optional
|
|
|
|
*timeout* parameter specifies a timeout in seconds for blocking operations
|
2008-05-29 13:39:26 -03:00
|
|
|
like the connection attempt (if not specified, the global default timeout
|
|
|
|
setting will be used).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
For normal use, you should only require the initialization/connect,
|
|
|
|
:meth:`sendmail`, and :meth:`quit` methods. An example is included below.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.6
|
|
|
|
*timeout* was added.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]])
|
|
|
|
|
|
|
|
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
|
2008-05-11 08:09:35 -03:00
|
|
|
required from the beginning of the connection and using :meth:`starttls` is
|
|
|
|
not appropriate. If *host* is not specified, the local host is used. If
|
|
|
|
*port* is omitted, the standard SMTP-over-SSL port (465) is used. *keyfile*
|
|
|
|
and *certfile* are also optional, and can contain a PEM formatted private key
|
|
|
|
and certificate chain file for the SSL connection. The optional *timeout*
|
|
|
|
parameter specifies a timeout in seconds for blocking operations like the
|
2008-05-29 13:39:26 -03:00
|
|
|
connection attempt (if not specified, the global default timeout setting
|
|
|
|
will be used).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 2.6
|
|
|
|
*timeout* was added.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: LMTP([host[, port[, local_hostname]]])
|
|
|
|
|
|
|
|
The LMTP protocol, which is very similar to ESMTP, is heavily based on the
|
2007-09-01 17:31:59 -03:00
|
|
|
standard SMTP client. It's common to use Unix sockets for LMTP, so our :meth:`connect`
|
2007-08-15 11:28:01 -03:00
|
|
|
method must support that as well as a regular host:port server. To specify a
|
|
|
|
Unix socket, you must use an absolute path for *host*, starting with a '/'.
|
|
|
|
|
|
|
|
Authentication is supported, using the regular SMTP mechanism. When using a Unix
|
|
|
|
socket, LMTP generally don't support or require any authentication, but your
|
|
|
|
mileage might vary.
|
|
|
|
|
|
|
|
.. versionadded:: 2.6
|
|
|
|
|
|
|
|
A nice selection of exceptions is defined as well:
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPException
|
|
|
|
|
|
|
|
Base exception class for all exceptions raised by this module.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: 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 :attr:`smtp_code` attribute of the error, and the
|
|
|
|
:attr:`smtp_error` attribute is set to the error message.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPSenderRefused
|
|
|
|
|
|
|
|
Sender address refused. In addition to the attributes set by on all
|
|
|
|
:exc:`SMTPResponseException` exceptions, this sets 'sender' to the string that
|
|
|
|
the SMTP server refused.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPRecipientsRefused
|
|
|
|
|
|
|
|
All recipient addresses refused. The errors for each recipient are accessible
|
|
|
|
through the attribute :attr:`recipients`, which is a dictionary of exactly the
|
|
|
|
same sort as :meth:`SMTP.sendmail` returns.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPDataError
|
|
|
|
|
|
|
|
The SMTP server refused to accept the message data.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPConnectError
|
|
|
|
|
|
|
|
Error occurred during establishment of a connection with the server.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPHeloError
|
|
|
|
|
|
|
|
The server refused our ``HELO`` message.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SMTPAuthenticationError
|
|
|
|
|
|
|
|
SMTP authentication went wrong. Most probably the server didn't accept the
|
|
|
|
username/password combination provided.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:rfc:`821` - Simple Mail Transfer Protocol
|
|
|
|
Protocol definition for SMTP. This document covers the model, operating
|
|
|
|
procedure, and protocol details for SMTP.
|
|
|
|
|
|
|
|
:rfc:`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.
|
|
|
|
|
|
|
|
|
|
|
|
.. _smtp-objects:
|
|
|
|
|
|
|
|
SMTP Objects
|
|
|
|
------------
|
|
|
|
|
|
|
|
An :class:`SMTP` instance has the following methods:
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.set_debuglevel(level)
|
|
|
|
|
|
|
|
Set the debug output level. A true value for *level* results in debug messages
|
|
|
|
for connection and for all messages sent to and received from the server.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.connect([host[, 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 (``':'``)
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.docmd(cmd, [, argstring])
|
|
|
|
|
|
|
|
Send a command *cmd* to the server. The optional argument *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,
|
|
|
|
:exc:`SMTPServerDisconnected` will be raised.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.helo([hostname])
|
|
|
|
|
|
|
|
Identify yourself to the SMTP server using ``HELO``. The hostname argument
|
|
|
|
defaults to the fully qualified domain name of the local host.
|
2008-09-06 18:26:02 -03:00
|
|
|
The message returned by the server is stored as the :attr:`helo_resp` attribute
|
|
|
|
of the object.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
In normal operation it should not be necessary to call this method explicitly.
|
|
|
|
It will be implicitly called by the :meth:`sendmail` when necessary.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.ehlo([hostname])
|
|
|
|
|
|
|
|
Identify yourself to an ESMTP server using ``EHLO``. The hostname argument
|
|
|
|
defaults to the fully qualified domain name of the local host. Examine the
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
response for ESMTP option and store them for use by :meth:`has_extn`.
|
|
|
|
Also sets several informational attributes: the message returned by
|
|
|
|
the server is stored as the :attr:`ehlo_resp` attribute, :attr:`does_esmtp`
|
2008-09-06 18:26:02 -03:00
|
|
|
is set to true or false depending on whether the server supports ESMTP, and
|
|
|
|
:attr:`esmtp_features` will be a dictionary containing the names of the
|
|
|
|
SMTP service extensions this server supports, and their
|
|
|
|
parameters (if any).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Unless you wish to use :meth:`has_extn` before sending mail, it should not be
|
|
|
|
necessary to call this method explicitly. It will be implicitly called by
|
|
|
|
:meth:`sendmail` when necessary.
|
|
|
|
|
2008-01-17 04:35:49 -04:00
|
|
|
.. method:: SMTP.ehlo_or_helo_if_needed()
|
|
|
|
|
|
|
|
This method call :meth:`ehlo` and or :meth:`helo` if there has been no
|
|
|
|
previous ``EHLO`` or ``HELO`` command this session. It tries ESMTP ``EHLO``
|
|
|
|
first.
|
|
|
|
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
:exc:`SMTPHeloError`
|
2008-01-17 04:35:49 -04:00
|
|
|
The server didn't reply properly to the ``HELO`` greeting.
|
|
|
|
|
|
|
|
.. versionadded:: 2.6
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. method:: SMTP.has_extn(name)
|
|
|
|
|
|
|
|
Return :const:`True` if *name* is in the set of SMTP service extensions returned
|
|
|
|
by the server, :const:`False` otherwise. Case is ignored.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.verify(address)
|
|
|
|
|
|
|
|
Check the validity of an address on this server using SMTP ``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 ``VRFY`` in order to foil spammers.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.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
|
|
|
|
``EHLO`` or ``HELO`` command this session, this method tries ESMTP ``EHLO``
|
|
|
|
first. This method will return normally if the authentication was successful, or
|
|
|
|
may raise the following exceptions:
|
|
|
|
|
|
|
|
:exc:`SMTPHeloError`
|
|
|
|
The server didn't reply properly to the ``HELO`` greeting.
|
|
|
|
|
|
|
|
:exc:`SMTPAuthenticationError`
|
|
|
|
The server didn't accept the username/password combination.
|
|
|
|
|
|
|
|
:exc:`SMTPException`
|
|
|
|
No suitable authentication method was found.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.starttls([keyfile[, certfile]])
|
|
|
|
|
|
|
|
Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP
|
|
|
|
commands that follow will be encrypted. You should then call :meth:`ehlo`
|
|
|
|
again.
|
|
|
|
|
|
|
|
If *keyfile* and *certfile* are provided, these are passed to the :mod:`socket`
|
|
|
|
module's :func:`ssl` function.
|
|
|
|
|
2008-01-17 04:35:49 -04:00
|
|
|
If there has been no previous ``EHLO`` or ``HELO`` command this session,
|
|
|
|
this method tries ESMTP ``EHLO`` first.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.6
|
|
|
|
|
|
|
|
:exc:`SMTPHeloError`
|
|
|
|
The server didn't reply properly to the ``HELO`` greeting.
|
|
|
|
|
|
|
|
:exc:`SMTPException`
|
|
|
|
The server does not support the STARTTLS extension.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.6
|
|
|
|
|
|
|
|
:exc:`RuntimeError`
|
2009-12-19 18:59:01 -04:00
|
|
|
SSL/TLS support is not available to your Python interpreter.
|
2008-01-17 04:35:49 -04:00
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. method:: SMTP.sendmail(from_addr, to_addrs, msg[, 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 ``8bitmime``) to be used in ``MAIL FROM`` commands as *mail_options*.
|
|
|
|
ESMTP options (such as ``DSN`` commands) that should be used with all ``RCPT``
|
|
|
|
commands can be passed as *rcpt_options*. (If you need to use different ESMTP
|
|
|
|
options to different recipients you have to use the low-level methods such as
|
|
|
|
:meth:`mail`, :meth:`rcpt` and :meth:`data` to send the message.)
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The *from_addr* and *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 ``EHLO`` or ``HELO`` command this session, this
|
|
|
|
method tries ESMTP ``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 ``EHLO`` fails, ``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:
|
|
|
|
|
|
|
|
:exc:`SMTPRecipientsRefused`
|
|
|
|
All recipients were refused. Nobody got the mail. The :attr:`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).
|
|
|
|
|
|
|
|
:exc:`SMTPHeloError`
|
|
|
|
The server didn't reply properly to the ``HELO`` greeting.
|
|
|
|
|
|
|
|
:exc:`SMTPSenderRefused`
|
|
|
|
The server didn't accept the *from_addr*.
|
|
|
|
|
|
|
|
:exc:`SMTPDataError`
|
|
|
|
The server replied with an unexpected error code (other than a refusal of a
|
|
|
|
recipient).
|
|
|
|
|
|
|
|
Unless otherwise noted, the connection will be open even after an exception is
|
|
|
|
raised.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: SMTP.quit()
|
|
|
|
|
2008-03-27 10:27:31 -03:00
|
|
|
Terminate the SMTP session and close the connection. Return the result of
|
|
|
|
the SMTP ``QUIT`` command.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.6
|
|
|
|
Return a value.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Low-level methods corresponding to the standard SMTP/ESMTP commands ``HELP``,
|
|
|
|
``RSET``, ``NOOP``, ``MAIL``, ``RCPT``, and ``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.
|
|
|
|
|
|
|
|
|
|
|
|
.. _smtp-example:
|
|
|
|
|
|
|
|
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. ::
|
|
|
|
|
|
|
|
import smtplib
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
Merged revisions 72319-72320,72467,72661,72675-72679,72703,72708,72710,72712,72801-72802,72820,72822,72824,72826-72828,72830 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72319 | georg.brandl | 2009-05-05 10:28:49 +0200 (Di, 05 Mai 2009) | 1 line
#1309567: fix linecache behavior of stripping subdirectories from paths when looking for relative filename matches. Also add a linecache test suite.
........
r72320 | georg.brandl | 2009-05-05 10:30:28 +0200 (Di, 05 Mai 2009) | 1 line
Add a news entry for r72319.
........
r72467 | georg.brandl | 2009-05-08 14:17:34 +0200 (Fr, 08 Mai 2009) | 1 line
Fix name.
........
r72661 | georg.brandl | 2009-05-15 10:03:03 +0200 (Fr, 15 Mai 2009) | 1 line
Fix example output for doctest-like demos.
........
r72675 | georg.brandl | 2009-05-16 13:13:21 +0200 (Sa, 16 Mai 2009) | 1 line
#6034: clarify __reversed__ doc.
........
r72676 | georg.brandl | 2009-05-16 13:14:46 +0200 (Sa, 16 Mai 2009) | 1 line
#6025: fix signature of parse().
........
r72677 | georg.brandl | 2009-05-16 13:18:55 +0200 (Sa, 16 Mai 2009) | 1 line
#6009: undocument default argument of Option as deprecated.
........
r72678 | georg.brandl | 2009-05-16 13:21:29 +0200 (Sa, 16 Mai 2009) | 1 line
#2856: document 2.x os.listdir() behavior for undecodable filenames.
........
r72679 | georg.brandl | 2009-05-16 13:24:41 +0200 (Sa, 16 Mai 2009) | 1 line
Fix about and bugs pages to match real workflow.
........
r72703 | georg.brandl | 2009-05-17 10:10:27 +0200 (So, 17 Mai 2009) | 1 line
part of #4144: fix exception message in console session.
........
r72708 | georg.brandl | 2009-05-17 10:24:29 +0200 (So, 17 Mai 2009) | 1 line
#6017: better document behavior of dictiterators when the dict is changed.
........
r72710 | georg.brandl | 2009-05-17 10:36:04 +0200 (So, 17 Mai 2009) | 1 line
#5942: Copy over flag table from dbm.rst which is clearer.
........
r72712 | georg.brandl | 2009-05-17 10:55:00 +0200 (So, 17 Mai 2009) | 1 line
#5935: mention that BROWSER is looked for in PATH.
........
r72801 | georg.brandl | 2009-05-20 20:31:14 +0200 (Mi, 20 Mai 2009) | 1 line
#6055: refer to "sqlite3" consistently.
........
r72802 | georg.brandl | 2009-05-20 20:35:27 +0200 (Mi, 20 Mai 2009) | 1 line
#6051: refer to email examples for better way to construct email messages.
........
r72820 | georg.brandl | 2009-05-22 09:23:32 +0200 (Fr, 22 Mai 2009) | 1 line
Use raise X(y).
........
r72822 | georg.brandl | 2009-05-22 11:33:25 +0200 (Fr, 22 Mai 2009) | 1 line
#6084: fix example.
........
r72824 | georg.brandl | 2009-05-22 11:43:17 +0200 (Fr, 22 Mai 2009) | 1 line
Fix references to file-related functions and methods (os.* vs file.*).
........
r72826 | georg.brandl | 2009-05-22 11:49:42 +0200 (Fr, 22 Mai 2009) | 1 line
Fix confusing wording.
........
r72827 | georg.brandl | 2009-05-22 11:50:30 +0200 (Fr, 22 Mai 2009) | 1 line
s/use/call/
........
r72828 | georg.brandl | 2009-05-22 11:58:48 +0200 (Fr, 22 Mai 2009) | 1 line
Correction in softspace behavior description.
........
r72830 | georg.brandl | 2009-05-22 12:40:00 +0200 (Fr, 22 Mai 2009) | 1 line
#6086: fix spelling and use a better exception to catch.
........
2009-05-26 06:04:23 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
In general, you will want to use the :mod:`email` package's features to
|
|
|
|
construct an email message, which you can then convert to a string and send
|
|
|
|
via :meth:`sendmail`; see :ref:`email-examples`.
|