471,601 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

sending mailing list with smtplib

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()
fp.close()

# message.txt is the plaine text message to send
textfile='message.txt'
subject='[xsbar] alive and kicking'
me********@xsbar.com'

# rom and example in the doc
import smtplib

from email.MIMEText import MIMEText

fp = open(textfile, 'rb')
msg = MIMEText(fp.read())
fp.close()

# ... but the actual message sending in a loop to send one email each
(didn't work)
for line in list:
you=line[:-1]
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

but it copied all the recipients in the header (To:) which I don't
understand?

Can someone help me understand what I did wrong?

Thanks,

EuGeNe

Aug 14 '06 #1
7 2246
At Monday 14/8/2006 10:22, 3KWA wrote:
># ... but the actual message sending in a loop to send one email each
(didn't work)

Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 14 '06 #2

Gabriel Genellina wrote:
>
Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html
Ooops, didn't work explained (the worst is I bought and read ESR's book
:()

list.txt=
email1
email2
email3
....
emailn

email1 received from:xsbar.com to:email1 subject: [xsbar] alive and
kicking ... message

from email2 onwards it seems what happened is that the recipient list
kept growing,
email 2 received from:eu****@xsbar.com to:email1 to:email2 subject: ...
email3 received from:eu****@xsbar.com to:email1 to:email2 to:email3
subject:...
....
emailn received from:eu****@xsbar.com to:email1 to:email2 to:email3 ...
to:emailn subject:...

Many didn't receive the email because the header grew too big (I
received 1257 failure notice ~50% of them for header too big or
inappropriate recipient list).

Apologize for the inaccuracy of my first post

Aug 15 '06 #3
3KWA wrote:
Gabriel Genellina wrote:
>>Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html


Ooops, didn't work explained (the worst is I bought and read ESR's book
:()

list.txt=
email1
email2
email3
....
emailn

email1 received from:xsbar.com to:email1 subject: [xsbar] alive and
kicking ... message

from email2 onwards it seems what happened is that the recipient list
kept growing,
email 2 received from:eu****@xsbar.com to:email1 to:email2 subject: ...
email3 received from:eu****@xsbar.com to:email1 to:email2 to:email3
subject:...
....
emailn received from:eu****@xsbar.com to:email1 to:email2 to:email3 ...
to:emailn subject:...

Many didn't receive the email because the header grew too big (I
received 1257 failure notice ~50% of them for header too big or
inappropriate recipient list).

Apologize for the inaccuracy of my first post
OK, now the problem is that you clearly aren't running the code you
posted, you are running *somethinglike* the code you posted, but that
has (an) error(s) the code you posted didn't.

So let's see the version with the "print" statements in it, not the
sanitised version you didn't copy-and-paste fro your python source :-)

For the record, it's clear that youa ren't resetting the senders to an
empty list each time but growing it as you go round the loop. If this
helps you find your error, at least confirm that you did indeed find the
problem.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Aug 15 '06 #4
Steve Holden wrote:
OK, now the problem is that you clearly aren't running the code you
posted, you are running *somethinglike* the code you posted, but that
has (an) error(s) the code you posted didn't.
All I did was changed the comments to make them more relevant to my
problem but here it is (not sanitized :P)

fp=open('list.txt','r')
list=fp.readlines()
fp.close()

textfile='message.txt'
subject='[xsbar] alive and kicking'
me********@xsbar.com'

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.MIMEText import MIMEText

# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
for line in list:
you=line[:-1]
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()
print you,
So let's see the version with the "print" statements in it, not the
sanitised version you didn't copy-and-paste fro your python source :-)
Yes there was a print statement at the end so I could follow the
process
For the record, it's clear that youa ren't resetting the senders to an
empty list each time but growing it as you go round the loop. If this
helps you find your error, at least confirm that you did indeed find the
problem.
I must be punching way out of my league here but it is not clear to me.
Why do I need to reset anything when all I seem to be doing is straight
assignation:
1) msg['To']=you
2) [you] in the sendmail call

In order to try to figure it out I did:

list=['a','b','c']

from email.MIMEText import MIMEText

msg=MIMEText('message')

for l in list:
msg['Subject']='subject'
msg['From']='me'
msg['To']=l
print msg.as_string()

Which outputs:

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a

message
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a
Subject: subject
From: me
To: b

message
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a
Subject: subject
From: me
To: b
Subject: subject
From: me
To: c

message

I realized too late that it is what was happenig but I am afraid I
don't understand why it is happening?

What would be the best way to go about it then? Instantiate a new msg
in the loop?

I guess I must read the doc more carefully, thanks for your time (if
you can spare some more I would be grateful).

Regards,

EuGeNe

Aug 15 '06 #5
On 15 Aug 2006 13:41:53 -0700, 3KWA <eu****@boardkulture.comwrote:
What would be the best way to go about it then? Instantiate a new msg
in the loop?

I guess I must read the doc more carefully, thanks for your time (if
you can spare some more I would be grateful).
You can reuse your message object, but you need to delete the old
header before setting a new one:

<http://docs.python.org/lib/module-email.Message.html#l2h-3843>
--
filip salomonsson
Aug 15 '06 #6
Filip Salomonsson wrote:
>
You can reuse your message object, but you need to delete the old
header before setting a new one:

<http://docs.python.org/lib/module-email.Message.html#l2h-3843>
Thanks!

I am only an occasional Python programmer (love it though). I guess it
is the first time I faced a case where what I thought the code would do
from reading it didn't match what the code is effectively doing i.e.

msg['To']=email1 appending to the header instead of setting the To
field to email. The way my brain works if I wanted to have several
recipient I would have done something like msg['To'].append(email2).

Just for education purposes (mine I guess :P) what was the idea behind
that design decision?

Thanks again for your assistance,

EuGeNe

Aug 16 '06 #7

3KWA wrote:
Just for education purposes (mine I guess :P) what was the idea behind
that design decision?
>From the doc (self education :P)
The following methods implement a mapping-like interface for accessing
the message's RFC 2822 headers. Note that there are some semantic
differences between these methods and a normal mapping (i.e.
dictionary) interface. For example, in a dictionary there are no
duplicate keys, but here there may be duplicate message headers. Also,
in dictionaries there is no guaranteed order to the keys returned by
keys(), but in a Message object, headers are always returned in the
order they appeared in the original message, or were added to the
message later. Any header deleted and then re-added are always appended
to the end of the header list.

These semantic differences are intentional and are biased toward
maximal convenience.

Aug 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stephen Boulet | last post: by
3 posts views Thread by praba kar | last post: by
reply views Thread by praba kar | last post: by
9 posts views Thread by Leo Breebaart | last post: by
5 posts views Thread by morphex | last post: by
5 posts views Thread by Kun | last post: by
14 posts views Thread by sridhar | last post: by
2 posts views Thread by ornto | last post: by
5 posts views Thread by sui | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.