smtplib.SMTP

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

## Class SMTP

Known Subclasses:
  `SMTP_TLS`

    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.

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

Class Variable Summary
`int` | `debuglevel` = 0
`int` | `does_esmtp` = 0
`NoneType` | `ehlo_resp` = None
`NoneType` | `file` = None
`NoneType` | `helo_resp` = None

Method Details

### __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().

### close(self) Close the connection to the SMTP 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.

### 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.

### docmd(self, cmd, args='') Send a command, and return its response code.

### ehlo(self, name='') SMTP 'ehlo' command. Hostname to send for this command defaults to the FQDN of the local host.

### expn(self, address) SMTP 'verify' command -- checks for address validity.

### 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.

### has_extn(self, opt) Does the server support a given SMTP service extension?

### helo(self, name='') SMTP 'helo' 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.

### 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.

### mail(self, sender, options=[]) SMTP 'mail' command -- begins mail xfer session.

### noop(self) SMTP 'noop' command -- doesn't do anything :>

### putcmd(self, cmd, args='') Send a command to the server.

### quit(self) Terminate the SMTP session.

### rcpt(self, recip, options=[]) SMTP 'rcpt' command -- indicates 1 recipient for this mail.

### rset(self) SMTP 'rset' command -- resets session.

### send(self, str) Send `str' to the server.

### 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.

### 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.

### 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.

### verify(self, address) SMTP 'verify' command -- checks for address validity.

### vrfy(self, address) SMTP 'verify' command -- checks for address validity.

Class Variable Details

    ### debuglevel Type: `int` Value:
        0

    ### does_esmtp Type: `int` Value:
        0

    ### ehlo_resp Type: `NoneType` Value:
        None

    ### file Type: `NoneType` Value:
        None

    ### helo_resp Type: `NoneType` Value:
        None

Generated by Epydoc 2.0 on Mon Feb 21 21:56:53 2005 | http://epydoc.sf.net