| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{smtplib} --- | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  |          SMTP protocol client} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \declaremodule{standard}{smtplib} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | \modulesynopsis{SMTP protocol client (requires sockets).} | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | \indexii{SMTP}{protocol} | 
					
						
							|  |  |  | \index{Simple Mail Transfer Protocol} | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | The \module{smtplib} module defines an SMTP client session object that | 
					
						
							|  |  |  | can be used to send mail to any Internet machine with an SMTP or ESMTP | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | listener daemon.  For details of SMTP and ESMTP operation, consult | 
					
						
							| 
									
										
										
										
											1999-11-09 20:11:17 +00:00
										 |  |  | \rfc{821} (\citetitle{Simple Mail Transfer Protocol}) and \rfc{1869} | 
					
						
							|  |  |  | (\citetitle{SMTP Service Extensions}). | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-24 15:41:40 +00:00
										 |  |  | \begin{classdesc}{SMTP}{\optional{host\optional{, port\optional{, | 
					
						
							|  |  |  |                         local_hostname}}}} | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | A \class{SMTP} instance encapsulates an SMTP connection.  It has | 
					
						
							|  |  |  | methods that support a full repertoire of SMTP and ESMTP | 
					
						
							|  |  |  | operations. If the optional host and port parameters are given, the | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | SMTP \method{connect()} method is called with those parameters during | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | initialization.  An \exception{SMTPConnectError} is raised if the | 
					
						
							|  |  |  | specified host doesn't respond correctly. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | For normal use, you should only require the initialization/connect, | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | \method{sendmail()}, and \method{quit()} methods.  An example is | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | included below. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{classdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | A nice selection of exceptions is defined as well: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPException} | 
					
						
							|  |  |  |   Base exception class for all exceptions raised by this module. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPServerDisconnected} | 
					
						
							|  |  |  |   This exception is raised when the server unexpectedly disconnects, | 
					
						
							|  |  |  |   or when an attempt is made to use the \class{SMTP} instance before | 
					
						
							|  |  |  |   connecting it to a server. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPResponseException} | 
					
						
							|  |  |  |   Base class for all exceptions that include an SMTP error code. | 
					
						
							|  |  |  |   These exceptions are generated in some instances when the SMTP | 
					
						
							|  |  |  |   server returns an error code.  The error code is stored in the | 
					
						
							|  |  |  |   \member{smtp_code} attribute of the error, and the | 
					
						
							|  |  |  |   \member{smtp_error} attribute is set to the error message. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPSenderRefused} | 
					
						
							|  |  |  |   Sender address refused.  In addition to the attributes set by on all  | 
					
						
							|  |  |  |   \exception{SMTPResponseException} exceptions, this sets `sender' to | 
					
						
							|  |  |  |   the string that the SMTP server refused. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPRecipientsRefused} | 
					
						
							|  |  |  |   All recipient addresses refused.  The errors for each recipient are | 
					
						
							| 
									
										
										
										
											2000-07-16 19:01:10 +00:00
										 |  |  |   accessible through the attribute \member{recipients}, which is a | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  |   dictionary of exactly the same sort as \method{SMTP.sendmail()} | 
					
						
							|  |  |  |   returns. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPDataError} | 
					
						
							|  |  |  |   The SMTP server refused to accept the message data. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPConnectError} | 
					
						
							|  |  |  |   Error occurred during establishment of a connection  with the server. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{SMTPHeloError} | 
					
						
							|  |  |  |   The server refused our \samp{HELO} message. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | \begin{seealso} | 
					
						
							| 
									
										
										
										
											2000-10-03 05:56:55 +00:00
										 |  |  |   \seerfc{821}{Simple Mail Transfer Protocol}{Protocol definition for | 
					
						
							|  |  |  |           SMTP.  This document covers the model, operating procedure, | 
					
						
							|  |  |  |           and protocol details for SMTP.} | 
					
						
							|  |  |  |   \seerfc{1869}{SMTP Service Extensions}{Definition of the ESMTP | 
					
						
							|  |  |  |           extensions for SMTP.  This describes a framework for | 
					
						
							|  |  |  |           extending SMTP with new commands, supporting dynamic | 
					
						
							|  |  |  |           discovery of the commands provided by the server, and | 
					
						
							|  |  |  |           defines a few additional commands.} | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | \end{seealso} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \subsection{SMTP Objects \label{SMTP-objects}} | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | An \class{SMTP} instance has the following methods: | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{set_debuglevel}{level} | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | Set the debug output level.  A true value for \var{level} results in | 
					
						
							|  |  |  | debug messages for connection and for all messages sent to and | 
					
						
							|  |  |  | received from the server. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | \begin{methoddesc}{connect}{\optional{host\optional{, port}}} | 
					
						
							|  |  |  | Connect to a host on a given port.  The defaults are to connect to the  | 
					
						
							|  |  |  | local host at the standard SMTP port (25). | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | If the hostname ends with a colon (\character{:}) followed by a | 
					
						
							|  |  |  | number, that suffix will be stripped off and the number interpreted as  | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | the port number to use. | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | This method is automatically invoked by the constructor if a | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | host is specified during instantiation. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{docmd}{cmd, \optional{, argstring}} | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | Send a command \var{cmd} to the server.  The optional argument | 
					
						
							|  |  |  | \var{argstring} is simply concatenated to the command, separated by a | 
					
						
							|  |  |  | space. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | This returns a 2-tuple composed of a numeric response code and the | 
					
						
							|  |  |  | actual response line (multiline responses are joined into one long | 
					
						
							|  |  |  | line.) | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | In normal operation it should not be necessary to call this method | 
					
						
							|  |  |  | explicitly.  It is used to implement other methods and may be useful | 
					
						
							|  |  |  | for testing private extensions. | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | If the connection to the server is lost while waiting for the reply, | 
					
						
							|  |  |  | \exception{SMTPServerDisconnected} will be raised. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{helo}{\optional{hostname}} | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | Identify yourself to the SMTP server using \samp{HELO}.  The hostname | 
					
						
							|  |  |  | argument defaults to the fully qualified domain name of the local | 
					
						
							|  |  |  | host. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | In normal operation it should not be necessary to call this method | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | explicitly.  It will be implicitly called by the \method{sendmail()} | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | when necessary. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{ehlo}{\optional{hostname}} | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | Identify yourself to an ESMTP server using \samp{EHLO}.  The hostname | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | argument defaults to the fully qualified domain name of the local | 
					
						
							|  |  |  | host.  Examine the response for ESMTP option and store them for use by | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | \method{has_option()}. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | Unless you wish to use \method{has_option()} before sending | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | mail, it should not be necessary to call this method explicitly.  It | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | will be implicitly called by \method{sendmail()} when necessary. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | \begin{methoddesc}{has_extn}{name} | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | Return \code{1} if \var{name} is in the set of SMTP service extensions | 
					
						
							|  |  |  | returned by the server, \code{0} otherwise.  Case is ignored. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{verify}{address} | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | Check the validity of an address on this server using SMTP \samp{VRFY}. | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | Returns a tuple consisting of code 250 and a full \rfc{822} address | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | (including human name) if the user address is valid. Otherwise returns | 
					
						
							|  |  |  | an SMTP error code of 400 or greater and an error string. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | \note{Many sites disable SMTP \samp{VRFY} in order to foil spammers.} | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-11 16:58:00 +00:00
										 |  |  | \begin{methoddesc}{login}{user, password} | 
					
						
							|  |  |  | Log in on an SMTP server that requires authentication. | 
					
						
							|  |  |  | The arguments are the username and the password to authenticate with. | 
					
						
							|  |  |  | If there has been no previous \samp{EHLO} or \samp{HELO} command this | 
					
						
							|  |  |  | session, this method tries ESMTP \samp{EHLO} first. | 
					
						
							|  |  |  | This method will return normally if the authentication was successful, | 
					
						
							|  |  |  | or may raise the following exceptions: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{description} | 
					
						
							|  |  |  |   \item[\exception{SMTPHeloError}] | 
					
						
							|  |  |  |     The server didn't reply properly to the \samp{HELO} greeting. | 
					
						
							|  |  |  |   \item[\exception{SMTPAuthenticationError}] | 
					
						
							|  |  |  |     The server didn't accept the username/password combination. | 
					
						
							|  |  |  |   \item[\exception{SMTPError}] | 
					
						
							|  |  |  |     No suitable authentication method was found. | 
					
						
							|  |  |  | \end{description} | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-14 17:48:41 +00:00
										 |  |  | \begin{methoddesc}{starttls}{\optional{keyfile\optional{, certfile}}} | 
					
						
							|  |  |  | Put the SMTP connection in TLS (Transport Layer Security) mode.  All | 
					
						
							|  |  |  | SMTP commands that follow will be encrypted.  You should then call | 
					
						
							|  |  |  | \method{ehlo()} again. | 
					
						
							| 
									
										
										
										
											2001-09-14 16:08:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-14 17:48:41 +00:00
										 |  |  | If \var{keyfile} and \var{certfile} are provided, these are passed to | 
					
						
							|  |  |  | the \refmodule{socket} module's \function{ssl()} function. | 
					
						
							| 
									
										
										
										
											2001-09-14 16:08:44 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | \begin{methoddesc}{sendmail}{from_addr, to_addrs, msg\optional{, | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  |                              mail_options, rcpt_options}} | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | Send mail.  The required arguments are an \rfc{822} from-address | 
					
						
							|  |  |  | string, a list of \rfc{822} to-address strings, and a message string. | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | The caller may pass a list of ESMTP options (such as \samp{8bitmime}) | 
					
						
							|  |  |  | to be used in \samp{MAIL FROM} commands as \var{mail_options}.  ESMTP | 
					
						
							|  |  |  | options (such as \samp{DSN} commands) that should be used with all | 
					
						
							|  |  |  | \samp{RCPT} commands can be passed as \var{rcpt_options}.  (If you | 
					
						
							|  |  |  | need to use different ESMTP options to different recipients you have | 
					
						
							|  |  |  | to use the low-level methods such as \method{mail}, \method{rcpt} and | 
					
						
							|  |  |  | \method{data} to send the message.) | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | \note{The \var{from_addr} and \var{to_addrs} parameters are | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | used to construct the message envelope used by the transport agents. | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  | The \class{SMTP} does not modify the message headers in any way.} | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | If there has been no previous \samp{EHLO} or \samp{HELO} command this | 
					
						
							|  |  |  | session, this method tries ESMTP \samp{EHLO} first. If the server does | 
					
						
							|  |  |  | ESMTP, message size and each of the specified options will be passed | 
					
						
							|  |  |  | to it (if the option is in the feature set the server advertises).  If | 
					
						
							|  |  |  | \samp{EHLO} fails, \samp{HELO} will be tried and ESMTP options | 
					
						
							|  |  |  | suppressed. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | This method will return normally if the mail is accepted for at least | 
					
						
							|  |  |  | one recipient. Otherwise it will throw an exception.  That is, if this | 
					
						
							|  |  |  | method does not throw an exception, then someone should get your mail. | 
					
						
							|  |  |  | If this method does not throw an exception, it returns a dictionary, | 
					
						
							|  |  |  | with one entry for each recipient that was refused.  Each entry | 
					
						
							|  |  |  | contains a tuple of the SMTP error code and the accompanying error | 
					
						
							|  |  |  | message sent by the server. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This method may raise the following exceptions: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | \begin{description} | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \item[\exception{SMTPRecipientsRefused}] | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | All recipients were refused.  Nobody got the mail.  The | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | \member{recipients} attribute of the exception object is a dictionary | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | with information about the refused recipients (like the one returned | 
					
						
							|  |  |  | when at least one recipient was accepted). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \item[\exception{SMTPHeloError}] | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | The server didn't reply properly to the \samp{HELO} greeting. | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \item[\exception{SMTPSenderRefused}] | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | The server didn't accept the \var{from_addr}. | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 17:23:34 +00:00
										 |  |  | \item[\exception{SMTPDataError}] | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | The server replied with an unexpected error code (other than a refusal | 
					
						
							|  |  |  | of a recipient). | 
					
						
							|  |  |  | \end{description} | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | Unless otherwise noted, the connection will be open even after | 
					
						
							| 
									
										
										
										
											1999-04-07 15:56:51 +00:00
										 |  |  | an exception is raised. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{quit}{} | 
					
						
							|  |  |  | Terminate the SMTP session and close the connection. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Low-level methods corresponding to the standard SMTP/ESMTP commands | 
					
						
							| 
									
										
										
										
											1998-07-01 14:10:52 +00:00
										 |  |  | \samp{HELP}, \samp{RSET}, \samp{NOOP}, \samp{MAIL}, \samp{RCPT}, and | 
					
						
							|  |  |  | \samp{DATA} are also supported.  Normally these do not need to be | 
					
						
							|  |  |  | called directly, so they are not documented here.  For details, | 
					
						
							|  |  |  | consult the module code. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-08-07 16:03:32 +00:00
										 |  |  | \subsection{SMTP Example \label{SMTP-example}} | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-22 18:04:48 +00:00
										 |  |  | This example prompts the user for addresses needed in the message | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | envelope (`To' and `From' addresses), and the message to be | 
					
						
							| 
									
										
										
										
											1998-12-22 18:04:48 +00:00
										 |  |  | delivered.  Note that the headers to be included with the message must  | 
					
						
							|  |  |  | be included in the message as entered; this example doesn't do any | 
					
						
							|  |  |  | processing of the \rfc{822} headers.  In particular, the `To' and | 
					
						
							|  |  |  | `From' addresses must be included in the message headers explicitly. | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1998-11-30 15:07:26 +00:00
										 |  |  | import smtplib | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | import string | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def prompt(prompt): | 
					
						
							| 
									
										
										
										
											2001-05-20 12:26:04 +00:00
										 |  |  |     return raw_input(prompt).strip() | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | fromaddr = prompt("From: ") | 
					
						
							| 
									
										
										
										
											2001-05-20 12:26:04 +00:00
										 |  |  | toaddrs  = prompt("To: ").split() | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | print "Enter message, end with ^D:" | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Add the From: and To: headers at the start! | 
					
						
							|  |  |  | msg = ("From: %s\r\nTo: %s\r\n\r\n"
 | 
					
						
							|  |  |  |        % (fromaddr, string.join(toaddrs, ", ")))
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | while 1: | 
					
						
							| 
									
										
										
										
											2001-05-20 12:26:04 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         line = raw_input() | 
					
						
							|  |  |  |     except EOFError: | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  |     if not line: | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |     msg = msg + line | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | print "Message length is " + `len(msg)` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-30 15:07:26 +00:00
										 |  |  | server = smtplib.SMTP('localhost') | 
					
						
							| 
									
										
										
										
											1998-06-30 16:53:52 +00:00
										 |  |  | server.set_debuglevel(1) | 
					
						
							|  |  |  | server.sendmail(fromaddr, toaddrs, msg) | 
					
						
							|  |  |  | server.quit() | 
					
						
							| 
									
										
										
										
											1998-06-28 17:55:53 +00:00
										 |  |  | \end{verbatim} |