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