Email goes to first recipient only smtp mail python

5.8k Views Asked by At

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:

4

There are 4 best solutions below

0
On

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

0
On

I've tried all the combinations on this post and the one recommended and using Python3 I couldn't make it work. This is the only way I manage to do it. It is not perfect as the ones reciving the emails are not geting all the emails where is being sent.

  for email in SMTP["to"]:
       msg = MIMEMultipart()
       msg['From'] = SMTP["user"]
       msg['To'] = email
       msg['Subject'] = f"[Repoman Notifier] Run on {today}!"
       msg.attach(MIMEText(html_builder(report_data), 'html'))

       server = smtplib.SMTP(f'{SMTP["host"]}:{SMTP["port"]}')
       server.starttls()
       server.login(msg['From'], SMTP["password"])

       server.send_message(msg, msg['From'], msg["to"])

       server.quit()
0
On

pretty old post, but just in-case some else runs to the same issue. we had the same issue and the only way we could get it working was to use MIMEMultipart:

import smtplib
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

email_to = ['[email protected]', '[email protected]', '[email protected]']
msg = MIMEMultipart('related')
msg['Subject'] = " You Tell me"
msg['From'] = "[email protected]"
msg['To'] = ", ".join(email_to)
#if reply_to:
#   msg['Reply-to'] = reply_to

part1 = MIMEText("WHY ME?!?!!", 'html')
msg.attach(part1)

s = smtplib.SMTP('smtp.server.com')
s.sendmail("[email protected]", email_to, msg.as_string())
s.quit()
0
On

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