0

I have a scenario where I have Extract Emails from Database and send mails to the respective Users. The values fetched from the database can be of Single Email Id or Multiple Email Id.

I have written the following code and its throwing me an error when it encounters multiple email id seperated by comma.

for index, row in df1.iterrows():
    myVar1 = row["abc"]
    #myVar2 = row["Email"]
    if row["Email"].count('@') > 1:
        myVar2 = ','.join(row["Email"])
    else:
        myVar2 = row["Email"]
    msg = email.message.Message()
    msg['From'] = 'do.not.reply@xyz.com'
    msg['To'] = myVar2
    msg['Subject'] = "abc to be read - {0}".format(myVar1)
    msg.add_header('Content-Type', 'text')
    msg.set_payload("Hello Users,\n\n ABC - {0} has reached its limit.".format(myVar1))
    smtp_obj = smtplib.SMTP("outlook.xyz.com")
    smtp_obj.sendmail(msg['From'],msg['To'], msg.as_string())
    smtp_obj.quit()

if it a single email id then the mail is trigerring properly but if multiple email is passed then each alphabet is seperated by comma

input 'abc@xyz.com,asd@xyz.com' error message : a,b,c,@,x,y,z,.,c,o,m,,,a,s,d,@,x,y,z,.,c,o,m

Please help me in this concern.

Thanks

experiment
  • 315
  • 3
  • 19
Ishan
  • 61
  • 3
  • 10
  • don`t you think that the input should be like this 'abc@xyz.com','asd@xyz.com' rather than 'abc@xyz.com,asd@xyz.com' ? – experiment Aug 08 '18 at 06:48
  • 1
    `row["Email"]` will be a comma separated string right. You send it directly `msg['To']` . If you need to `join`. split it first ('returns a list') then use join. – Madhan Varadhodiyil Aug 08 '18 at 06:48
  • can you provide us with the content in row["Email"] so that it would be easy to provide you with solution. – experiment Aug 08 '18 at 06:55

0 Answers0