Sunday, October 23, 2011

Sending mail through gmail using Python

The more I use Python the more I like it. Thanks to its immense user base there are libraries available to do practically anything you can think of. For instance, the following very simple code snippet sends mail using an existing gmail account:

import smtplib

fromA = 'your_account@gmail.com'
to = raw_input("Destination Mail: ")

username="your_account@gmail.com"
password=raw_input("Introduce your password: ")

msg="Subject: Test Subject\n\n"
msg=msg+raw_input("Message: ")
server=smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username,password)
server.sendmail(fromA,to,msg)
server.quit()