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