6

I was wondering. Is there any way to add multiple receivers in Python on its default SMTPlib?

Like (subject and content set already, smtp server gmail.):

python sendmail.py receiver1@gmail.com receiver2@gmail.com receiver3@gmail.com ...

Thanks

brc.sw
  • 91
  • 1
  • 2
  • 7
  • possible duplicate of [How to send email to multiple recipients using python smtplib?](http://stackoverflow.com/questions/8856117/how-to-send-email-to-multiple-recipients-using-python-smtplib) – AliciaBytes May 20 '15 at 09:04

2 Answers2

8

Tested before posting!

import smtplib
from email.mime.text import MIMEText

s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(msg.get('From'), recipients, msg.as_string())
sorin
  • 161,544
  • 178
  • 535
  • 806
  • 1
    This fails when the total characters exceeds 255 in recipients field. The last email address gets trimmed off in the email sent. But the mail will be sent. – Dr. Essen Jun 19 '20 at 12:57
  • You need to work with send_message instead of sendmail. – César HM Dec 01 '22 at 11:46
3

From the docs:

Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.

Rob Wouters
  • 15,797
  • 3
  • 42
  • 36