[Tutor] Mail? What's that?

Luke Paireepinart rabidpoobear at gmail.com
Thu Dec 6 06:47:32 CET 2007


Ricardo Aráoz wrote:
> So I eventually got to sending mail with python.
> Some articles, trying and google led me to this script:
>
> import smtplib
> import time
>
> date = time.ctime(time.time( ))
> >From = 'mymail at gmail.com'
> To = ['othermail at hotmail.com', 'YetOtherMail at yahoo.com']
> Subj = 'Hi'
> text = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n'
>                          % (From, ';'.join(To), date, Subj))
>
> s = smtplib.SMTP('smtp.gmail.com')
> s.set_debuglevel(1)
> s.ehlo()
> s.starttls()
> s.ehlo()
> s.login('foo', 'bar')
> s.sendmail(From, To, text)
> s.close()
>
>
> So, if there's someone who really knows this stuff in the neighborhood
> I'd like to ask a couple of questions.
> What is ehlo and why do I have to call it twice? And set_debuglevel?
> If I where to connect through other smtp server the sequence would be
> the exactly the same, say yahoo or hotmail?
> Are From: To: Date: and Subject: mandatory in the contents of the
> email(text)?  Do I have to put "real" address in  From when calling
> sendmail()? And in the contents?
> Ok, if someone can answer these I'll be grateful.
>
> TIA
>
> Ricardo
>   
Ricardo -
I say this in the nicest way possible, but did you RTFM? :)
Python has built-in help support on modules.
You should start there, do some Googling, and if you're stumped, get 
some help.  Not for any other reason than it'll probably get you 1) more 
experience at navigating the docs, and 2) a quicker, probably more 
detailed response.

So using Help, we get:


 >>> import smtplib
 >>> help(smtplib)
Help on module smtplib:

NAME
    smtplib - SMTP/ESMTP client class.

FILE
    c:\python24\lib\smtplib.py

[snip 10 pages of documentation]


 >>> help(smtplib.SMTP.set_debuglevel)
Help on method set_debuglevel in module smtplib:

set_debuglevel(self, debuglevel) unbound smtplib.SMTP method
    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.

 >>> help(smtplib.SMTP.ehlo)
Help on method ehlo in module smtplib:

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

 >>> help(smtplib.SMTP.sendmail)
Help on method sendmail in module smtplib:

sendmail(self, from_addr, to_addrs, msg, mail_options=[], 
rcpt_options=[]) unbound smtplib.SMTP method
    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 at one.org","two at two.org","three at three.org","four at four.org"]
     >>> msg = '''\
     ... From: Me at my.org
     ... Subject: testin'...
     ...
     ... This is a test '''
     >>> s.sendmail("me at my.org",tolist,msg)
     { "three at 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.

 >>>




So mess around in the built-in docs, then check python.org's docs, and 
let us know what you find :)
Also, try experimenting! pass it stuff that you don't think will work 
just to see if maybe it does :D
-Luke


More information about the Tutor mailing list