Emailing multiple people from a config file
I'm writing a program which emails multiple people and it takes the addresses from a config file. The problem is that every time it runs it only takes to first address and sends it to that one. By the way this is using the configparser modual in python.
heres my code:
reporter_email = '%s' %config.get("email", "reporter_email")
notification_emails = ['%s' %(config.get("email", "notification_emails"))]
to = '%s' %config.get("email", "to")
subject = '%s' %config.get("email", "subject")
body = ('''%s\n
%s
%s''' %(config.get("email", "intro_body"), str(fileNames), config.get("email"开发者_StackOverflow, "con_body")))
to = "To: %s\n" %(to)
subject = "Subject: %s\n\n" %(subject)
smtpObj = smtplib.SMTP('%s' %config.get("email", "portal"), 25)
smtpObj.sendmail(reporter_email, notification_emails, to + subject + body)
smtpObj.quit()
the problem is here:
notification_emails = ['%s' %(config.get("email", "notification_emails"))]
this builds the list of destination emails, but after executing this line, notification_emails
contains a single item. you have to use some sort of loop (or list comprehension) to insert multiple items in this list.
what is the content of your config file ? how can you extract multiple values from the notification_emails
entry ?
(printing the content of variables of interrest during the execution of the script allows to see what they contains, and is a good starting poitn when debugging)
take address and Throw into list or dict, after that use "for loop" and send emails
other way: use bcc
i used a list w/o a for loop.
[Mail Recipients]
p1 = user1@email.com
p2 = user2@email.com
# users defined in config file
p1 = config.get('Mail Recipients', 'p1')
p2 = config.get('Mail Recipients', 'p2')
# pulling users into my script
sendTo = [p1,p2]
now you can use smtp function/method and plug in the 'sendTo' variable in the 'to:' portion of sendmail code and it should work.
精彩评论