Friday, 2 June 2017

Python Script to Send Mails Using Gmail

Hi friends,

I have been sending mails to students telling them about my blog but I got tired soon enough. So, I decided to automate the process of sending mails using Python. So, I wrote a Python script to automatically send mails to students. The script uses smtplib library in Python to interact with Gmail. The task is achieved using the following main steps:
  1. Connecting to the Gmail Server
  2. Sending the SMTP "Hello" Message
  3. Starting TLS Encryption
  4. Logging in to the SMTP Server
  5. Logging in to the SMTP Server
  6. Disconnecting from the SMTP Server
You can see each of the above mentioned steps in detail from the following link

# -*- coding: utf-8 -*-
"""
Created on Thu Jun  1 11:01:56 2017
Script to send mails thorugh Gmail using smtplib
"""

import smtplib
# Connecting to an SMTP Server
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)

# Sending the SMTP “Hello” Message
smtpObj.ehlo()

# Starting TLS Encryption
smtpObj.starttls()

# Logging in to the SMTP Server
smtpObj.login('your_gmail_id', 'your_gmail_password')

# Sending an Email
smtpObj.sendmail('your_gmail_id', 'receiver_gmail_id', 
'Subject: Data Science, Machine Learning & Coding Interview Questions \
\nMessage to be sent with the mail')

# Disconnecting from the SMTP Server
smtpObj.quit()

Note: Make sure to make appropriate changes in the script before running.
Share:

1 comment:

Contact Me

Name

Email *

Message *

Popular Posts