sending emails to a list of recipients

Tim Roberts timr at probo.com
Mon Mar 27 10:53:55 CEST 2006


"Gerard Flanagan" <grflanagan at yahoo.co.uk> wrote:

>Kun wrote:
>
>> i have the following code:
>>
>> ----------------------------------
>> import smtplib
>>...
>> msg['Subject'] = 'Purchase Confirmation'
>> msg ['From'] = From
>> msg['To'] = emails
>>
>> s = smtplib.SMTP('xxxx.xxx.xxx.edu')
>> s.login('xxxxx','xxxx')
>> s.sendmail(msg['From'], msg['To'], msg.as_string())
>> s.close()
>> ----------------------------------
>>
>> it works if msg['To'] = 'email at email.com'
>>
>> however, i'm trying to attach a list of emails named 'emails' to msg['To']
>>
>> emails is in the following format: ['nxxx at gmail.com', 'nxxx at gmail.com',
>> 'xxx at xxx.xxxx.edu']
>>
>> anyone have an idea how i can modify this script to work with sending a
>> list? note this is a snippet of a larger code, 'emails' is as a string
>> defined earlier.

What did you try?  You should just be able to pass the list:

   s.sendmail( msg['From'], emails, msg.as_string() )
Or, if you must,
   msg['To'] = emails
   s.sendmail( msg['From'], msg['To'], msg.as_string() )

It needs to be a list or tuple of individual addresses, e-mail only, with
no "nicknames".  If you tried that, what did you see?

>maybe try :  msg['To'] = ', '.join( emails )
>
>taken from:
>
>    http://docs.python.org/lib/node597.html

No, you misread the example.  It uses that in the headers of the message.
That won't work for the second parameter of SMTP.sendmail.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list