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