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