Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{smtplib} --- |
| 2 | SMTP protocol client.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{smtplib} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | |
| 6 | \modulesynopsis{SMTP protocol client (requires sockets).} |
| 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 |
| 14 | \rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869} |
| 15 | (\emph{SMTP Service Extensions}). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 16 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 17 | \begin{classdesc}{SMTP}{\optional{host\optional{, port}}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 18 | A \class{SMTP} instance encapsulates an SMTP connection. It has |
| 19 | methods that support a full repertoire of SMTP and ESMTP |
| 20 | operations. If the optional host and port parameters are given, the |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 21 | SMTP \method{connect()} method is called with those parameters during |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 22 | initialization. An \exception{SMTPConnectError} is raised if the |
| 23 | specified host doesn't respond correctly. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 24 | |
| 25 | For normal use, you should only require the initialization/connect, |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 26 | \method{sendmail()}, and \method{quit()} methods. An example is |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 27 | included below. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 28 | \end{classdesc} |
| 29 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 31 | \subsection{SMTP Objects} |
| 32 | \label{SMTP-objects} |
| 33 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 34 | An \class{SMTP} instance has the following methods: |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 35 | |
| 36 | \begin{methoddesc}{set_debuglevel}{level} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 37 | Set the debug output level. A true value for \var{level} results in |
| 38 | debug messages for connection and for all messages sent to and |
| 39 | received from the server. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 40 | \end{methoddesc} |
| 41 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 42 | \begin{methoddesc}{connect}{\optional{host\optional{, port}}} |
| 43 | Connect to a host on a given port. The defaults are to connect to the |
| 44 | local host at the standard SMTP port (25). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 45 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 46 | If the hostname ends with a colon (\character{:}) followed by a |
| 47 | 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] | 48 | the port number to use. |
| 49 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 50 | Note: This method is automatically invoked by the constructor if a |
| 51 | host is specified during instantiation. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 52 | \end{methoddesc} |
| 53 | |
| 54 | \begin{methoddesc}{docmd}{cmd, \optional{, argstring}} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 55 | Send a command \var{cmd} to the server. The optional argument |
| 56 | \var{argstring} is simply concatenated to the command, separated by a |
| 57 | space. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 58 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 59 | This returns a 2-tuple composed of a numeric response code and the |
| 60 | actual response line (multiline responses are joined into one long |
| 61 | line.) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 62 | |
| 63 | In normal operation it should not be necessary to call this method |
| 64 | explicitly. It is used to implement other methods and may be useful |
| 65 | for testing private extensions. |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 66 | |
| 67 | If the connection to the server is lost while waiting for the reply an |
| 68 | \exception{SMTPServerDisconnected} exception will be raised. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 69 | \end{methoddesc} |
| 70 | |
| 71 | \begin{methoddesc}{helo}{\optional{hostname}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 72 | Identify yourself to the SMTP server using \samp{HELO}. The hostname |
| 73 | argument defaults to the fully qualified domain name of the local |
| 74 | host. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 75 | |
| 76 | In normal operation it should not be necessary to call this method |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 77 | explicitly. It will be implicitly called by the \method{sendmail()} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 78 | when necessary. |
| 79 | \end{methoddesc} |
| 80 | |
| 81 | \begin{methoddesc}{ehlo}{\optional{hostname}} |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 82 | Identify yourself to an ESMTP server using \samp{EHLO}. The hostname |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 83 | argument defaults to the fully qualified domain name of the local |
| 84 | host. Examine the response for ESMTP option and store them for use by |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 85 | \method{has_option()}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 86 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 87 | Unless you wish to use \method{has_option()} before sending |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 88 | mail, it should not be necessary to call this method explicitly. It |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 89 | will be implicitly called by \method{sendmail()} when necessary. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 90 | \end{methoddesc} |
| 91 | |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 92 | \begin{methoddesc}{has_extn}{name} |
| 93 | Return \code{1} if \var{name} is in the set of SMTP service extensions returned |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 94 | by the server, \code{0} otherwise. Case is ignored. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 95 | \end{methoddesc} |
| 96 | |
| 97 | \begin{methoddesc}{verify}{address} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 98 | 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] | 99 | 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] | 100 | (including human name) if the user address is valid. Otherwise returns |
| 101 | an SMTP error code of 400 or greater and an error string. |
| 102 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 103 | 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] | 104 | \end{methoddesc} |
| 105 | |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 106 | \begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, |
| 107 | mail_options, rcpt_options}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 108 | Send mail. The required arguments are an \rfc{822} from-address |
| 109 | string, a list of \rfc{822} to-address strings, and a message string. |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 110 | The caller may pass a list of ESMTP options (such as \samp{8bitmime}) |
| 111 | to be used in \samp{MAIL FROM} commands as \var{mail_options}. ESMTP |
| 112 | options (such as \samp{DSN} commands) that should be used with all |
| 113 | \samp{RCPT} commands can be passed as \var{rcpt_options}. (If you |
| 114 | need to use different ESMTP options to different recipients you have |
| 115 | to use the low-level methods such as \method{mail}, \method{rcpt} and |
| 116 | \method{data} to send the message.) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 117 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 118 | If there has been no previous \samp{EHLO} or \samp{HELO} command this |
| 119 | session, this method tries ESMTP \samp{EHLO} first. If the server does |
| 120 | ESMTP, message size and each of the specified options will be passed |
| 121 | to it (if the option is in the feature set the server advertises). If |
| 122 | \samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options |
| 123 | suppressed. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 7969f31 | 1999-04-07 15:56:51 +0000 | [diff] [blame^] | 125 | This method will return normally if the mail is accepted for at least |
| 126 | one recipient. Otherwise it will throw an exception. That is, if this |
| 127 | method does not throw an exception, then someone should get your mail. |
| 128 | If this method does not throw an exception, it returns a dictionary, |
| 129 | with one entry for each recipient that was refused. Each entry |
| 130 | contains a tuple of the SMTP error code and the accompanying error |
| 131 | message sent by the server. |
| 132 | |
| 133 | This method may raise the following exceptions: |
| 134 | |
| 135 | \begin{itemize} |
| 136 | |
| 137 | \item \exception{SMTPRecipientsRefused} |
| 138 | |
| 139 | All recipients were refused. Nobody got the mail. The |
| 140 | \var{recipients} attribute of the exception object is a dictionary |
| 141 | with information about the refused recipients (like the one returned |
| 142 | when at least one recipient was accepted). |
| 143 | |
| 144 | \item \exception{SMTPHeloError} |
| 145 | |
| 146 | The server didn't reply properly to |
| 147 | the helo greeting. The connection has |
| 148 | been closed. |
| 149 | |
| 150 | \item \exception{SMTPSenderRefused} |
| 151 | |
| 152 | The server didn't accept the from_addr. |
| 153 | |
| 154 | \item \exception{SMTPDataError} |
| 155 | |
| 156 | The server replied with an unexpected |
| 157 | error code (other than a refusal of |
| 158 | a recipient). |
| 159 | |
| 160 | \end{itemize} |
| 161 | |
| 162 | Unless otherwise noted the connection will be open even after |
| 163 | an exception is raised. |
| 164 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 165 | \end{methoddesc} |
| 166 | |
| 167 | \begin{methoddesc}{quit}{} |
| 168 | Terminate the SMTP session and close the connection. |
| 169 | \end{methoddesc} |
| 170 | |
| 171 | Low-level methods corresponding to the standard SMTP/ESMTP commands |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame] | 172 | \samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and |
| 173 | \samp{DATA} are also supported. Normally these do not need to be |
| 174 | called directly, so they are not documented here. For details, |
| 175 | consult the module code. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 176 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 177 | |
Fred Drake | 60c3caf | 1998-08-07 16:03:32 +0000 | [diff] [blame] | 178 | \subsection{SMTP Example \label{SMTP-example}} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 179 | |
Fred Drake | 7be0cde | 1998-12-22 18:04:48 +0000 | [diff] [blame] | 180 | This example prompts the user for addresses needed in the message |
| 181 | envelop (`To' and `From' addresses), and the message to be |
| 182 | delivered. Note that the headers to be included with the message must |
| 183 | be included in the message as entered; this example doesn't do any |
| 184 | processing of the \rfc{822} headers. In particular, the `To' and |
| 185 | `From' addresses must be included in the message headers explicitly. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 186 | |
| 187 | \begin{verbatim} |
Fred Drake | 60c3caf | 1998-08-07 16:03:32 +0000 | [diff] [blame] | 188 | import rfc822, string, sys |
Fred Drake | f6c59e8 | 1998-11-30 15:07:26 +0000 | [diff] [blame] | 189 | import smtplib |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 190 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 191 | def prompt(prompt): |
| 192 | sys.stdout.write(prompt + ": ") |
| 193 | return string.strip(sys.stdin.readline()) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 194 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 195 | fromaddr = prompt("From") |
| 196 | toaddrs = string.splitfields(prompt("To"), ',') |
| 197 | print "Enter message, end with ^D:" |
Fred Drake | 7be0cde | 1998-12-22 18:04:48 +0000 | [diff] [blame] | 198 | msg = "" |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 199 | while 1: |
| 200 | line = sys.stdin.readline() |
| 201 | if not line: |
| 202 | break |
| 203 | msg = msg + line |
| 204 | print "Message length is " + `len(msg)` |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 205 | |
Fred Drake | f6c59e8 | 1998-11-30 15:07:26 +0000 | [diff] [blame] | 206 | server = smtplib.SMTP('localhost') |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 207 | server.set_debuglevel(1) |
| 208 | server.sendmail(fromaddr, toaddrs, msg) |
| 209 | server.quit() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 210 | \end{verbatim} |
| 211 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 212 | |
| 213 | \begin{seealso} |
| 214 | \seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available |
| 215 | online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.} |
| 216 | |
| 217 | \seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online |
| 218 | at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.} |
| 219 | \end{seealso} |