[Tutor] smtplib question

Wayne srilyk at gmail.com
Tue Jun 9 14:13:47 CEST 2009


On Mon, Jun 8, 2009 at 1:42 PM, Albert-jan Roskam <fomcl at yahoo.com> wrote:

>
> 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.


So your code would, in this case, print out

"happyhappy at joy.com" <happyhappy at joy.com>
"you at eediot.com" <you at eediot.com>

or does it print out

"happyhappy at joy.com" <happyhappy at joy.com>
"happyhappy at joy.com" <happyhappy at joy.com>
"you at eediot.com" <you at eediot.com>

If it's the former, that's what you have told it to do.
If it's the latter, I'm not sure what the problem is, unless it has
something to do with your debug level.

HTH,
Wayne


>
>
> Pointers, anyone?
>
> Thanks!
> Albert-Jan
>
> 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()
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090609/21916942/attachment-0001.htm>


More information about the Tutor mailing list