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
chrisg
41.8k39 gold badges90 silver badges109 bronze badges
-
1have you tried '[email protected]; [email protected]' ?Karmic Coder– Karmic Coder2010年01月05日 14:36:26 +00:00Commented Jan 5, 2010 at 14:36
4 Answers 4
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
John Feminella
314k49 gold badges348 silver badges362 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
YOU
125k34 gold badges192 silver badges222 bronze badges
Comments
you = ('one@address', 'another@address')
s.sendmail(me, you, msg.as_string())
Dennis Williamson
364k95 gold badges387 silver badges446 bronze badges
Comments
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.inifile we add the code from below:
[mail]
SENDER = [email protected]
RECEIVERS = [email protected],[email protected]
- Create another
test.pyfile 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
Milovan Tomašević
8,9912 gold badges58 silver badges49 bronze badges
Comments
lang-py