A better way to send mail?

Barry A. Warsaw barry at zope.com
Thu Nov 1 02:30:43 EST 2001


>>>>> "DB" == David Brady <daves_spam_dodging_account at yahoo.com> writes:

    DB> Python promotes marital harmony.

My wife will be very happy to hear this! :)

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

Probably so.  I'm hoping the email package[1] will promote MIME and
email handling harmony in the Python standard library. :)  I developed
it for Mailman 2.1, which uses it exclusive now, and others have
started using it too.  Here's an example of some (untested) code that
should be largely equivalent to your own.

One difference: it base64 encodes the image data rather than
uuencoding it.  The email package doesn't have a built-in uuencoder,
although one could be easily added if there's enough of a demand.

-------------------- snip snip --------------------
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase

text_attachment = MIMEText(boilerplate)
image_attachment = MIMEImage(img)
outer = MIMEBase('multipart', 'mixed')
outer.attach(text_attachment)
outer.attach(image_attachment)
outer['From'] = SMTPSender
outer['To'] = SMTPRecipient
outer['Subject'] = 'Here ya go, honey!'

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

I hope this makes sense!

-Barry

[1] The email package comes with Python 2.2, but there is a
distutils-based compatibility package for earlier versions.  It
requires at least Python 2.0.  See

    http://sourceforge.net/projects/mimelib/

for more details.  Documentation is at:

    http://python.sourceforge.net/devel-docs/lib/module-email.html



More information about the Python-list mailing list