/  Technology   /  How to send an email using python
How to send an email using python

How to send an email using python

As we all know python is fastest growing language be it in terms of libraries, application that can be used in machine learning, artificial intelligence, web development and many other things which python has got covered. It is famous for its simple programming syntax, code readability which make work more productive and easier.

 

Though python has many applications such as in making web application, machine learning, etc. In this article we will discuss its one such application of how to send email using python. In this article we will walk through the process of sending email using python.

 

Getting Started

 

First of all you should have email account to send email, without the email account it is not possible to send email. For this you can use Gmail, Hotmail, yahoo, etc. any of email service provider . You can send email for signup confirmation, for sending otp, confirmation of buying any product or anything. You can automate it using python, which is quite time-consuming and error-prone task manually.

 

In this article we will use gmail with which most of us are comfortable with. To use gmail we have to use mail server and we will using google mail server.

 

So, to use Google’s server we will use python library known as SMTP which stands for Simple Mail Transfer Protocol. It is standard python library  ,which means we don’t have to install it .We will use SMTP to use google server .Lets us take an example to get clear understanding of using server.

For example if I want to send email to a client on behalf of i2tutorial. So I will using i2tutorial id i.e. info@i2tutorials.com. For this email I will require the domain name as server which we will be smtp.i2 tutorial.com. Similarly, if I am using gmail then I will require

smtp.gmail.com server.

 

STMP is simple mail transfer protocol .Every protocol works on port number. SMTP works on two ports which are Transport Layer Security (TSL) and Secure Socket Layer (SSL). Every time you login into gmail with your username and password depending upon security you want you can choose either of two TLS or SSL.

 

As we are using gmail there are couple of setting that we need to change in your gmail account in order to send email from your gmail account. First you need to turn on less secure app access in your gmail account. To change this setting just click on the below URL.

 

https://myaccount.google.com/lesssecureapps

 

This setting is necessary as google will not allow to gain access for your python script. So now we are ready to send mail from your email account.

 

Sending Mail

 

import smtplib
conn=smtplib.smtp('smtp.gmail.com', 587)
conn.starttls()

First we need to import smtp which is inbuilt library in python. Then we need to create a session by using method SMTP() in which we have to specify which server we are using .Here we are using gmail that’s why we are using ‘smtp.gmail.com’.  Next we need to specify the port we want to use. If you are specifying port as ‘587’  then you are using TSL and if you specify port as ‘465’ then you are using SSL. Here we want to use Transport Layer Security (TSL) that’s why we specified the port as 587.

 

So now we just created a connection and stored it in variable called as conn. This will be our connection with server or google. As we have connected with gmail, then we can start the server.

 

conn.login('your email' , 'your password')

 

After we have started the server, next step is authentication. Basically in this step you have to login into the server by using your email id and password.

 

msg = 'This is trial mail for learning python'

 

After login we need to write the message that you want to send.

 

conn.sendmail('your mail', 'Receiver email', msg)
conn.quit()

 

In this step we need to specify the receivers email address to whom you want to send mail. Then after the mail is sent we quit the server and the session will be destroyed and you will be logged out of your gmail.

 

Below is whole source code

 

import smtplib
conn=smtplib.SMTP('smtp.gmail.com', 587)
conn.starttls ()
conn.login('your email', 'your password')
msg = 'This is trial mail for learning python'
conn.sendmail('your mail' , 'Receiver email' , msg)
conn.quit()

 

Output :

 

How to send an email using python

In the above program you can see we have to specify the senders,

receivers email in the program and also the password of senders mail which is in plain text and is visible to every one. So we will try to get the senders and receivers mail as a input from user and will encode the password so that no one could see it. It will make the program more  generalized.

 

Source Code :

 

import getpass
import smtplib
Email1=input("Enter Your Email ID:")
Pass=getpass.getpass ("Enter Your Password")
Email2=input ("Enter Receiver Email ID:")
conn=smtplib.SMTP('smtp.gmail.com','587')
conn.starttls()
conn.login(Email1,pass)
msg =  'This is trial mail for learning python '
conn.sendmail(Email1, Email2, msg)
conn.quit()

 

Sending Same Mail To Multiple People

 

If you want to send same mail or same message to multiple peoples, you have to use for loop. For looping over the different email ids and send mail to each of them. It can be implemented as

 

Source Code :

 

import smtplib
emails = ['abc@gmail.com', 'xyz@gmail.com', 'pqr@gmail.com']
for mail in emails:
conn=smtplib.STMP('stmp.gmail.com',587)
conn.starttls ()
conn.login ('your Email', 'Your Password')
msg = 'This is trial mail for learning python'
conn.sendmail('your mail', mail, msg)
conn.quit()

 

Using Local SMTP Server

 

If you want to test email functionality, without sending yourself a bunch of email. Then you can use localhost debug server for testing  emails. It prints the content of mail or message in the console rather than sending mail to the specified address.

 

For that we need to start a debug mail server on our localhost. To start debug mail server on our localhost, use the following command in terminal.

 

python -m smtpd -c DebuggingServer -n localhost:1025

 

Now any email sent through this server will be discarded  and shown in the console as

 

How to send an email using python

 

In the article we have seen how to send mail using python and what all the things you require to send mail using python. Hope you will enjoy sending emails with python.

 

 

 

 

 

Leave a comment