Hi David and Tino, Thanks! I will try this tonight when I'm back home from the office. It's interesting to see how you split up the code into several little functions. Maybe it's typical for a newbie (like myself) to have the tendency to put everything into one function, instead of letting each function have its own purpose.
Cheers!! Albert-Jan --- On Tue, 6/9/09, David <da...@abbottdavid.com> wrote: > From: David <da...@abbottdavid.com> > Subject: Re: [Tutor] smptlib question > To: "Albert-jan Roskam" <fo...@yahoo.com> > Cc: "*tutor python" <tutor@python.org> > Date: Tuesday, June 9, 2009, 2:13 AM > Albert-jan Roskam wrote: > > (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 = 'r...@stimpy.com' > > recipients = ['happyha...@joy.com', > 'y...@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() > > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > Hi Albert, > I got this to work; > > #!/usr/bin/python > > import smtplib, email.utils > from email.mime.text import MIMEText > from email.MIMEMultipart import MIMEMultipart > from email.Utils import COMMASPACE, formatdate > > def recipients(): > recipients = ['happyha...@joy.com', > 'y...@eediot.com] > for no, recipient in enumerate(recipients): > print "--> Sending message > to: %s (%s of %s)" % (recipient, no+1, len(recipients)) > message(recipient) > > def message(recipient): > body = "Dear reader:\n\nblaaaaah blaah > blaah.\nSincerely,\nThe dude" > send_mail([recipient], 'Change of email > address', body) > > > def send_mail(send_to, subject, text, > server="mail.stimpy.com"): > send_from = "r...@stimpy.com" > msg = MIMEMultipart() > msg['To'] = COMMASPACE.join(send_to) > msg['Date'] = formatdate(localtime=True) > msg['From'] = send_from > msg['Subject'] = subject > msg.attach( MIMEText(text) ) > smtp = smtplib.SMTP(server) > smtp.sendmail(send_from, send_to, > msg.as_string()) > smtp.close() > > recipients() > > -- Powered by Gentoo GNU/Linux > http://linuxcrazy.com > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor