sending emails to a list of recipients

K

Kun

i have the following code:

----------------------------------
import smtplib

from email.MIMEText import MIMEText
fp = open('confirmation.txt', 'rb')
msg = MIMEText(fp.read())

From = '(e-mail address removed)'

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'] = '(e-mail address removed)'

however, i'm trying to attach a list of emails named 'emails' to msg['To']

emails is in the following format: ['(e-mail address removed)', '(e-mail address removed)',
'(e-mail address removed)']


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.
 
K

Kun

Kun said:
i have the following code:

----------------------------------
import smtplib

from email.MIMEText import MIMEText
fp = open('confirmation.txt', 'rb')
msg = MIMEText(fp.read())

From = '(e-mail address removed)'

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'] = '(e-mail address removed)'

however, i'm trying to attach a list of emails named 'emails' to msg['To']

emails is in the following format: ['(e-mail address removed)', '(e-mail address removed)',
'(e-mail address removed)']


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'
 
G

Gerard Flanagan

Kun said:
i have the following code:

----------------------------------
import smtplib

from email.MIMEText import MIMEText
fp = open('confirmation.txt', 'rb')
msg = MIMEText(fp.read())

From = '(e-mail address removed)'

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'] = '(e-mail address removed)'

however, i'm trying to attach a list of emails named 'emails' to msg['To']

emails is in the following format: ['(e-mail address removed)', '(e-mail address removed)',
'(e-mail address removed)']


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.


maybe try : msg['To'] = ', '.join( emails )

taken from:

http://docs.python.org/lib/node597.html

Gerard
 
L

Larry Bates

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 said:
i have the following code:

----------------------------------
import smtplib

from email.MIMEText import MIMEText
fp = open('confirmation.txt', 'rb')
msg = MIMEText(fp.read())

From = '(e-mail address removed)'

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'] = '(e-mail address removed)'

however, i'm trying to attach a list of emails named 'emails' to
msg['To']

emails is in the following format: ['(e-mail address removed)',
'(e-mail address removed)', '(e-mail address removed)']


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'
 
T

Tim Roberts

Gerard Flanagan said:
Kun said:
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'] = '(e-mail address removed)'

however, i'm trying to attach a list of emails named 'emails' to msg['To']

emails is in the following format: ['(e-mail address removed)', '(e-mail address removed)',
'(e-mail address removed)']

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.
 
T

Tim Roberts

Kun said:
i have the following code:

----------------------------------
import smtplib

from email.MIMEText import MIMEText
fp = open('confirmation.txt', 'rb')
msg = MIMEText(fp.read())

From = '(e-mail address removed)'

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()
----------------------------------

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/quopriMIME.py",
line 79, in _max_append
L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'

OK, I see what's going on now. The problem is that SMTP.sendmail and
email.MIMEText need two different things.

email.MIMEText sets up the "To:" header for the body of the e-mail. It is
ONLY used for displaying a result to the human being at the other end, and
like all e-mail headers, must be a single string. (Note that it does not
actually have to have anything to do with the people who actually receive
the message.)

SMTP.sendmail, on the other hand, sets up the "envelope" of the message for
the SMTP protocol. It needs a Python list of strings, each of which has a
single address.

So, what you need to do is COMBINE the two replies you received. Set
msg['To'] to a single string, but pass the raw list to sendmail:

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

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top