Sending Mail Using Python’s smtplib library

We shall discuss about automating the Sending Email Process.

Here I am making assumption that Our emails is stored as a Dataframe.

So if emails are present in excel_file then it’s obvious that we can convert them into dataframe . Although it is also possible to get data from excel but for me it is more easy to handle data as a dataframe.

Now move forward and take a look at some coding part.

a) First import some important libraries.

b) Convert Excel file to Dataframe

Note : Excel file contains ‘Email’ column as header so in Dataframe “Email” becomes column name.

Note: You may need to install openpyxl module.

c) Create smtp object

Here point to note is that port 587 has been used that is by default mail submission port. When a mail client or server is submitting an email to be routed by a proper mail server, it should always use this port. know more here

d) Start tls(Transport layer security) service

This is done for secure end-to-end data transmission between applications.To know more click here

e) Login with your credentials as shown below

Here GMAIL_ID is your gmail id from which you want to send the mail and GMAIL_PSWD is that gmail’s password.

Also make sure to allow less secure apps for login because your application tries to login to your gmail account but gmail does not give permission to do so.

For better understanding of steps see below :

i) Open less secure apps page to turn on less secure apps toggle button

BEFORE

AFTER

ii) Now Try to login using running python script. It will throw error showing that unable to authorize(Some times it may not occcur)

So, now go to your gmail account and there will be message of Security alert. See below:

Click on check activity button .Below pic will be displayed:

Click on Yes, it was me button and this image will be displayed:

iii) Next step is to generate list of Email ids to which we have to send the mail.

iv) Now write your subject and message of mail and send it.

It’s Done ………..

Cheers…….Happy Learning!!!!!

My Github Project Link: https://github.com/pandeynandancse/auto_mail_sender

Contact Me : https://www.linkedin.com/in/nandan-pandey/

--

--