5

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:

Community
  • 1
  • 1
trex
  • 3,848
  • 4
  • 31
  • 54
  • @all :- Please specify the reason before you do -1. If you can't specify the reason, you're not the right person to do that. – trex Nov 22 '14 at 04:55
  • @nu11p01n73R - I tried that too, there was an error `list has no object lstrip()` – trex Nov 22 '14 at 05:11
  • @nu11p01n73R:- Can you please link the documentation or syntax please?Thanks – trex Nov 22 '14 at 05:22
  • did you ever find a solution for this? – pyramidface Nov 10 '15 at 19:44
  • edit: nevermind, for all those who stumble upon this thread, I found a solution here on the answer with the most votes: http://stackoverflow.com/questions/8856117/how-to-send-email-to-multiple-recipients-using-python-smtplib – pyramidface Nov 10 '15 at 19:58
  • other solution https://stackoverflow.com/a/64947133/6007952 – tnusraddinov Nov 21 '20 at 19:26

4 Answers4

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
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
0

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()
Alex Anadon
  • 77
  • 3
  • 11
0

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 = ['foo@here.com', 'bar@there.com', 'dumb@a.com']
msg = MIMEMultipart('related')
msg['Subject'] = " You Tell me"
msg['From'] = "noReply@every.com"
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("whyme@here.com", email_to, msg.as_string())
s.quit()
-1

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

tripleee
  • 175,061
  • 34
  • 275
  • 318
Shaggy89
  • 689
  • 1
  • 5
  • 18