Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29898

Sending an email with python using csv data for the body

$
0
0

I am using the csv library to pull data to into an email body. I am pulling certain columns from the csv for the body. I am using a junk gmail account to test. I'm just confused on how to use the for loop. If I'm correct, you need a for loop to read the rows then a for loop to email out. I did read Reading and Writing CSV Files but was uncertain how to implement it with smtplib.

import csv, smtplib, ssl

message = """Subject: Your Title

{supervisor} {title} {start}"""

from_address = "test@gmail.com"
password = input("Type your password and press enter: ")

context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(from_address, password)
    with open('test.csv','rb') as file:
        reader = csv.reader(file, delimiter=',')
        next(reader)

            #start = []
            #title = []
            #supervisor = []

            for row in reader:
                title = row[3]
                start = row[0]
                supervisor = row[4]

            for supervisor, title, start in reader:
                 server.sendmail(
                    from_address,
                     Email,
                     message.format(supervisor=supervisor, start=start, title=title)
                 )

print('Emails were sent successfully!')  

CSV File:

StartDate,Employee Name,PC,Title,Supervisor Name,email 
12/9/2019,Katti M. Ricke,289,Recruiter,Courtney Clark,test@gmail.com

Viewing all articles
Browse latest Browse all 29898


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>