Python – Send email to a list of emails from a spreadsheet

Nowadays working with Google forms is quite popular. It is used to mass gather information easily. Email addresses are one of the most common piece of information asked. It is stored in a spreadsheet. In this article we shall see how to send an email to all the email addresses present in a spreadsheet.

Prerequisite knowledge:

  1. Loading Excel spreadsheet as pandas DataFrame
  2. Send mail from your Gmail account using Python

Procedure:

  • Step 1: Read the spreadsheet using the pandas library. The structure of the spreadsheet used here is :
  • Step 2: Extablish connection with your gmail account using smtplib library.
  • Step 3: Extract the names and email addresses from the spreadsheet.
  • Step 4: Run a loop and for every record send an email.
  • Step 5: Close the smtp server.

The Python implementation is:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to send email to a list of
# emails from a spreadsheet
  
# import the required libraries
import pandas as pd
import smtplib
  
# change these as per use
your_email = "XYZ@gmail.com"
your_password = "XYZ"
  
# establishing connection with gmail
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(your_email, your_password)
  
# reading the spreadsheet
email_list = pd.read_excel('C:/Users/user/Desktop/gfg.xlsx')
  
# getting the names and the emails
names = email_list['NAME']
emails = email_list['EMAIL']
  
# iterate through the records
for i in range(len(emails)):
  
    # for every record get the name and the email addresses
    name = names[i]
    email = emails[i]
  
    # the message to be emailed
    message = "Hello " + name
  
    # sending the email
    server.sendmail(your_email, [email], message)
  
# close the smtp server
server.close()

chevron_right


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.