0

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

User19
  • 101
  • 5
  • you want to send multiple emails from 1 account , and all of them should be different(personalized),right? – Aaryansh Sahay May 28 '20 at 15:39
  • @AaryanshSahay yes – User19 May 28 '20 at 15:40
  • Get rid of the `try... except`, which could be masking all sorts of things. Just allow any exception to bubble, and then see if you get any clues. – alani May 28 '20 at 16:04
  • This is a duplicate. There are a lot of questions and answers for this already. Ultimately, they point to https://support.google.com/mail/answer/7126229?visit_id=637262788710115783-3911985954&rd=2#cantsignin and either allowing less secure apps or unlocking Captcha. Good luck. – patmcb May 28 '20 at 16:11
  • Does this answer your question? [smptlib and gmail python script problems](https://stackoverflow.com/questions/778202/smtplib-and-gmail-python-script-problems) – patmcb May 28 '20 at 17:20

0 Answers0