| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`smtplib` --- SMTP protocol client | 
|  | 2 | ======================================= | 
|  | 3 |  | 
|  | 4 | .. module:: smtplib | 
|  | 5 | :synopsis: SMTP protocol client (requires sockets). | 
|  | 6 | .. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com> | 
|  | 7 |  | 
|  | 8 |  | 
|  | 9 | .. index:: | 
|  | 10 | pair: SMTP; protocol | 
|  | 11 | single: Simple Mail Transfer Protocol | 
|  | 12 |  | 
| Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 13 | **Source code:** :source:`Lib/smtplib.py` | 
|  | 14 |  | 
|  | 15 | -------------- | 
|  | 16 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | The :mod:`smtplib` module defines an SMTP client session object that can be used | 
|  | 18 | to send mail to any Internet machine with an SMTP or ESMTP listener daemon.  For | 
|  | 19 | details of SMTP and ESMTP operation, consult :rfc:`821` (Simple Mail Transfer | 
|  | 20 | Protocol) and :rfc:`1869` (SMTP Service Extensions). | 
|  | 21 |  | 
|  | 22 |  | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 23 | .. class:: SMTP(host='', port=0, local_hostname=None[, timeout], source_address=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 |  | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 25 | A :class:`SMTP` instance encapsulates an SMTP connection.  It has methods | 
|  | 26 | that support a full repertoire of SMTP and ESMTP operations. If the optional | 
|  | 27 | host and port parameters are given, the SMTP :meth:`connect` method is called | 
| R David Murray | 14ee3cf | 2013-04-13 14:40:33 -0400 | [diff] [blame^] | 28 | with those parameters during initialization.  If the :meth:`connect` call | 
|  | 29 | returns anything other than a success code, an :exc:`SMTPConnectError` is | 
|  | 30 | raised. The optional | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 31 | *timeout* parameter specifies a timeout in seconds for blocking operations | 
| Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 32 | like the connection attempt (if not specified, the global default timeout | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 33 | setting will be used). The optional source_address parameter allows to bind to some | 
|  | 34 | specific source address in a machine with multiple network interfaces, | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 35 | and/or to some specific source TCP port. It takes a 2-tuple (host, port), | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 36 | for the socket to bind to as its source address before connecting. If | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 37 | omitted (or if host or port are ``''`` and/or 0 respectively) the OS default | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 38 | behavior will be used. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 |  | 
|  | 40 | For normal use, you should only require the initialization/connect, | 
| Jesus Cea | c73f863 | 2012-12-26 16:47:03 +0100 | [diff] [blame] | 41 | :meth:`sendmail`, and :meth:`~smtplib.quit` methods. | 
|  | 42 | An example is included below. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 |  | 
| Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 44 | The :class:`SMTP` class supports the :keyword:`with` statement.  When used | 
|  | 45 | like this, the SMTP ``QUIT`` command is issued automatically when the | 
|  | 46 | :keyword:`with` statement exits.  E.g.:: | 
|  | 47 |  | 
|  | 48 | >>> from smtplib import SMTP | 
|  | 49 | >>> with SMTP("domain.org") as smtp: | 
|  | 50 | ...     smtp.noop() | 
|  | 51 | ... | 
|  | 52 | (250, b'Ok') | 
|  | 53 | >>> | 
|  | 54 |  | 
| Antoine Pitrou | 45456a0 | 2011-04-26 18:53:42 +0200 | [diff] [blame] | 55 | .. versionchanged:: 3.3 | 
| Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 56 | Support for the :keyword:`with` statement was added. | 
|  | 57 |  | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 58 | .. versionchanged:: 3.3 | 
|  | 59 | source_address argument was added. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 |  | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 61 | .. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, certfile=None[, timeout], context=None, source_address=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 |  | 
|  | 63 | A :class:`SMTP_SSL` instance behaves exactly the same as instances of | 
|  | 64 | :class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 65 | required from the beginning of the connection and using :meth:`starttls` is | 
|  | 66 | not appropriate. If *host* is not specified, the local host is used. If | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 67 | *port* is zero, the standard SMTP-over-SSL port (465) is used. *keyfile* | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 68 | and *certfile* are also optional, and can contain a PEM formatted private key | 
| Antoine Pitrou | e065020 | 2011-05-18 18:03:09 +0200 | [diff] [blame] | 69 | and certificate chain file for the SSL connection. *context* also optional, can contain | 
|  | 70 | a SSLContext, and is an alternative to keyfile and certfile; If it is specified both | 
|  | 71 | keyfile and certfile must be None.  The optional *timeout* | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 72 | parameter specifies a timeout in seconds for blocking operations like the | 
| Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 73 | connection attempt (if not specified, the global default timeout setting | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 74 | will be used). The optional source_address parameter allows to bind to some | 
|  | 75 | specific source address in a machine with multiple network interfaces, | 
|  | 76 | and/or to some specific source tcp port. It takes a 2-tuple (host, port), | 
|  | 77 | for the socket to bind to as its source address before connecting. If | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 78 | omitted (or if host or port are ``''`` and/or 0 respectively) the OS default | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 79 | behavior will be used. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 |  | 
| Antoine Pitrou | e065020 | 2011-05-18 18:03:09 +0200 | [diff] [blame] | 81 | .. versionchanged:: 3.3 | 
|  | 82 | *context* was added. | 
|  | 83 |  | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 84 | .. versionchanged:: 3.3 | 
|  | 85 | source_address argument was added. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 |  | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 87 |  | 
|  | 88 | .. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None, source_address=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 |  | 
|  | 90 | The LMTP protocol, which is very similar to ESMTP, is heavily based on the | 
| Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 91 | standard SMTP client. It's common to use Unix sockets for LMTP, so our | 
|  | 92 | :meth:`connect` method must support that as well as a regular host:port | 
| Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 93 | server. The optional arguments local_hostname and source_address have the | 
|  | 94 | same meaning as that of SMTP client. To specify a Unix socket, you must use | 
| Senthil Kumaran | 63d4fb4 | 2011-07-30 10:58:30 +0800 | [diff] [blame] | 95 | an absolute path for *host*, starting with a '/'. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | Authentication is supported, using the regular SMTP mechanism. When using a Unix | 
|  | 98 | socket, LMTP generally don't support or require any authentication, but your | 
|  | 99 | mileage might vary. | 
|  | 100 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 |  | 
|  | 102 | A nice selection of exceptions is defined as well: | 
|  | 103 |  | 
|  | 104 |  | 
|  | 105 | .. exception:: SMTPException | 
|  | 106 |  | 
| R David Murray | 14ee3cf | 2013-04-13 14:40:33 -0400 | [diff] [blame^] | 107 | The base exception class for all the other excpetions provided by this | 
|  | 108 | module. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 |  | 
|  | 110 |  | 
|  | 111 | .. exception:: SMTPServerDisconnected | 
|  | 112 |  | 
|  | 113 | This exception is raised when the server unexpectedly disconnects, or when an | 
|  | 114 | attempt is made to use the :class:`SMTP` instance before connecting it to a | 
|  | 115 | server. | 
|  | 116 |  | 
|  | 117 |  | 
|  | 118 | .. exception:: SMTPResponseException | 
|  | 119 |  | 
|  | 120 | Base class for all exceptions that include an SMTP error code. These exceptions | 
|  | 121 | are generated in some instances when the SMTP server returns an error code.  The | 
|  | 122 | error code is stored in the :attr:`smtp_code` attribute of the error, and the | 
|  | 123 | :attr:`smtp_error` attribute is set to the error message. | 
|  | 124 |  | 
|  | 125 |  | 
|  | 126 | .. exception:: SMTPSenderRefused | 
|  | 127 |  | 
|  | 128 | Sender address refused.  In addition to the attributes set by on all | 
|  | 129 | :exc:`SMTPResponseException` exceptions, this sets 'sender' to the string that | 
|  | 130 | the SMTP server refused. | 
|  | 131 |  | 
|  | 132 |  | 
|  | 133 | .. exception:: SMTPRecipientsRefused | 
|  | 134 |  | 
|  | 135 | All recipient addresses refused.  The errors for each recipient are accessible | 
|  | 136 | through the attribute :attr:`recipients`, which is a dictionary of exactly the | 
|  | 137 | same sort as :meth:`SMTP.sendmail` returns. | 
|  | 138 |  | 
|  | 139 |  | 
|  | 140 | .. exception:: SMTPDataError | 
|  | 141 |  | 
|  | 142 | The SMTP server refused to accept the message data. | 
|  | 143 |  | 
|  | 144 |  | 
|  | 145 | .. exception:: SMTPConnectError | 
|  | 146 |  | 
|  | 147 | Error occurred during establishment of a connection  with the server. | 
|  | 148 |  | 
|  | 149 |  | 
|  | 150 | .. exception:: SMTPHeloError | 
|  | 151 |  | 
|  | 152 | The server refused our ``HELO`` message. | 
|  | 153 |  | 
|  | 154 |  | 
|  | 155 | .. exception:: SMTPAuthenticationError | 
|  | 156 |  | 
|  | 157 | SMTP authentication went wrong.  Most probably the server didn't accept the | 
|  | 158 | username/password combination provided. | 
|  | 159 |  | 
|  | 160 |  | 
|  | 161 | .. seealso:: | 
|  | 162 |  | 
|  | 163 | :rfc:`821` - Simple Mail Transfer Protocol | 
|  | 164 | Protocol definition for SMTP.  This document covers the model, operating | 
|  | 165 | procedure, and protocol details for SMTP. | 
|  | 166 |  | 
|  | 167 | :rfc:`1869` - SMTP Service Extensions | 
|  | 168 | Definition of the ESMTP extensions for SMTP.  This describes a framework for | 
|  | 169 | extending SMTP with new commands, supporting dynamic discovery of the commands | 
|  | 170 | provided by the server, and defines a few additional commands. | 
|  | 171 |  | 
|  | 172 |  | 
|  | 173 | .. _smtp-objects: | 
|  | 174 |  | 
|  | 175 | SMTP Objects | 
|  | 176 | ------------ | 
|  | 177 |  | 
|  | 178 | An :class:`SMTP` instance has the following methods: | 
|  | 179 |  | 
|  | 180 |  | 
|  | 181 | .. method:: SMTP.set_debuglevel(level) | 
|  | 182 |  | 
|  | 183 | Set the debug output level.  A true value for *level* results in debug messages | 
|  | 184 | for connection and for all messages sent to and received from the server. | 
|  | 185 |  | 
|  | 186 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 187 | .. method:: SMTP.docmd(cmd, args='') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 189 | Send a command *cmd* to the server.  The optional argument *args* is simply | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | concatenated to the command, separated by a space. | 
|  | 191 |  | 
|  | 192 | This returns a 2-tuple composed of a numeric response code and the actual | 
|  | 193 | response line (multiline responses are joined into one long line.) | 
|  | 194 |  | 
|  | 195 | In normal operation it should not be necessary to call this method explicitly. | 
|  | 196 | It is used to implement other methods and may be useful for testing private | 
|  | 197 | extensions. | 
|  | 198 |  | 
|  | 199 | If the connection to the server is lost while waiting for the reply, | 
|  | 200 | :exc:`SMTPServerDisconnected` will be raised. | 
|  | 201 |  | 
|  | 202 |  | 
| R David Murray | 14ee3cf | 2013-04-13 14:40:33 -0400 | [diff] [blame^] | 203 | .. method:: SMTP.connect(host='localhost', port=0) | 
|  | 204 |  | 
|  | 205 | Connect to a host on a given port.  The defaults are to connect to the local | 
|  | 206 | host at the standard SMTP port (25). If the hostname ends with a colon (``':'``) | 
|  | 207 | followed by a number, that suffix will be stripped off and the number | 
|  | 208 | interpreted as the port number to use. This method is automatically invoked by | 
|  | 209 | the constructor if a host is specified during instantiation.  Returns a | 
|  | 210 | 2-tuple of the response code and message sent by the server in its | 
|  | 211 | connection response. | 
|  | 212 |  | 
|  | 213 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 214 | .. method:: SMTP.helo(name='') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 |  | 
|  | 216 | Identify yourself to the SMTP server using ``HELO``.  The hostname argument | 
|  | 217 | defaults to the fully qualified domain name of the local host. | 
| Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 218 | The message returned by the server is stored as the :attr:`helo_resp` attribute | 
|  | 219 | of the object. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 |  | 
|  | 221 | In normal operation it should not be necessary to call this method explicitly. | 
|  | 222 | It will be implicitly called by the :meth:`sendmail` when necessary. | 
|  | 223 |  | 
|  | 224 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 225 | .. method:: SMTP.ehlo(name='') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 |  | 
|  | 227 | Identify yourself to an ESMTP server using ``EHLO``.  The hostname argument | 
|  | 228 | defaults to the fully qualified domain name of the local host.  Examine the | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 229 | response for ESMTP option and store them for use by :meth:`has_extn`. | 
|  | 230 | Also sets several informational attributes: the message returned by | 
|  | 231 | the server is stored as the :attr:`ehlo_resp` attribute, :attr:`does_esmtp` | 
| Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 232 | is set to true or false depending on whether the server supports ESMTP, and | 
|  | 233 | :attr:`esmtp_features` will be a dictionary containing the names of the | 
|  | 234 | SMTP service extensions this server supports, and their | 
|  | 235 | parameters (if any). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 |  | 
|  | 237 | Unless you wish to use :meth:`has_extn` before sending mail, it should not be | 
|  | 238 | necessary to call this method explicitly.  It will be implicitly called by | 
|  | 239 | :meth:`sendmail` when necessary. | 
|  | 240 |  | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 241 | .. method:: SMTP.ehlo_or_helo_if_needed() | 
|  | 242 |  | 
|  | 243 | This method call :meth:`ehlo` and or :meth:`helo` if there has been no | 
|  | 244 | previous ``EHLO`` or ``HELO`` command this session.  It tries ESMTP ``EHLO`` | 
|  | 245 | first. | 
|  | 246 |  | 
| Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 247 | :exc:`SMTPHeloError` | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 248 | The server didn't reply properly to the ``HELO`` greeting. | 
|  | 249 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | .. method:: SMTP.has_extn(name) | 
|  | 251 |  | 
|  | 252 | Return :const:`True` if *name* is in the set of SMTP service extensions returned | 
|  | 253 | by the server, :const:`False` otherwise. Case is ignored. | 
|  | 254 |  | 
|  | 255 |  | 
|  | 256 | .. method:: SMTP.verify(address) | 
|  | 257 |  | 
|  | 258 | Check the validity of an address on this server using SMTP ``VRFY``. Returns a | 
|  | 259 | tuple consisting of code 250 and a full :rfc:`822` address (including human | 
|  | 260 | name) if the user address is valid. Otherwise returns an SMTP error code of 400 | 
|  | 261 | or greater and an error string. | 
|  | 262 |  | 
|  | 263 | .. note:: | 
|  | 264 |  | 
|  | 265 | Many sites disable SMTP ``VRFY`` in order to foil spammers. | 
|  | 266 |  | 
|  | 267 |  | 
|  | 268 | .. method:: SMTP.login(user, password) | 
|  | 269 |  | 
|  | 270 | Log in on an SMTP server that requires authentication. The arguments are the | 
|  | 271 | username and the password to authenticate with. If there has been no previous | 
|  | 272 | ``EHLO`` or ``HELO`` command this session, this method tries ESMTP ``EHLO`` | 
|  | 273 | first. This method will return normally if the authentication was successful, or | 
|  | 274 | may raise the following exceptions: | 
|  | 275 |  | 
|  | 276 | :exc:`SMTPHeloError` | 
|  | 277 | The server didn't reply properly to the ``HELO`` greeting. | 
|  | 278 |  | 
|  | 279 | :exc:`SMTPAuthenticationError` | 
|  | 280 | The server didn't accept the username/password combination. | 
|  | 281 |  | 
|  | 282 | :exc:`SMTPException` | 
|  | 283 | No suitable authentication method was found. | 
|  | 284 |  | 
|  | 285 |  | 
| Antoine Pitrou | e065020 | 2011-05-18 18:03:09 +0200 | [diff] [blame] | 286 | .. method:: SMTP.starttls(keyfile=None, certfile=None, context=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 |  | 
|  | 288 | Put the SMTP connection in TLS (Transport Layer Security) mode.  All SMTP | 
|  | 289 | commands that follow will be encrypted.  You should then call :meth:`ehlo` | 
|  | 290 | again. | 
|  | 291 |  | 
|  | 292 | If *keyfile* and *certfile* are provided, these are passed to the :mod:`socket` | 
|  | 293 | module's :func:`ssl` function. | 
|  | 294 |  | 
| Antoine Pitrou | e065020 | 2011-05-18 18:03:09 +0200 | [diff] [blame] | 295 | Optional *context* parameter is a :class:`ssl.SSLContext` object; This is an alternative to | 
|  | 296 | using a keyfile and a certfile and if specified both *keyfile* and *certfile* should be None. | 
|  | 297 |  | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 298 | If there has been no previous ``EHLO`` or ``HELO`` command this session, | 
|  | 299 | this method tries ESMTP ``EHLO`` first. | 
|  | 300 |  | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 301 | :exc:`SMTPHeloError` | 
|  | 302 | The server didn't reply properly to the ``HELO`` greeting. | 
|  | 303 |  | 
|  | 304 | :exc:`SMTPException` | 
|  | 305 | The server does not support the STARTTLS extension. | 
|  | 306 |  | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 307 | :exc:`RuntimeError` | 
| Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 308 | SSL/TLS support is not available to your Python interpreter. | 
| Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 309 |  | 
| Antoine Pitrou | e065020 | 2011-05-18 18:03:09 +0200 | [diff] [blame] | 310 | .. versionchanged:: 3.3 | 
|  | 311 | *context* was added. | 
|  | 312 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 314 | .. method:: SMTP.sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 |  | 
|  | 316 | Send mail.  The required arguments are an :rfc:`822` from-address string, a list | 
|  | 317 | of :rfc:`822` to-address strings (a bare string will be treated as a list with 1 | 
|  | 318 | address), and a message string.  The caller may pass a list of ESMTP options | 
|  | 319 | (such as ``8bitmime``) to be used in ``MAIL FROM`` commands as *mail_options*. | 
|  | 320 | ESMTP options (such as ``DSN`` commands) that should be used with all ``RCPT`` | 
|  | 321 | commands can be passed as *rcpt_options*.  (If you need to use different ESMTP | 
|  | 322 | options to different recipients you have to use the low-level methods such as | 
|  | 323 | :meth:`mail`, :meth:`rcpt` and :meth:`data` to send the message.) | 
|  | 324 |  | 
|  | 325 | .. note:: | 
|  | 326 |  | 
|  | 327 | The *from_addr* and *to_addrs* parameters are used to construct the message | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 328 | envelope used by the transport agents.  ``sendmail`` does not modify the | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | message headers in any way. | 
|  | 330 |  | 
| Benjamin Peterson | a6c4a10 | 2011-12-30 23:08:09 -0600 | [diff] [blame] | 331 | *msg* may be a string containing characters in the ASCII range, or a byte | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 332 | string.  A string is encoded to bytes using the ascii codec, and lone ``\r`` | 
| Benjamin Peterson | a6c4a10 | 2011-12-30 23:08:09 -0600 | [diff] [blame] | 333 | and ``\n`` characters are converted to ``\r\n`` characters.  A byte string is | 
|  | 334 | not modified. | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 335 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | If there has been no previous ``EHLO`` or ``HELO`` command this session, this | 
|  | 337 | method tries ESMTP ``EHLO`` first. If the server does ESMTP, message size and | 
|  | 338 | each of the specified options will be passed to it (if the option is in the | 
|  | 339 | feature set the server advertises).  If ``EHLO`` fails, ``HELO`` will be tried | 
|  | 340 | and ESMTP options suppressed. | 
|  | 341 |  | 
|  | 342 | This method will return normally if the mail is accepted for at least one | 
| Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 343 | recipient. Otherwise it will raise an exception.  That is, if this method does | 
|  | 344 | not raise an exception, then someone should get your mail. If this method does | 
|  | 345 | not raise an exception, it returns a dictionary, with one entry for each | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | recipient that was refused.  Each entry contains a tuple of the SMTP error code | 
|  | 347 | and the accompanying error message sent by the server. | 
|  | 348 |  | 
|  | 349 | This method may raise the following exceptions: | 
|  | 350 |  | 
|  | 351 | :exc:`SMTPRecipientsRefused` | 
|  | 352 | All recipients were refused.  Nobody got the mail.  The :attr:`recipients` | 
|  | 353 | attribute of the exception object is a dictionary with information about the | 
|  | 354 | refused recipients (like the one returned when at least one recipient was | 
|  | 355 | accepted). | 
|  | 356 |  | 
|  | 357 | :exc:`SMTPHeloError` | 
|  | 358 | The server didn't reply properly to the ``HELO`` greeting. | 
|  | 359 |  | 
|  | 360 | :exc:`SMTPSenderRefused` | 
|  | 361 | The server didn't accept the *from_addr*. | 
|  | 362 |  | 
|  | 363 | :exc:`SMTPDataError` | 
|  | 364 | The server replied with an unexpected error code (other than a refusal of a | 
|  | 365 | recipient). | 
|  | 366 |  | 
|  | 367 | Unless otherwise noted, the connection will be open even after an exception is | 
|  | 368 | raised. | 
|  | 369 |  | 
| Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 370 | .. versionchanged:: 3.2 | 
|  | 371 | *msg* may be a byte string. | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 372 |  | 
|  | 373 |  | 
| R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 374 | .. method:: SMTP.send_message(msg, from_addr=None, to_addrs=None, \ | 
|  | 375 | mail_options=[], rcpt_options=[]) | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 376 |  | 
|  | 377 | This is a convenience method for calling :meth:`sendmail` with the message | 
|  | 378 | represented by an :class:`email.message.Message` object.  The arguments have | 
|  | 379 | the same meaning as for :meth:`sendmail`, except that *msg* is a ``Message`` | 
|  | 380 | object. | 
|  | 381 |  | 
| R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 382 | If *from_addr* is ``None`` or *to_addrs* is ``None``, ``send_message`` fills | 
|  | 383 | those arguments with addresses extracted from the headers of *msg* as | 
|  | 384 | specified in :rfc:`2822`\: *from_addr* is set to the :mailheader:`Sender` | 
|  | 385 | field if it is present, and otherwise to the :mailheader:`From` field. | 
|  | 386 | *to_adresses* combines the values (if any) of the :mailheader:`To`, | 
|  | 387 | :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*.  If exactly one | 
|  | 388 | set of :mailheader:`Resent-*` headers appear in the message, the regular | 
|  | 389 | headers are ignored and the :mailheader:`Resent-*` headers are used instead. | 
|  | 390 | If the message contains more than one set of :mailheader:`Resent-*` headers, | 
|  | 391 | a :exc:`ValueError` is raised, since there is no way to unambiguously detect | 
|  | 392 | the most recent set of :mailheader:`Resent-` headers. | 
|  | 393 |  | 
|  | 394 | ``send_message`` serializes *msg* using | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 395 | :class:`~email.generator.BytesGenerator` with ``\r\n`` as the *linesep*, and | 
| R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 396 | calls :meth:`sendmail` to transmit the resulting message.  Regardless of the | 
|  | 397 | values of *from_addr* and *to_addrs*, ``send_message`` does not transmit any | 
|  | 398 | :mailheader:`Bcc` or :mailheader:`Resent-Bcc` headers that may appear | 
|  | 399 | in *msg*. | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 400 |  | 
|  | 401 | .. versionadded:: 3.2 | 
|  | 402 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 |  | 
|  | 404 | .. method:: SMTP.quit() | 
|  | 405 |  | 
| Christian Heimes | ba4af49 | 2008-03-28 00:55:15 +0000 | [diff] [blame] | 406 | Terminate the SMTP session and close the connection.  Return the result of | 
|  | 407 | the SMTP ``QUIT`` command. | 
|  | 408 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 409 |  | 
|  | 410 | Low-level methods corresponding to the standard SMTP/ESMTP commands ``HELP``, | 
|  | 411 | ``RSET``, ``NOOP``, ``MAIL``, ``RCPT``, and ``DATA`` are also supported. | 
|  | 412 | Normally these do not need to be called directly, so they are not documented | 
|  | 413 | here.  For details, consult the module code. | 
|  | 414 |  | 
|  | 415 |  | 
|  | 416 | .. _smtp-example: | 
|  | 417 |  | 
|  | 418 | SMTP Example | 
|  | 419 | ------------ | 
|  | 420 |  | 
|  | 421 | This example prompts the user for addresses needed in the message envelope ('To' | 
|  | 422 | and 'From' addresses), and the message to be delivered.  Note that the headers | 
|  | 423 | to be included with the message must be included in the message as entered; this | 
|  | 424 | example doesn't do any processing of the :rfc:`822` headers.  In particular, the | 
|  | 425 | 'To' and 'From' addresses must be included in the message headers explicitly. :: | 
|  | 426 |  | 
|  | 427 | import smtplib | 
|  | 428 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | def prompt(prompt): | 
| Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 430 | return input(prompt).strip() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 |  | 
|  | 432 | fromaddr = prompt("From: ") | 
|  | 433 | toaddrs  = prompt("To: ").split() | 
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 434 | print("Enter message, end with ^D (Unix) or ^Z (Windows):") | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | # Add the From: and To: headers at the start! | 
|  | 437 | msg = ("From: %s\r\nTo: %s\r\n\r\n" | 
|  | 438 | % (fromaddr, ", ".join(toaddrs))) | 
| Collin Winter | 4633448 | 2007-09-10 00:49:57 +0000 | [diff] [blame] | 439 | while True: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | try: | 
| Georg Brandl | 8d5c392 | 2007-12-02 22:48:17 +0000 | [diff] [blame] | 441 | line = input() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | except EOFError: | 
|  | 443 | break | 
|  | 444 | if not line: | 
|  | 445 | break | 
|  | 446 | msg = msg + line | 
|  | 447 |  | 
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 448 | print("Message length is", len(msg)) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 449 |  | 
|  | 450 | server = smtplib.SMTP('localhost') | 
|  | 451 | server.set_debuglevel(1) | 
|  | 452 | server.sendmail(fromaddr, toaddrs, msg) | 
|  | 453 | server.quit() | 
|  | 454 |  | 
| Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 455 | .. note:: | 
|  | 456 |  | 
|  | 457 | In general, you will want to use the :mod:`email` package's features to | 
| R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 458 | construct an email message, which you can then send | 
|  | 459 | via :meth:`~smtplib.SMTP.send_message`; see :ref:`email-examples`. |