sending mailing list with smtplib

3KWA eugene at boardkulture.com
Tue Aug 15 16:41:53 EDT 2006


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='eugene at 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




More information about the Python-list mailing list