473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sending emails to a list of recipients

Kun
i have the following code:

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

from email.MIMEText import MIMEText
fp = open('confirmat ion.txt', 'rb')
msg = MIMEText(fp.rea d())

From = 'x***@xxxx.xxxx .edu'

msg['Subject'] = 'Purchase Confirmation'
msg ['From'] = From
msg['To'] = emails

s = smtplib.SMTP('x xxx.xxx.xxx.edu ')
s.login('xxxxx' ,'xxxx')
s.sendmail(msg['From'], msg['To'], msg.as_string() )
s.close()
----------------------------------

it works if msg['To'] = 'e****@email.co m'

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

emails is in the following format: ['n***@gmail.com ', 'n***@gmail.com ',
'x**@xxx.xxxx.e du']
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.
Mar 25 '06 #1
5 23778
Kun
Kun wrote:
i have the following code:

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

from email.MIMEText import MIMEText
fp = open('confirmat ion.txt', 'rb')
msg = MIMEText(fp.rea d())

From = 'x***@xxxx.xxxx .edu'

msg['Subject'] = 'Purchase Confirmation'
msg ['From'] = From
msg['To'] = emails

s = smtplib.SMTP('x xxx.xxx.xxx.edu ')
s.login('xxxxx' ,'xxxx')
s.sendmail(msg['From'], msg['To'], msg.as_string() )
s.close()
----------------------------------

it works if msg['To'] = 'e****@email.co m'

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

emails is in the following format: ['n***@gmail.com ', 'n***@gmail.com ',
'x**@xxx.xxxx.e du']
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.p y", line 41, in -toplevel-
s.sendmail(msg['From'], msg['To'], msg.as_string() )
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Message.py",
line 129, in as_string
g.flatten(self, unixfrom=unixfr om)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Generator.py",
line 82, in flatten
self._write(msg )
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Generator.py",
line 120, in _write
self._write_hea ders(msg)
File
"/Library/Frameworks/Python.framewor k/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.framewor k/Versions/2.4/lib/python2.4/email/Header.py",
line 395, in encode
return self._encode_ch unks(newchunks, maxlinelen)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Header.py",
line 355, in _encode_chunks
_max_append(chu nks, s, maxlinelen, extra)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/quopriMIME.py",
line 79, in _max_append
L.append(s.lstr ip())
AttributeError: 'list' object has no attribute 'lstrip'
Mar 25 '06 #2
Kun wrote:
i have the following code:

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

from email.MIMEText import MIMEText
fp = open('confirmat ion.txt', 'rb')
msg = MIMEText(fp.rea d())

From = 'x***@xxxx.xxxx .edu'

msg['Subject'] = 'Purchase Confirmation'
msg ['From'] = From
msg['To'] = emails

s = smtplib.SMTP('x xxx.xxx.xxx.edu ')
s.login('xxxxx' ,'xxxx')
s.sendmail(msg['From'], msg['To'], msg.as_string() )
s.close()
----------------------------------

it works if msg['To'] = 'e****@email.co m'

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

emails is in the following format: ['n***@gmail.com ', 'n***@gmail.com ',
'x**@xxx.xxxx.e du']
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

Mar 26 '06 #3
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('confirmat ion.txt', 'rb')
msg = MIMEText(fp.rea d())

From = 'x***@xxxx.xxxx .edu'

msg['Subject'] = 'Purchase Confirmation'
msg ['From'] = From
msg['To'] = emails

s = smtplib.SMTP('x xxx.xxx.xxx.edu ')
s.login('xxxxx' ,'xxxx')
s.sendmail(msg['From'], msg['To'], msg.as_string() )
s.close()
----------------------------------

it works if msg['To'] = 'e****@email.co m'

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

emails is in the following format: ['n***@gmail.com ',
'n***@gmail.com ', 'x**@xxx.xxxx.e du']
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.p y", line 41, in -toplevel-
s.sendmail(msg['From'], msg['To'], msg.as_string() )
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Message.py",
line 129, in as_string
g.flatten(self, unixfrom=unixfr om)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Generator.py",
line 82, in flatten
self._write(msg )
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Generator.py",
line 120, in _write
self._write_hea ders(msg)
File
"/Library/Frameworks/Python.framewor k/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.framewor k/Versions/2.4/lib/python2.4/email/Header.py",
line 395, in encode
return self._encode_ch unks(newchunks, maxlinelen)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Header.py",
line 355, in _encode_chunks
_max_append(chu nks, s, maxlinelen, extra)
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/quopriMIME.py",
line 79, in _max_append
L.append(s.lstr ip())
AttributeError: 'list' object has no attribute 'lstrip'

Mar 26 '06 #4
"Gerard Flanagan" <gr********@yah oo.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('x xxx.xxx.xxx.edu ')
s.login('xxxxx' ,'xxxx')
s.sendmail(msg['From'], msg['To'], msg.as_string() )
s.close()
----------------------------------

it works if msg['To'] = 'e****@email.co m'

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

emails is in the following format: ['n***@gmail.com ', 'n***@gmail.com ',
'x**@xxx.xxxx.e du']

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, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 27 '06 #5
Kun <ne*******@gmai l.com> wrote:

Kun wrote:
i have the following code:

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

from email.MIMEText import MIMEText
fp = open('confirmat ion.txt', 'rb')
msg = MIMEText(fp.rea d())

From = 'x***@xxxx.xxxx .edu'

msg['Subject'] = 'Purchase Confirmation'
msg ['From'] = From
msg['To'] = emails

s = smtplib.SMTP('x xxx.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.p y", line 41, in -toplevel-
s.sendmail(msg['From'], msg['To'], msg.as_string() )
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/Message.py",
line 129, in as_string
g.flatten(self, unixfrom=unixfr om)
...
File
"/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/email/quopriMIME.py",
line 79, in _max_append
L.append(s.lstr ip())
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() )
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3352
by: jason | last post by:
Database: Access 2000 Host: Maximum ASP I am trying to work out the best way to send bulk email below the 1000 mark (about 800). I understand that I could either use ASPEmail or CDO although I believe that ASPEmail may be superior in that it offers SENDQUEUE command which will allow me to loop through my customer database and insert them...
2
1902
by: Joe | last post by:
Hi, I am sending an email from an asp page. Besides sending an email to sender, I am sending myself a BCC also. Out of 100 emails sent, about 5 recipients received a blank email (no text in subject and body). The BCC of all these emails that I sent to myself were fine. When I send email manually to these 5 recipients they receive it...
1
4139
by: Devonish | last post by:
I am composing an email with Access VB and then sending it from within Access. Everything works correctly (the email actually goes!) but Outlook ask some irritating questions that the user is required to answer. A summary of the relevant code is: Dim mailObj as Outlook.MailItem
4
3121
by: splicemix | last post by:
Hi all, I have recently set up a Drupal website. I am a beginner. My shared host server does not allow nobody@localhost to send emails, and prevents access to php.ini, so I spent some time getting a SMTP script running. While the Drupal feedback module and the new account creation works fine in terms of sending of mails, I am having some...
5
2964
by: horsetransport | last post by:
Hello, Below is what I "Know how to do" but it doesn't accomplish what I want I have table called sndmail fields that matter useremail and mailsent
0
1196
by: Jim Devenish | last post by:
I wish to send a copy of an email to a selected number of recipients. I can do this when the email contains only text but have a problem when the email contains a picture. I create a new email with a picture inserted into its body and save it in the Drafts folder. I then, from within Access, send a copy of this to a number of people. ...
4
5600
by: Michelle | last post by:
Hi Is it possible/anyone know how to send a document (.pdf) to multiple recipients using POP where the email is not just a matter of throwing 1000 email addresses in the BCC list, but each email is addressed personally.. ie: dear Harry... I need to generate a subset of people via a query who have authorised to receive information via...
7
2350
by: 3KWA | last post by:
Hi all, I tried to send a small mailing list using python today (2036 emails). For that purpose I looked in the doc for some code examples (I had never done it before). And I ended up writing and running this little script: # list.txt is a text file container an email per line fp=open('list.txt','r') list=fp.readlines()
0
1766
by: damimkader | last post by:
Hi, I'm trying to send emails using a Macro based on an Excel Sheet and the Email Client I'm using is Lotus Notes. OS used - Windows Xp. Language - VB Below is the Code I'm using for doing the same. Dim Maildb As Object
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.