smtp module - specifying multiple email recipients?

A

Alex Hunsley

I'm using the smtp module to send emails from python.
I would like to specify several recipients of the email like follows:

msg = MIMEText(time.ctime("Body text of email")

me = '(e-mail address removed)'
you = '(e-mail address removed),[email protected]'
msg['Subject'] = '*** Carwatch alert'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
# s = smtplib.SMTP('smtp.ednet.co.uk')
s = smtplib.SMTP('192.168.1.105')
# s.connect()
s.sendmail(me, [you], msg.as_string())

... when I execute this code, only (e-mail address removed) gets the email.
Is there a way to specifiy multiple addresses in the To field?

thanks
alex
 
R

Richie Hindle

[Alex]
I would like to specify several recipients of the email like follows:

you = '(e-mail address removed),[email protected]'
...
msg['To'] = you
...
s.sendmail(me, [you], msg.as_string())

.. when I execute this code, only (e-mail address removed) gets the email.

sendmail() takes a list of recipients, not a list with a single
comma-separated string in it. You need to do this:

you = ['(e-mail address removed)', '(e-mail address removed)']
...
msg['To'] = ', '.join(you)
...
s.sendmail(me, you, msg.as_string())
 
P

Paul Wright

I'm using the smtp module to send emails from python.
me = '(e-mail address removed)'
you = '(e-mail address removed),[email protected]'
s.sendmail(me, [you], msg.as_string())

.. when I execute this code, only (e-mail address removed) gets the email.
Is there a way to specifiy multiple addresses in the To field?

The second argument to sendmail is a list, not a comma separated string.
Currently, you've specified a single address containing a comma. Your
MTA has charitably decided to deliver it to the first address, although
I expect other MTAs would just reject it. If you've got comma separated
addresses in a string, make a list using s.split(",").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top