0

I am trying to send my class their quiz results with their First Name and Score using an excel file (pandas df). However, how do I loop through the df line by line to extract individual names and scores?

The df looks like this:

enter image description here

The following output obviously only works for the first line item of the df as I have specified index of 0 for First Name and Score both.

email_list = [abc@gmail.com, def@gmail.com, ghi@gmail.com]

email['from'] = 'Zoya Aqib'
email['subject'] = 'Corporate Rating Quiz'


for recipient in email_list:
    email['To'] = recipient
    email.set_content(html.substitute({'name' : df['First Name'][0], 'score' : df['Score'][0]}))
    with smtplib.SMTP(host='smtp.gmail.com',port=587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.login('****@gmail.com','****')
        smtp.send_message(email)
        del email['To']
        print('all done!')

Edit: for those suggesting iterating over df, this error is received when i try to do so: "smtplib.SMTPRecipientsRefused: {}" And the email ends up only being sent to first recipient

Zoya Aqib
  • 25
  • 2

1 Answers1

1
for i, item in df.iterrows():
    email['To'] = item['Email']
    email.set_content(html.substitute({'name' : item['First Name'], 'score' : item['Score']}))
    with ...

If the emails are not in your df add this after the emai_list initialization and befor the for loop starts.

df['Email'] = email_list
samusa
  • 449
  • 2
  • 11
  • Tried that! Error received: "smtplib.SMTPRecipientsRefused: {}". Ends up sending to only the first recipient. – Zoya Aqib Apr 12 '21 at 20:41