A better way to send mail?

David Brady daves_spam_dodging_account at yahoo.com
Tue Oct 30 16:12:09 EST 2001


Hello all,

Python promotes marital harmony.

I came across an interesting problem the other day,
and used Python to solve it.  My wife always wants to
know what the weather is going to be like, and works
in a place where she can receive e-mail with image
attachments but is not allowed to access the web.  As
a newcomer to Python, it seemed like a good exercise
for me to learn how to write a script that gets the
forecast image from our local news station's website,
then e-mails it to her.  It took me about 5 hours,
most of which was spent reading up on urllib and
smtplib.

I finally ended up with a script that I'm rather proud
of.  I'm sure it's stilted and baby-talkish, but it
works.  Every day at 2:30pm my wife gets the forecast
in her e-mail.

My question is this: someone on this list just
mentioned MimeWriter as the better package to use for
authoring e-mail and attachments.  I could not figure
MimeWriter out in the hour or so I spent trying to get
this script working, and I finally got the uu module
to do what I needed so I used that instead.  But I
wonder if mime wouldn't be the better way.

Here is the code I use to uuencode the forecast image
and send it, with the error handling and other fluff
stripped out.  Can anyone tell me how I'd go about
this using MimeWriter, and more importantly, do I want
to?  On the one hand, this script works, so I can
easily call it "good enough" and forget it.  On the
other hand, I'd like to do a whole lot more with
e-mail and attachments, so I'd like to learn to do it
right.

Here is the code:

    # Note: img contains the binary image
    # downloaded with urlib.
    imgfile = StringIO.StringIO(img)
    uuimg = StringIO.StringIO()
    uu.encode( imgfile, uuimg, 'SevenDayForecast.jpg'
)

    # msg starts out with the boilerplate text I
    # send with every e-mail.  Basically it reads,
    # "here's the forecast.  This was sent with 
    # Python, e-mail me if you want it to stop."
    msg = msg + uuimg.getvalue()

    # SMTPSender, SMTPRecipient are set to my and her
    # e-mail addresses, respectively
    s = smtplib.SMTP(SMTPServer)
    ErrorAddresses = s.sendmail( SMTPSender,
SMTPRecipient, msg )
    s.close()

Anyway, I'm just wondering if the MimeWriter libs
would be better suited, and if so, how?  Sorry if this
example is too long.  I probably only needed to post
the first three lines of code, but I wanted other
newcomers like me to be able to see the context.

Thank you,

-dB

=====
David Brady
daves_spam_dodging_account at yahoo.com
I'm feeling very surreal today... or *AM* I?

__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com




More information about the Python-list mailing list