sending emails to a list of recipients [update]

Larry Bates larry.bates at websafe.com
Sat Mar 25 20:48:23 EST 2006


smtplib docs http://python.active-venture.com/lib/SMTP-example.html
say that the to should be a list of addresses (your emails);

s.sendmail(msg['From'], emails, msg.as_string())

-Larry Bates


Kun wrote:
> Kun wrote:
>> i have the following code:
>>
>> ----------------------------------
>> import smtplib
>>
>> from email.MIMEText import MIMEText
>> fp = open('confirmation.txt', 'rb')
>> msg = MIMEText(fp.read())
>>
>>  From = 'xxxx at xxxx.xxxx.edu'
>>
>> 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.
> 
> this is my error msg of leaving the code in its current state... (brave
> yourself)
> 
> Traceback (most recent call last):
>   File "/Tutorial/IMAP/scannermailer.py", line 41, in -toplevel-
>     s.sendmail(msg['From'], msg['To'], msg.as_string())
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Message.py",
> line 129, in as_string
>     g.flatten(self, unixfrom=unixfrom)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Generator.py",
> line 82, in flatten
>     self._write(msg)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Generator.py",
> line 120, in _write
>     self._write_headers(msg)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Generator.py",
> line 166, in _write_headers
>     header_name=h, continuation_ws='\t').encode()
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Header.py",
> line 395, in encode
>     return self._encode_chunks(newchunks, maxlinelen)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/Header.py",
> line 355, in _encode_chunks
>     _max_append(chunks, s, maxlinelen, extra)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/email/quopriMIME.py",
> line 79, in _max_append
>     L.append(s.lstrip())
> AttributeError: 'list' object has no attribute 'lstrip'



More information about the Python-list mailing list