Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{smtplib} --- |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 2 | SMTP protocol client} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{smtplib} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{SMTP protocol client (requires sockets).} |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 6 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 8 | \indexii{SMTP}{protocol} |
| 9 | \index{Simple Mail Transfer Protocol} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 10 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 11 | The \module{smtplib} module defines an SMTP client session object that |
| 12 | can be used to send mail to any Internet machine with an SMTP or ESMTP |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 13 | listener daemon. For details of SMTP and ESMTP operation, consult |
Fred Drake | ebe0341 | 1999-11-09 20:11:17 +0000 | [diff] [blame] | 14 | \rfc{821} (\citetitle{Simple Mail Transfer Protocol}) and \rfc{1869} |
| 15 | (\citetitle{SMTP Service Extensions}). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 16 | |
Neil Schemenauer | ccbb0ed | 2002-03-24 15:41:40 +0000 | [diff] [blame] | 17 | \begin{classdesc}{SMTP}{\optional{host\optional{, port\optional{, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 18 | local_hostname\optional{, timeout}}}}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 19 | A \class{SMTP} instance encapsulates an SMTP connection. It has |
| 20 | methods that support a full repertoire of SMTP and ESMTP |
| 21 | operations. If the optional host and port parameters are given, the |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 22 | SMTP \method{connect()} method is called with those parameters during |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 23 | initialization. An \exception{SMTPConnectError} is raised if the |
| 24 | specified host doesn't respond correctly. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 25 | The optional \var{timeout} parameter specifies a timeout in seconds for the |
| 26 | connection attempt (if not specified, or passed as None, the global |
| 27 | default timeout setting will be used). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 28 | |
| 29 | For normal use, you should only require the initialization/connect, |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 30 | \method{sendmail()}, and \method{quit()} methods. An example is |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 31 | included below. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 32 | \end{classdesc} |
| 33 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | \begin{classdesc}{SMTP_SSL}{\optional{host\optional{, port\optional{, |
| 35 | local_hostname\optional{, |
| 36 | keyfile\optional{, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 37 | certfile\optional{, timeout}}}}}}} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | A \class{SMTP_SSL} instance behaves exactly the same as instances of \class{SMTP}. |
| 39 | \class{SMTP_SSL} should be used for situations where SSL is required from |
| 40 | the beginning of the connection and using \method{starttls()} is not appropriate. |
| 41 | If \var{host} is not specified, the local host is used. If \var{port} is |
| 42 | omitted, the standard SMTP-over-SSL port (465) is used. \var{keyfile} and \var{certfile} |
| 43 | are also optional, and can contain a PEM formatted private key and |
| 44 | certificate chain file for the SSL connection. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 45 | The optional \var{timeout} parameter specifies a timeout in seconds for the |
| 46 | connection attempt (if not specified, or passed as None, the global |
| 47 | default timeout setting will be used). |
| 48 | \end{classdesc} |
| 49 | |
| 50 | \begin{classdesc}{LMTP}{\optional{host\optional{, port\optional{, |
| 51 | local_hostname}}}} |
| 52 | |
| 53 | The LMTP protocol, which is very similar to ESMTP, is heavily based |
| 54 | on the standard SMTP client. It's common to use Unix sockets for LMTP, |
| 55 | so our connect() method must support that as well as a regular |
| 56 | host:port server. To specify a Unix socket, you must use an absolute |
| 57 | path for \var{host}, starting with a '/'. |
| 58 | |
| 59 | Authentication is supported, using the regular SMTP mechanism. When |
| 60 | using a Unix socket, LMTP generally don't support or require any |
| 61 | authentication, but your mileage might vary. |
| 62 | |
| 63 | \versionadded{2.6} |
| 64 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 65 | \end{classdesc} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 66 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 67 | A nice selection of exceptions is defined as well: |
| 68 | |
| 69 | \begin{excdesc}{SMTPException} |
| 70 | Base exception class for all exceptions raised by this module. |
| 71 | \end{excdesc} |
| 72 | |
| 73 | \begin{excdesc}{SMTPServerDisconnected} |
| 74 | This exception is raised when the server unexpectedly disconnects, |
| 75 | or when an attempt is made to use the \class{SMTP} instance before |
| 76 | connecting it to a server. |
| 77 | \end{excdesc} |
| 78 | |
| 79 | \begin{excdesc}{SMTPResponseException} |
| 80 | Base class for all exceptions that include an SMTP error code. |
| 81 | These exceptions are generated in some instances when the SMTP |
| 82 | server returns an error code. The error code is stored in the |
| 83 | \member{smtp_code} attribute of the error, and the |
| 84 | \member{smtp_error} attribute is set to the error message. |
| 85 | \end{excdesc} |
| 86 | |
| 87 | \begin{excdesc}{SMTPSenderRefused} |
Tim Peters | 9ca3f02 | 2003-06-15 23:08:45 +0000 | [diff] [blame] | 88 | Sender address refused. In addition to the attributes set by on all |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 89 | \exception{SMTPResponseException} exceptions, this sets `sender' to |
| 90 | the string that the SMTP server refused. |
| 91 | \end{excdesc} |
| 92 | |
| 93 | \begin{excdesc}{SMTPRecipientsRefused} |
| 94 | All recipient addresses refused. The errors for each recipient are |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 95 | accessible through the attribute \member{recipients}, which is a |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 96 | dictionary of exactly the same sort as \method{SMTP.sendmail()} |
| 97 | returns. |
| 98 | \end{excdesc} |
| 99 | |
| 100 | \begin{excdesc}{SMTPDataError} |
| 101 | The SMTP server refused to accept the message data. |
| 102 | \end{excdesc} |
| 103 | |
| 104 | \begin{excdesc}{SMTPConnectError} |
| 105 | Error occurred during establishment of a connection with the server. |
| 106 | \end{excdesc} |
| 107 | |
| 108 | \begin{excdesc}{SMTPHeloError} |
| 109 | The server refused our \samp{HELO} message. |
| 110 | \end{excdesc} |
| 111 | |
| 112 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 113 | \begin{seealso} |
Fred Drake | 58a2dff | 2000-10-03 05:56:55 +0000 | [diff] [blame] | 114 | \seerfc{821}{Simple Mail Transfer Protocol}{Protocol definition for |
| 115 | SMTP. This document covers the model, operating procedure, |
| 116 | and protocol details for SMTP.} |
| 117 | \seerfc{1869}{SMTP Service Extensions}{Definition of the ESMTP |
| 118 | extensions for SMTP. This describes a framework for |
| 119 | extending SMTP with new commands, supporting dynamic |
| 120 | discovery of the commands provided by the server, and |
| 121 | defines a few additional commands.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 122 | \end{seealso} |
| 123 | |
| 124 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 125 | \subsection{SMTP Objects \label{SMTP-objects}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 126 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 127 | An \class{SMTP} instance has the following methods: |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 129 | \begin{methoddesc}[SMTP]{set_debuglevel}{level} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 130 | Set the debug output level. A true value for \var{level} results in |
| 131 | debug messages for connection and for all messages sent to and |
| 132 | received from the server. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 133 | \end{methoddesc} |
| 134 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 135 | \begin{methoddesc}[SMTP]{connect}{\optional{host\optional{, port}}} |
Tim Peters | 9ca3f02 | 2003-06-15 23:08:45 +0000 | [diff] [blame] | 136 | Connect to a host on a given port. The defaults are to connect to the |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 137 | local host at the standard SMTP port (25). |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 138 | If the hostname ends with a colon (\character{:}) followed by a |
Tim Peters | 9ca3f02 | 2003-06-15 23:08:45 +0000 | [diff] [blame] | 139 | number, that suffix will be stripped off and the number interpreted as |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 140 | the port number to use. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 141 | This method is automatically invoked by the constructor if a |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 142 | host is specified during instantiation. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 143 | \end{methoddesc} |
| 144 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 145 | \begin{methoddesc}[SMTP]{docmd}{cmd, \optional{, argstring}} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 146 | Send a command \var{cmd} to the server. The optional argument |
| 147 | \var{argstring} is simply concatenated to the command, separated by a |
| 148 | space. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 149 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 150 | This returns a 2-tuple composed of a numeric response code and the |
| 151 | actual response line (multiline responses are joined into one long |
| 152 | line.) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 153 | |
| 154 | In normal operation it should not be necessary to call this method |
| 155 | explicitly. It is used to implement other methods and may be useful |
| 156 | for testing private extensions. |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 157 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 158 | If the connection to the server is lost while waiting for the reply, |
| 159 | \exception{SMTPServerDisconnected} will be raised. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 160 | \end{methoddesc} |
| 161 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 162 | \begin{methoddesc}[SMTP]{helo}{\optional{hostname}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 163 | Identify yourself to the SMTP server using \samp{HELO}. The hostname |
| 164 | argument defaults to the fully qualified domain name of the local |
| 165 | host. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 166 | |
| 167 | In normal operation it should not be necessary to call this method |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 168 | explicitly. It will be implicitly called by the \method{sendmail()} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 169 | when necessary. |
| 170 | \end{methoddesc} |
| 171 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 172 | \begin{methoddesc}[SMTP]{ehlo}{\optional{hostname}} |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 173 | Identify yourself to an ESMTP server using \samp{EHLO}. The hostname |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 174 | argument defaults to the fully qualified domain name of the local |
| 175 | host. Examine the response for ESMTP option and store them for use by |
Greg Ward | 19b6f60 | 2003-01-08 03:04:42 +0000 | [diff] [blame] | 176 | \method{has_extn()}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 177 | |
Greg Ward | 19b6f60 | 2003-01-08 03:04:42 +0000 | [diff] [blame] | 178 | Unless you wish to use \method{has_extn()} before sending |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 179 | mail, it should not be necessary to call this method explicitly. It |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 180 | will be implicitly called by \method{sendmail()} when necessary. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 181 | \end{methoddesc} |
| 182 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 183 | \begin{methoddesc}[SMTP]{has_extn}{name} |
Raymond Hettinger | 9b4dab4 | 2003-12-31 18:37:28 +0000 | [diff] [blame] | 184 | Return \constant{True} if \var{name} is in the set of SMTP service |
| 185 | extensions returned by the server, \constant{False} otherwise. |
| 186 | Case is ignored. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 187 | \end{methoddesc} |
| 188 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 189 | \begin{methoddesc}[SMTP]{verify}{address} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 190 | Check the validity of an address on this server using SMTP \samp{VRFY}. |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 191 | Returns a tuple consisting of code 250 and a full \rfc{822} address |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 192 | (including human name) if the user address is valid. Otherwise returns |
| 193 | an SMTP error code of 400 or greater and an error string. |
| 194 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 195 | \note{Many sites disable SMTP \samp{VRFY} in order to foil spammers.} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 196 | \end{methoddesc} |
| 197 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 198 | \begin{methoddesc}[SMTP]{login}{user, password} |
Fred Drake | aae8da1 | 2001-09-11 16:58:00 +0000 | [diff] [blame] | 199 | Log in on an SMTP server that requires authentication. |
| 200 | The arguments are the username and the password to authenticate with. |
| 201 | If there has been no previous \samp{EHLO} or \samp{HELO} command this |
| 202 | session, this method tries ESMTP \samp{EHLO} first. |
| 203 | This method will return normally if the authentication was successful, |
| 204 | or may raise the following exceptions: |
| 205 | |
| 206 | \begin{description} |
| 207 | \item[\exception{SMTPHeloError}] |
| 208 | The server didn't reply properly to the \samp{HELO} greeting. |
| 209 | \item[\exception{SMTPAuthenticationError}] |
| 210 | The server didn't accept the username/password combination. |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 211 | \item[\exception{SMTPException}] |
Fred Drake | aae8da1 | 2001-09-11 16:58:00 +0000 | [diff] [blame] | 212 | No suitable authentication method was found. |
| 213 | \end{description} |
| 214 | \end{methoddesc} |
| 215 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 216 | \begin{methoddesc}[SMTP]{starttls}{\optional{keyfile\optional{, certfile}}} |
Fred Drake | f2a5f3f | 2001-09-14 17:48:41 +0000 | [diff] [blame] | 217 | Put the SMTP connection in TLS (Transport Layer Security) mode. All |
| 218 | SMTP commands that follow will be encrypted. You should then call |
| 219 | \method{ehlo()} again. |
Guido van Rossum | f7fcf5e | 2001-09-14 16:08:44 +0000 | [diff] [blame] | 220 | |
Fred Drake | f2a5f3f | 2001-09-14 17:48:41 +0000 | [diff] [blame] | 221 | If \var{keyfile} and \var{certfile} are provided, these are passed to |
| 222 | the \refmodule{socket} module's \function{ssl()} function. |
Guido van Rossum | f7fcf5e | 2001-09-14 16:08:44 +0000 | [diff] [blame] | 223 | \end{methoddesc} |
| 224 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 225 | \begin{methoddesc}[SMTP]{sendmail}{from_addr, to_addrs, msg\optional{, |
| 226 | mail_options, rcpt_options}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 227 | Send mail. The required arguments are an \rfc{822} from-address |
Georg Brandl | 9020a21 | 2005-06-25 18:24:03 +0000 | [diff] [blame] | 228 | string, a list of \rfc{822} to-address strings (a bare string will be |
| 229 | treated as a list with 1 address), and a message string. The caller |
| 230 | may pass a list of ESMTP options (such as \samp{8bitmime}) to be used |
| 231 | in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP options |
| 232 | (such as \samp{DSN} commands) that should be used with all \samp{RCPT} |
| 233 | commands can be passed as \var{rcpt_options}. (If you need to use |
| 234 | different ESMTP options to different recipients you have to use the |
| 235 | low-level methods such as \method{mail}, \method{rcpt} and |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 236 | \method{data} to send the message.) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 237 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 238 | \note{The \var{from_addr} and \var{to_addrs} parameters are |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 239 | used to construct the message envelope used by the transport agents. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 240 | The \class{SMTP} does not modify the message headers in any way.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 241 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 242 | If there has been no previous \samp{EHLO} or \samp{HELO} command this |
| 243 | session, this method tries ESMTP \samp{EHLO} first. If the server does |
| 244 | ESMTP, message size and each of the specified options will be passed |
| 245 | to it (if the option is in the feature set the server advertises). If |
| 246 | \samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options |
| 247 | suppressed. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 249 | This method will return normally if the mail is accepted for at least |
| 250 | one recipient. Otherwise it will throw an exception. That is, if this |
| 251 | method does not throw an exception, then someone should get your mail. |
| 252 | If this method does not throw an exception, it returns a dictionary, |
| 253 | with one entry for each recipient that was refused. Each entry |
| 254 | contains a tuple of the SMTP error code and the accompanying error |
| 255 | message sent by the server. |
| 256 | |
| 257 | This method may raise the following exceptions: |
| 258 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 259 | \begin{description} |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 260 | \item[\exception{SMTPRecipientsRefused}] |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 261 | All recipients were refused. Nobody got the mail. The |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 262 | \member{recipients} attribute of the exception object is a dictionary |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 263 | with information about the refused recipients (like the one returned |
| 264 | when at least one recipient was accepted). |
| 265 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 266 | \item[\exception{SMTPHeloError}] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 267 | The server didn't reply properly to the \samp{HELO} greeting. |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 268 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 269 | \item[\exception{SMTPSenderRefused}] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 270 | The server didn't accept the \var{from_addr}. |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 271 | |
Fred Drake | 79936fe | 1999-04-22 17:23:34 +0000 | [diff] [blame] | 272 | \item[\exception{SMTPDataError}] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 273 | The server replied with an unexpected error code (other than a refusal |
| 274 | of a recipient). |
| 275 | \end{description} |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 276 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 277 | Unless otherwise noted, the connection will be open even after |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame] | 278 | an exception is raised. |
| 279 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 280 | \end{methoddesc} |
| 281 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 282 | \begin{methoddesc}[SMTP]{quit}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 283 | Terminate the SMTP session and close the connection. |
| 284 | \end{methoddesc} |
| 285 | |
| 286 | Low-level methods corresponding to the standard SMTP/ESMTP commands |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 287 | \samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and |
| 288 | \samp{DATA} are also supported. Normally these do not need to be |
| 289 | called directly, so they are not documented here. For details, |
| 290 | consult the module code. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 291 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 292 | |
Fred Drake | 60c3caf | 1998-08-07 16:03:32 +0000 | [diff] [blame] | 293 | \subsection{SMTP Example \label{SMTP-example}} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 294 | |
Fred Drake | 7be0cde | 1998-12-22 18:04:48 +0000 | [diff] [blame] | 295 | This example prompts the user for addresses needed in the message |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 296 | envelope (`To' and `From' addresses), and the message to be |
Tim Peters | 9ca3f02 | 2003-06-15 23:08:45 +0000 | [diff] [blame] | 297 | delivered. Note that the headers to be included with the message must |
Fred Drake | 7be0cde | 1998-12-22 18:04:48 +0000 | [diff] [blame] | 298 | be included in the message as entered; this example doesn't do any |
| 299 | processing of the \rfc{822} headers. In particular, the `To' and |
| 300 | `From' addresses must be included in the message headers explicitly. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 301 | |
| 302 | \begin{verbatim} |
Fred Drake | f6c59e8 | 1998-11-30 15:07:26 +0000 | [diff] [blame] | 303 | import smtplib |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 304 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 305 | def raw_input(prompt): |
| 306 | import sys |
| 307 | sys.stdout.write(prompt) |
| 308 | sys.stdout.flush() |
| 309 | return sys.stdin.readline() |
| 310 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 311 | def prompt(prompt): |
Fred Drake | e9719fe | 2001-05-20 12:26:04 +0000 | [diff] [blame] | 312 | return raw_input(prompt).strip() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 313 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 314 | fromaddr = prompt("From: ") |
Fred Drake | e9719fe | 2001-05-20 12:26:04 +0000 | [diff] [blame] | 315 | toaddrs = prompt("To: ").split() |
Tim Peters | 9ca3f02 | 2003-06-15 23:08:45 +0000 | [diff] [blame] | 316 | print "Enter message, end with ^D (Unix) or ^Z (Windows):" |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 317 | |
| 318 | # Add the From: and To: headers at the start! |
| 319 | msg = ("From: %s\r\nTo: %s\r\n\r\n" |
Raymond Hettinger | 1e80359 | 2003-09-22 06:25:10 +0000 | [diff] [blame] | 320 | % (fromaddr, ", ".join(toaddrs))) |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 321 | while 1: |
Fred Drake | e9719fe | 2001-05-20 12:26:04 +0000 | [diff] [blame] | 322 | try: |
| 323 | line = raw_input() |
| 324 | except EOFError: |
| 325 | break |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 326 | if not line: |
| 327 | break |
| 328 | msg = msg + line |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 329 | |
Raymond Hettinger | 1e80359 | 2003-09-22 06:25:10 +0000 | [diff] [blame] | 330 | print "Message length is " + repr(len(msg)) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 331 | |
Fred Drake | f6c59e8 | 1998-11-30 15:07:26 +0000 | [diff] [blame] | 332 | server = smtplib.SMTP('localhost') |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 333 | server.set_debuglevel(1) |
| 334 | server.sendmail(fromaddr, toaddrs, msg) |
| 335 | server.quit() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 336 | \end{verbatim} |