Email goes to first recipient only smtp mail python

痞子三分冷 提交于 2019-12-10 16:40:46

问题


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 = ["mail1@gmail.com", 'mail2@mydomain.in' ]
TO = ", ".join(RECIPIENTS)
USER = "user30@gmail.com"
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 imported properly. In fact, it sends mail successfully.

I tried following old posts:

  • How to send email to multiple recipints using python smtplib?
  • SMTP sent mail to many recipients but doesn't received it
  • Send Email to multiple recipients from .txt file with Python smtplib
  • Why can't I send emails to multiple recipients with this script? and
  • many more

回答1:


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



回答2:


In your first to you have a " but on the second email you used '; try to remove the ' and replace with "



来源:https://stackoverflow.com/questions/27074175/email-goes-to-first-recipient-only-smtp-mail-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!