11

I am trying to send an email to multiple addresses. The code below shows what I'm attempting to achieve. When I add two addresses the email does not send to the second address. The code is:

 me = '[email protected]'
 you = '[email protected], [email protected]'
 msg['Subject'] = "Some Subject"
 msg['From'] = me
 msg['To'] = you
 # Send the message via our own SMTP server
 s = smtplib.SMTP('a.a.a.a')
 s.sendmail(me, [you], msg.as_string())
 s.quit()

I have tried:

you = ['[email protected]', '[email protected]']

and

you = '[email protected]', '[email protected]'

Thanks

asked Jan 5, 2010 at 14:34
1

4 Answers 4

14

You want this:

from email.utils import COMMASPACE
...
you = ["[email protected]", "[email protected]"]
...
msg['To'] = COMMASPACE.join(you)
...
s.sendmail(me, you, msg.as_string())
answered Jan 5, 2010 at 14:38
Sign up to request clarification or add additional context in comments.

Comments

12

Try

s.sendmail(me, you.split(","), msg.as_string())

If you do you = ['[email protected]', '[email protected]']

Try

msg['To'] = ",".join(you)
...
s.sendmail(me, you, msg.as_string())
answered Jan 5, 2010 at 14:38

Comments

2
you = ('one@address', 'another@address')
s.sendmail(me, you, msg.as_string())
Dennis Williamson
364k95 gold badges387 silver badges446 bronze badges
answered Jan 5, 2010 at 14:45

Comments

0
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.foobar.com')
msg = MIMEText("""body""")
sender = '[email protected]'
recipients = ['[email protected]', '[email protected]']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())

In the example to read emails from the configuration file, for example configparser - pip install configparser.

  • In the created config.ini file we add the code from below:
[mail]
SENDER = [email protected]
RECEIVERS = [email protected],[email protected]
  • Create another test.py file located in the same directory
import configparser
import smtplib
from smtplib import SMTPException
config = configparser.RawConfigParser() 
config.read('config.ini')
# template
msg = """From: From Person <{0}>
To:{1}
MIME-Version: 1.0
Content-type: text/html
Subject: Hello {2}
Check your results :)
{3}
"""
# universal function that would send mail when called
def send_mail(sender, receivers, subject, message):
 try:
 session = smtplib.SMTP(host = "ip", port = 25)
 session.ehlo()
 session.starttls()
 session.ehlo()
 session.sendmail(sender, receivers, msg.format(sender, receivers, subject, message)) 
 session.quit()
 print("Your mail has been sent successfuly! Thank you for your feedback!")
 except SMTPException:
 print("Error: Unable to send email.")
# html context for email
your_subject = "SMTP HTML e-mail test"
your_message = """This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
# The call to the universal e-mail function to which we pass the parameters in this example are given above
def setup_and_send_mail():
 send_mail(config.get('mail', 'SENDER'), (config.get('mail', 'RECEIVERS')), your_subject, your_message) 
if __name__ == "__main__":
 setup_and_send_mail()
answered Dec 10, 2020 at 12:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.