I know, There are hundreds of questions with the same query. Sorry about this. I tried almost each of them. But still did not get the solution. In fact, I copied some of the code from one of stackoverflow query and improved it as per my requirement.
I'm writing a script to send error report using python for one of our server. My problem is Email is sending to first member of RECIPIENTS only.
It needs to be send to the team of managers as well as to the admins at a time.
RECIPIENTS = ["[email protected]", '[email protected]' ]
TO = ", ".join(RECIPIENTS)
USER = "[email protected]"
PASSWD = "userpass"
def sendmail():
msg = MIMEMultipart('alternative')
msg['Subject'] = subject()
msg['From'] = USER
msg['To'] = TO
mime_text = MIMEText(get_msg_text(), 'plain')
msg.attach(mime_text)
#-- Auth by Gmail
SERVER = smtplib.SMTP("smtp.gmail.com:587")
SERVER.starttls()
try:
SERVER.login(USER,PASSWD)
except SMTPAuthenticationError, e:
logit(e)
return False
try:
SERVER.sendmail(msg['From'], msg['To'], msg.as_string())
except Exception, e:
logit(e)
return False
finally:
SERVER.quit()
return True
if __name__ == "__main__":
sendmail()
Note :- All the mentioned functions and modules are import
ed properly. In fact, it sends mail successfully.
I tried following old posts:
To send the email to multiple people you need to pass a list not string in sendmail function. This will work fine for you.
try:
SERVER.sendmail(msg['From'], RECIPIENTS, msg.as_string())
except Exception, e:
logit(e)
return False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With