Python: Sending Email to Multiple Recipients -
i'm trying send email multiple people using python.
def getrecipients(filewithrecipients): recipients = [] f = open(filewithrecipients, 'r') line in f: recipients.append(line) return recipients def test_email(): gmail_user = "csarankings@gmail.com" gmail_pass = getpassword("pass.txt") = "csarankings@gmail.com" = getrecipients("recipients.txt") date = getcurrentdate() subject = "current csa rankings: " + date text = createemailmessagefromfile("rankings.txt") message = """\from: %s\nto: %s\nsubject: %s\n\n%s """ % (from, ", ".join(to), subject, text) try: server = smtplib.smtp("smtp.gmail.com", 587) server.ehlo() server.starttls() server.login(gmail_user, gmail_pass) server.sendmail(from, to, message) server.close() print("it worked") except: print("it failed :(")
for whatever reason, emails go first recipient in list of recipients (the to
variable within test_email()
function.
my recipients file consists of email address per line (only has 2 in there @ moment). thought there might wrong getrecipients() function, hardcoded 2 emails to
variable , works first email address , not second.
any idea why might happening? suspect has with:
", ".join(to)
where set message
variable.
nb: test_email()
largely based on upvoted answer here:
how send email gmail provider using python?
edit: it's worth, when hardcode list of single email address variable, subject displays in email. when use getrecipients() function (even 1 email address in recipients.txt file), displays in body of email:
subject: current csa rankings 6/13/15
not sure if relevant thought might related issue.
edit_2: don't think question duplicate of:
how send email multiple recipients using python smtplib?
because, while use smtplib.sendmail()
function, don't use email module @ all, our code different...
Comments
Post a Comment