I am new to python I wrote this simple code which should loop on 20 records sending emails to each
import csv,smtplib,time
def send_mail(SEND_TO,title):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
EMAIL = 'myemail@gmail.com'
PASSWORD = 'password'
server.login(EMAIL, PASSWORD)
temp = f"Subject: Hello\nHi,{title}"
server.sendmail(EMAIL, SEND_TO, temp)
server.close()
return True
except:
return False
with open('test.csv') as f:
clients = [{k: v for k, v in row.items()}
for row in csv.DictReader(f, skipinitialspace=True)]
for i in clients:
send_mail(i['email'],i['Title'])
#time.sleep(30)
for some reason, only the first record is sending the email (I tried switch record places in the csv,it gives the same issue).
I tried adding a delay of 30 seconds but still same outcome, is there something I am doing wrong?
Note: I don't want to send 1 email to the 20 employees because each one will get a separate email body. Also all emails I am sending to are gmail