Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 1 | % Documentation by ESR |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 2 | \section{Standard Module \module{smtplib}} |
| 3 | \stmodindex{smtplib} |
| 4 | \label{module-smtplib} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 5 | \indexii{SMTP}{protocol} |
| 6 | \index{Simple Mail Transfer Protocol} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 7 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 8 | The \module{smtplib} module defines an SMTP client session object that |
| 9 | 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] | 10 | listener daemon. For details of SMTP and ESMTP operation, consult |
| 11 | \rfc{821} (\emph{Simple Mail Transfer Protocol}) and \rfc{1869} |
| 12 | (\emph{SMTP Service Extensions}). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 13 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 14 | \begin{classdesc}{SMTP}{\optional{host\optional{, port}}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 15 | A \class{SMTP} instance encapsulates an SMTP connection. It has |
| 16 | methods that support a full repertoire of SMTP and ESMTP |
| 17 | operations. If the optional host and port parameters are given, the |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 18 | SMTP \method{connect()} method is called with those parameters during |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 19 | initialization. |
| 20 | |
| 21 | For normal use, you should only require the initialization/connect, |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 22 | \method{sendmail()}, and \method{quit()} methods An example is |
| 23 | included below. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 24 | \end{classdesc} |
| 25 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 26 | |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 27 | \subsection{SMTP Objects} |
| 28 | \label{SMTP-objects} |
| 29 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 30 | An \class{SMTP} instance has the following methods: |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 31 | |
| 32 | \begin{methoddesc}{set_debuglevel}{level} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 33 | Set the debug output level. A true value for \var{level} results in |
| 34 | debug messages for connection and for all messages sent to and |
| 35 | received from the server. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 36 | \end{methoddesc} |
| 37 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 38 | \begin{methoddesc}{connect}{\optional{host\optional{, port}}} |
| 39 | Connect to a host on a given port. The defaults are to connect to the |
| 40 | local host at the standard SMTP port (25). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 41 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 42 | If the hostname ends with a colon (\character{:}) followed by a |
| 43 | 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] | 44 | the port number to use. |
| 45 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 46 | Note: This method is automatically invoked by the constructor if a |
| 47 | host is specified during instantiation. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 48 | \end{methoddesc} |
| 49 | |
| 50 | \begin{methoddesc}{docmd}{cmd, \optional{, argstring}} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 51 | Send a command \var{cmd} to the server. The optional argument |
| 52 | \var{argstring} is simply concatenated to the command, separated by a |
| 53 | space. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 54 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 55 | This returns a 2-tuple composed of a numeric response code and the |
| 56 | actual response line (multiline responses are joined into one long |
| 57 | line.) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 58 | |
| 59 | In normal operation it should not be necessary to call this method |
| 60 | explicitly. It is used to implement other methods and may be useful |
| 61 | for testing private extensions. |
| 62 | \end{methoddesc} |
| 63 | |
| 64 | \begin{methoddesc}{helo}{\optional{hostname}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 65 | Identify yourself to the SMTP server using \samp{HELO}. The hostname |
| 66 | argument defaults to the fully qualified domain name of the local |
| 67 | host. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 68 | |
| 69 | In normal operation it should not be necessary to call this method |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 70 | explicitly. It will be implicitly called by the \method{sendmail()} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 71 | when necessary. |
| 72 | \end{methoddesc} |
| 73 | |
| 74 | \begin{methoddesc}{ehlo}{\optional{hostname}} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 75 | Identify yourself to an ESMTP server using \samp{HELO}. The hostname |
| 76 | argument defaults to the fully qualified domain name of the local |
| 77 | 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] | 78 | \method{has_option()}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 79 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 80 | Unless you wish to use \method{has_option()} before sending |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 81 | mail, it should not be necessary to call this method explicitly. It |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 82 | will be implicitly called by \method{sendmail()} when necessary. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 83 | \end{methoddesc} |
| 84 | |
| 85 | \begin{methoddesc}{has_option}{name} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 86 | Return \code{1} if \var{name} is in the set of ESMTP options returned |
| 87 | by the server, \code{0} otherwise. Case is ignored. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 88 | \end{methoddesc} |
| 89 | |
| 90 | \begin{methoddesc}{verify}{address} |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 91 | 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] | 92 | 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] | 93 | (including human name) if the user address is valid. Otherwise returns |
| 94 | an SMTP error code of 400 or greater and an error string. |
| 95 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 96 | 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] | 97 | \end{methoddesc} |
| 98 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 99 | \begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, options}} |
| 100 | Send mail. The required arguments are an \rfc{822} from-address |
| 101 | string, a list of \rfc{822} to-address strings, and a message string. |
| 102 | The caller may pass a list of ESMTP options to be used in \samp{MAIL |
| 103 | FROM} commands as \var{options}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 104 | |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 105 | If there has been no previous \samp{EHLO} or \samp{HELO} command this |
| 106 | session, this method tries ESMTP \samp{EHLO} first. If the server does |
| 107 | ESMTP, message size and each of the specified options will be passed |
| 108 | to it (if the option is in the feature set the server advertises). If |
| 109 | \samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options |
| 110 | suppressed. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 111 | |
| 112 | This method will return normally if the mail is accepted for at least |
| 113 | one recipient. Otherwise it will throw an exception (either |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 114 | \exception{SMTPSenderRefused}, \exception{SMTPRecipientsRefused}, or |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 115 | \exception{SMTPDataError}). That is, if this method does not throw an |
| 116 | exception, then someone should get your mail. If this method does not |
| 117 | throw an exception, it returns a dictionary, with one entry for each |
| 118 | recipient that was refused. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 119 | \end{methoddesc} |
| 120 | |
| 121 | \begin{methoddesc}{quit}{} |
| 122 | Terminate the SMTP session and close the connection. |
| 123 | \end{methoddesc} |
| 124 | |
| 125 | Low-level methods corresponding to the standard SMTP/ESMTP commands |
Fred Drake | 3240dd2 | 1998-07-01 14:10:52 +0000 | [diff] [blame^] | 126 | \samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and |
| 127 | \samp{DATA} are also supported. Normally these do not need to be |
| 128 | called directly, so they are not documented here. For details, |
| 129 | consult the module code. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 130 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 131 | |
| 132 | \subsection{SMTP Example} |
| 133 | \label{SMTP-example} |
| 134 | |
| 135 | % really need a little description here... |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 136 | |
| 137 | \begin{verbatim} |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 138 | import sys, rfc822 |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 139 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 140 | def prompt(prompt): |
| 141 | sys.stdout.write(prompt + ": ") |
| 142 | return string.strip(sys.stdin.readline()) |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 143 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 144 | fromaddr = prompt("From") |
| 145 | toaddrs = string.splitfields(prompt("To"), ',') |
| 146 | print "Enter message, end with ^D:" |
| 147 | msg = '' |
| 148 | while 1: |
| 149 | line = sys.stdin.readline() |
| 150 | if not line: |
| 151 | break |
| 152 | msg = msg + line |
| 153 | print "Message length is " + `len(msg)` |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 154 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 155 | server = SMTP('localhost') |
| 156 | server.set_debuglevel(1) |
| 157 | server.sendmail(fromaddr, toaddrs, msg) |
| 158 | server.quit() |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 159 | \end{verbatim} |
| 160 | |
Fred Drake | 5b68362 | 1998-06-30 16:53:52 +0000 | [diff] [blame] | 161 | |
| 162 | \begin{seealso} |
| 163 | \seetext{\rfc{821}, \emph{Simple Mail Transfer Protocol}. Available |
| 164 | online at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt}.} |
| 165 | |
| 166 | \seetext{\rfc{1869}, \emph{SMTP Service Extensions}. Available online |
| 167 | at \url{http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt}.} |
| 168 | \end{seealso} |