How to send an email with GMail in Python from Windows

Discussion in 'Python' started by ∂ √ ¡ ŋ ∂ ♪ ђ, Mar 12, 2009.

  1. Hi
    Can somebody help me with sending an email using Python from GMail
    Here's what I tried but it fails always.

    ____________________________________________________________________
    import smtplib
    import base64

    smtpserver = 'smtp.gmail.com'
    AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
    smtpuser = '' # for SMTP AUTH, set SMTP username here
    smtppass = '*****' # for SMTP AUTH, set SMTP password here

    RECIPIENTS = ['']
    SENDER = ''
    mssg = open('mssg.txt', 'r').read() # I am reading from this file on
    the same directory

    session = smtplib.SMTP(smtpserver,'465')
    session.ehlo()
    #session.esmtp_features["auth"] = "LOGIN PLAIN"
    session.connect(smtpserver,'465')
    session.ehlo()
    session.starttls()
    session.set_debuglevel(1)
    session.helo()

    if AUTHREQUIRED:
    try:
    session.login(smtpuser, smtppass)

    except SMTPAuthenticationError, e:
    # if login fails, try again using a manual plain login method
    smtp.docmd("AUTH LOGIN", base64.b64encode( smtpuser ))
    smtp.docmd(base64.b64encode( smtppass ), "")
    smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
    if smtpresult:
    errstr = ""
    for recip in smtpresult.keys():
    errstr = """Could not delivery mail to: %s

    Server said: %s
    %s

    %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    raise smtplib.SMTPException, errstr
    ____________________________________________________
    This is the Stack Trace I got when I ran the above script after a very
    long time(>15min)

    Traceback (most recent call last):
    File "C:\Python26\Mail.py", line 13, in <module>
    session = smtplib.SMTP(smtpserver,'465')
    File "C:\Python26\lib\smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
    File "C:\Python26\lib\smtplib.py", line 296, in connect
    (code, msg) = self.getreply()
    File "C:\Python26\lib\smtplib.py", line 340, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
    smtplib.SMTPServerDisconnected: Connection unexpectedly closed

    Tool completed with exit code 1

    ______________________________________________________________________
    Thanks
    Avinash
     
    ∂ √ ¡ ŋ ∂ ♪ ђ, Mar 12, 2009
    #1
    1. Advertising

  2. ∂ √ ¡ ŋ ∂ ♪ ђ

    dorzey Guest

    dorzey, Mar 12, 2009
    #2
    1. Advertising

  3. ∂ √ ¡ ŋ ∂ ♪ ђ

    gordyt Guest

    Howdy Avinash,

    Here is a simple example for you.

    from smtplib import SMTP
    HOST = "smtp.gmail.com"
    PORT = 587
    ACCOUNT = "" # put your gmail email account here
    PASSWORD = "" # put your gmail email password here

    def send_email(to_addrs, subject, msg):
    server = SMTP(HOST,PORT)
    server.set_debuglevel(1) # you don't need this (comment out to
    avoid debug messages)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(ACCOUNT, PASSWORD)
    server.sendmail(ACCOUNT, to_addrs,
    """From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
    ACCOUNT, ",".join(to_addrs), subject, msg
    )
    )
    server.quit()

    if __name__ == "__main__":
    send_email( [''], 'this is just a test',
    "hello world!" )


    --gordy
     
    gordyt, Mar 12, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Genellina

    Re: smtplib send email by using gmail smtp server

    Gabriel Genellina, May 3, 2009, in forum: Python
    Replies:
    0
    Views:
    487
    Gabriel Genellina
    May 3, 2009
  2. Junkone

    send email using gmail as smtp

    Junkone, Dec 20, 2007, in forum: Ruby
    Replies:
    3
    Views:
    314
    Junkone
    Dec 20, 2007
  3. Brian Rhiel
    Replies:
    1
    Views:
    200
    Hassan Schroeder
    May 31, 2010
  4. ashish makani
    Replies:
    5
    Views:
    697
    Mark Lawrence
    Sep 20, 2012
  5. heycooldude
    Replies:
    0
    Views:
    284
    heycooldude
    Mar 26, 2014
Loading...

Share This Page