Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email goes to first recipient only smtp mail python

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 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
like image 507
trex Avatar asked Nov 22 '22 07:11

trex


1 Answers

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
like image 76
Prashant Pratap Singh Avatar answered Dec 28 '22 20:12

Prashant Pratap Singh