Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email multiple contacts in Python

Tags:

python

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

like image 402
chrisg Avatar asked Jan 05 '10 14:01

chrisg


3 Answers

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())
like image 79
John Feminella Avatar answered Sep 29 '22 08:09

John Feminella


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())
like image 20
YOU Avatar answered Sep 29 '22 09:09

YOU


you = ('one@address', 'another@address')
s.sendmail(me, you, msg.as_string())
like image 20
alvherre Avatar answered Sep 29 '22 08:09

alvherre