[Tutor] smptlib question

Albert-jan Roskam fomcl at yahoo.com
Mon Jun 8 23:15:27 CEST 2009


(sorry for posting this again, but something might have gone wrong)

Hi,

I made a very simple program to send everybody I know a change of address I am parsing the .msg files with another function, which returns a set called 'recipients'.

The code below works, but it displays all recipients in the 'to' field. That is, in the Nth iteration, N recipients are shown. So the first mail has one recipient in the 'to' field, the second mail has the first and the second recipient, and so forth. I only want one recipient to be shown in the 'to' field. It's ugly and since I have a lot of email addresses to parse (4 years worth of emails!), it would become a very long list.

Pointers, anyone?

Thanks!
Albert-Jan
Python 2.5, Windows XP

import smtplib, email.utils
from email.mime.text import MIMEText

msg = MIMEText("""Dear reader:\n\nblaaaaah blaah blaah.\nSincerely,\nThe dude""")

sender = 'ren at stimpy.com'
recipients = ['happyhappy at joy.com', 'you at eediot.com]
for no, recipient in enumerate(recipients):
    print "--> Sending message to: %s (%s of %s)" % (recipient, no+1, len(recipients))
    msg['To'] = email.utils.formataddr((recipient, recipient))
    print msg['To']
    msg['From'] = email.utils.formataddr(('Cool dude', sender))
    msg['Subject'] = 'Change of email address'

    server = smtplib.SMTP("mysmtpserver.isp.com")
    server.set_debuglevel(True)
    try:
        server.sendmail(sender, [recipient], msg.as_string())
    finally:
        server.quit()


      


More information about the Tutor mailing list