Send an email by commandline using python script

Today, I thought to write a script for sending an email by the command line . I selected “python” as the language . Below is the script to do the same.

#!/usr/bin/python
# Humble Devassy humble.devassy@gmail.com
# This is a simple script to send an email
“”” This is the script to send an email”””
import sys
import smtplib

From = raw_input(“From:”)
Toaddress = raw_input(“To:”).split(‘,’)
print “Enter message , Once you have entered the content exit with [Ctrl]+D”
body = “”
while 1:
line = sys.stdin.readline()
if not line:
break
body = body + line
server = smtplib.SMTP(‘localhost’)
server.sendmail(From, Toaddress,body)
server.quit()

print “%s-%s” %(From, Toaddress)
if __name__ == ‘__main__’:
print ” “

Ahhh…. U have to take care of python indentation.. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *