sending mailing list with smtplib

3

3KWA

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

# 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
 
3

3KWA

Gabriel said:

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:[email protected] to:email1 to:email2 subject: ...
email3 received from:[email protected] to:email1 to:email2 to:email3
subject:...
....
emailn received from:[email protected] 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
 
S

Steve Holden

3KWA said:
Gabriel said:


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:[email protected] to:email1 to:email2 subject: ...
email3 received from:[email protected] to:email1 to:email2 to:email3
subject:...
....
emailn received from:[email protected] 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
 
3

3KWA

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

# 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
 
3

3KWA

Filip said:
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
 
3

3KWA

3KWA said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top