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