Wednesday, December 14, 2011

How to send an email using a python script

Hi all,
If you want to send a mail you have to follow several steps including the following.
open up the web browser.
login into your account.
click on compose mail.
compose the message specify receiver and send the mail.

But if you can send a mail through a script it will save time for you.
Here is a python code for send a mail.

import smtplib
sender= 'senders_name@gmail.com'
receiver = 'receivers_name@gmail.com'
subject='Python is a powerful scripting language'
text = 'This is an auto generated email using python!!!'

# Credentials (if needed)
username = 'senders_name'
password = 'password'

msg = """\
From: %s
To: %s
Subject: %s

%s
""" % (sender, receiver, subject, text)


# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(sender, receiver, msg)
server.quit()

if the file name is mail.py
run this command in terminal by typing

>>python mail.py

No comments:

Post a Comment