How to send an email with GMail in Python from Windows

gordyt gordyt at gmail.com
Thu Mar 12 15:46:03 CET 2009


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( ['somewhat at somewhere.com'], 'this is just a test',
        "hello world!" )


--gordy



More information about the Python-list mailing list