Sending email with python….

Hi. Have you ever thought of sending a mail to your friends without opening the gmail website in your browser?….It takes a bit time for everyone in opening browser, logging into your account and then composing your email. But python can make your work simpler and faster.

In this blog I will explain you about the process of sending an email using python script through a terminal and about the problems that you might encounter during this process.

https://github.com/KalidindiAmitRaja/FOSS-tasks/blob/master/CLImail/CLImail.py

Here is a simple python code for sending an email through a terminal.

import smtplib
import getpass
b=input("From:")
c=input("To:")
d=input("Message:")
e=getpass.getpass()
a=smtplib.SMTP('smtp.gmail.com',587)
a.ehlo()
a.starttls()
a.login(b,e)
a.sendmail(b,c,d)
a.quit()

SMTP stands for Simple Mail Transfer Protocol, it is a protocol used for sending an email. smtplib is a module which is already pre-installed in a python package. This module helps us in connecting to an SMTP server.

First mistake:Create a python file named email.py and copy the above code into it and try to run it in the terminal and see what happens. You will receive an error message,”module smptlib doesnot exit”. Try to install it using the command pip3 install smtplib and try to run it. Once again you will receive the similar message that smtplib does not exit. But I have already mentioned that smtplib is pre-installed. Then what might be the reason for the error?…..You received an error message because email is one of the modules of python package. So you cannot name your python file with a module name like email.py


The first step in this process is to import all the necessary modules.

import smtplib
import getpass

getpass is a built-in  module which takes input in the form of a password. These two lines of code will import the smtplib and getpass modules.

The next step is connecting to an SMTP Server. For connecting to an SMTP server you need to have an SMTP server domain name. The domain name for the SMTP server will usually be the name of your email provider’s domain name, with smtp. in front of it , for example “smtp.gmail.com”. The following piece of code will help us in connecting to the SMTP server through the port 587. Make it sure that you store it into a variable as the rest of processes like logging in,etc must know the email provider’s domin name.

a=smtplib.SMTP('smtp.gmail.com',587)

By calling ehlo() we can establish a connection to the server. starttls() puts your SMTP connection in TLS mode. TLS is Transport Layer Security protocol that protects communications on internet.

Now the next step is logging into the SMTP server and sending the mail .The following piece of code will help you in that process.

a.login('email address','password')
a.sendmail('Your email address','To address','message')

You can take inputs from the user to fill the necessary details like email address, to address, etc. The final step after sending the mail is to quit the SMTP server and you can do it by

a.quit().

NOTE: Before running your python code make sure that you turn on the access to third party applications which is present in Sign-in and Security settings in your email account.

In this way python can help you to send mails directly through terminal.