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