Module smtplib :: Class SMTP
[hide private]
[frames] | no frames]

_ClassType SMTP

This class manages a connection to an SMTP or ESMTP server.
SMTP Objects:
    SMTP objects have the following attributes:
        helo_resp
            This is the message given by the server in response to the
            most recent HELO command.

        ehlo_resp
            This is the message given by the server in response to the
            most recent EHLO command. This is usually multiline.

        does_esmtp
            This is a True value _after you do an EHLO command_, if the
            server supports ESMTP.

        esmtp_features
            This is a dictionary, which, if the server supports ESMTP,
            will _after you do an EHLO command_, contain the names of the
            SMTP service extensions this server supports, and their
            parameters (if any).

            Note, all extension names are mapped to lower case in the
            dictionary.

    See each method's docstrings for details.  In general, there is a
    method of the same name to perform each SMTP command.  There is also a
    method called 'sendmail' that will do an entire mail transaction.
    

Instance Methods [hide private]
 
__init__(self, host='', port=0, local_hostname=None)
Initialize a new instance.
 
set_debuglevel(self, debuglevel)
Set the debug output level.
 
connect(self, host='localhost', port=0)
Connect to a host on a given port.
 
send(self, str)
Send `str' to the server.
 
putcmd(self, cmd, args='')
Send a command to the server.
 
getreply(self)
Get a reply from the server.
 
docmd(self, cmd, args='')
Send a command, and return its response code.
 
helo(self, name='')
SMTP 'helo' command.
 
ehlo(self, name='')
SMTP 'ehlo' command.
 
has_extn(self, opt)
Does the server support a given SMTP service extension?
 
help(self, args='')
SMTP 'help' command.
 
rset(self)
SMTP 'rset' command -- resets session.
 
noop(self)
SMTP 'noop' command -- doesn't do anything :>
 
mail(self, sender, options=[])
SMTP 'mail' command -- begins mail xfer session.
 
rcpt(self, recip, options=[])
SMTP 'rcpt' command -- indicates 1 recipient for this mail.
 
data(self, msg)
SMTP 'DATA' command -- sends message data to server.
 
verify(self, address)
SMTP 'verify' command -- checks for address validity.
 
vrfy(self, address)
SMTP 'verify' command -- checks for address validity.
 
expn(self, address)
SMTP 'verify' command -- checks for address validity.
 
login(self, user, password)
Log in on an SMTP server that requires authentication.
 
starttls(self, keyfile=None, certfile=None)
Puts the connection to the SMTP server into TLS mode.
 
sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])
This command performs an entire mail transaction.
 
close(self)
Close the connection to the SMTP server.
 
quit(self)
Terminate the SMTP session.
Class Variables [hide private]
  debuglevel = 0
  file = None
  helo_resp = None
  ehlo_resp = None
  does_esmtp = 0
Method Details [hide private]

__init__(self, host='', port=0, local_hostname=None)
(Constructor)

 

Initialize a new instance.

If specified, `host' is the name of the remote host to which to connect. If specified, `port' specifies the port to which to connect. By default, smtplib.SMTP_PORT is used. An SMTPConnectError is raised if the specified `host' doesn't respond correctly. If specified, `local_hostname` is used as the FQDN of the local host. By default, the local hostname is found using socket.getfqdn().

set_debuglevel(self, debuglevel)

 

Set the debug output level.

A non-false value results in debug messages for connection and for all messages sent to and received from the server.

connect(self, host='localhost', port=0)

 

Connect to a host on a given port.

If the hostname ends with a colon (`:') followed by a number, and there is no port specified, that suffix will be stripped off and the number interpreted as the port number to use.

Note: This method is automatically invoked by __init__, if a host is specified during instantiation.

getreply(self)

 

Get a reply from the server.

Returns a tuple consisting of:

  • server response code (e.g. '250', or such, if all goes well) Note: returns -1 if it can't read response code.
  • server response string corresponding to response code (multiline responses are converted to a single, multiline string).

Raises SMTPServerDisconnected if end-of-file is reached.

helo(self, name='')

 

SMTP 'helo' command. Hostname to send for this command defaults to the FQDN of the local host.

ehlo(self, name='')

 

SMTP 'ehlo' command. Hostname to send for this command defaults to the FQDN of the local host.

help(self, args='')

 

SMTP 'help' command. Returns help text from server.

data(self, msg)

 

SMTP 'DATA' command -- sends message data to server.

Automatically quotes lines beginning with a period per rfc821. Raises SMTPDataError if there is an unexpected reply to the DATA command; the return value from this method is the final response code received when the all data is sent.

login(self, user, password)

 
Log in on an SMTP server that requires authentication.

The arguments are:
    - user:     The user name to authenticate with.
    - password: The password for the authentication.

If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.

This method will return normally if the authentication was successful.

This method may raise the following exceptions:

 SMTPHeloError            The server didn't reply properly to
                          the helo greeting.
 SMTPAuthenticationError  The server didn't accept the username/
                          password combination.
 SMTPException            No suitable authentication method was
                          found.

starttls(self, keyfile=None, certfile=None)

 

Puts the connection to the SMTP server into TLS mode.

If the server supports TLS, this will encrypt the rest of the SMTP session. If you provide the keyfile and certfile parameters, the identity of the SMTP server and client can be checked. This, however, depends on whether the socket module really checks the certificates.

sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])

 
This command performs an entire mail transaction.

The arguments are:
    - from_addr    : The address sending this mail.
    - to_addrs     : A list of addresses to send this mail to.  A bare
                     string will be treated as a list with 1 address.
    - msg          : The message to send.
    - mail_options : List of ESMTP options (such as 8bitmime) for the
                     mail command.
    - rcpt_options : List of ESMTP options (such as DSN commands) for
                     all the rcpt commands.

If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.  If the server does ESMTP, message size
and each of the specified options will be passed to it.  If EHLO
fails, HELO will be tried and ESMTP options suppressed.

This method will return normally if the mail is accepted for at least
one recipient.  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:

 SMTPHeloError          The server didn't reply properly to
                        the helo greeting.
 SMTPRecipientsRefused  The server rejected ALL recipients
                        (no mail was sent).
 SMTPSenderRefused      The server didn't accept the from_addr.
 SMTPDataError          The server replied with an unexpected
                        error code (other than a refusal of
                        a recipient).

Note: the connection will be open even after an exception is raised.

Example:

 >>> import smtplib
 >>> s=smtplib.SMTP("localhost")
 >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"]
 >>> msg = '''\
 ... From: Me@my.org
 ... Subject: testin'...
 ...
 ... This is a test '''
 >>> s.sendmail("me@my.org",tolist,msg)
 { "three@three.org" : ( 550 ,"User unknown" ) }
 >>> s.quit()

In the above example, the message was accepted for delivery to three
of the four addresses, and one was rejected, with the error code
550.  If all addresses are accepted, then the method will return an
empty dictionary.