Custom Python Script: Sending an Email Through Terminal

July 7, 2016 | Views: 10755

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Ever wondered how you’d write up and send an email without launching the browser and the gateway portal of the serving entity? In this post, we describe how to send an email through a command line terminal. You’ll be able to do so with little prior knowledge of Python or any other programming language.


Lets get started…

1. Import major modules required to carry out the operations

import smtplib  #this is required so that we can use  email requisite modules

import getpass #to prevent shoulder surfing attacks thus hiding screen display while taken pass

from email.mime.text import MIMEText  #to use MIMEText objects in our custom script

2. Define the custom module for the sending function “sendemail”

def sendemail(from,pass,to,subject,body):






smtpserver=smtplib.SMTP(‘’,587)   #details of smtp server

print “trying to establish connection with mail server”

smtpserver.ehlo()   #send an ehlo message to server

print “starting private session”

smtpserver.starttls() #establish encrypted session


print “logging into the email server”

smtpserver.login(from,pass)  #login to the smtp server


smtpserver.close() #close the connection

print “message transfer successfull.”


print “failure sending the message!” #catch an exception

3. Define the main function

def main():

from=raw_input(“enter your email(gmail) ID/n”)


to=raw_input(“enter receiver email ID/n”)

subject=raw_input(“enter subject/n”)

body=raw_input(“enter body/n”)


if __name__==’__main__’:

4. Integrate all the pieces of code and save it into a single file with the extension .py.

5. Run the script using “python” on your terminal (or run it as bash script by first setting permissions using chmod and adding a line of code to starting of file with the location of python.)

Note: We’ve described this process incorporating the sender email as Gmail. It can be changed to another program with appropriate port numbers and service locations of other providers.

Note: This can be enhanced by using SMTP_SSL object to have a more secure SSL connection with a change in port number 465. If doing so, server_ssl_starttls() is not needed.

Thanks for reading this. Please comment below!

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. great it really helpful

  2. Thanks for the information. I learn something new thanks to you.

  3. Forgot to say, cheers for the article, quite a cool implementation

  4. No shebang on the start.

    try adding the following as the first line, you may have to find your python instance if it is in a different place:


Page 3 of 3«123
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?