A better way to send mail?

Steve Holden sholden at holdenweb.com
Wed Oct 31 01:00:35 CET 2001

"David Brady" <daves_spam_dodging_account at yahoo.com> wrote in ...
> Hello all,
> Python promotes marital harmony.
[ ... ]
> 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,


Don't know whether this will improve your marital harmony, but it shows how
to send graphic attachments using the MimeWriter library!

import MimeWriter, base64
# Writes a multipart mail message: text plus associated graphic
mfile = "multimsg.eml"
f = open(mfile, "w")
# Create a MimeWriter
mail = MimeWriter.MimeWriter(f)
mail.addheader("From", "Steve Holden <sholden at holdenweb.com")
mail.addheader("To", """Gentle Reader <bookuser at holdenweb.com>,
     Steve Holden <sholden at holdenweb.com>""")
mail.addheader("Subject", "The Python You Wanted")
mail.addheader("Received", """from thinker [] by
    (SMTPD32-6.04) id A244C78500BA; Fri, 09 Mar 2001 07:33:38 -0500""")
# Mail will be multi-part: First part explains format
part1 = mail.startmultipartbody ("mixed")
part1.write("This is a MIME-encoded message, with attachments. "
"If you are seeing this message your mail program probably cannot "
"show you the attachments. Please try another program, or read 'Web "
"Programming in Python' to see the attached picture."
Sorry ...
Steve Holden
# Second part is intended to be read
part2 = mail.nextpart()
f = part2.startbody("text/plain")
f.write("Here we have a multipart message. This "
"means that the message body must be processed "
"as MIME-encoded content where possible [which "
"it clearly is in Outlook Express]."

 Your Humble Author
# Third part is a graphic, which we encode in base64
part3 = mail.nextpart()
part3.addheader("Content-Transfer-Encoding", "base64")
f = part3.startbody("image/gif", [["Name", "python.gif"]])
b64 = base64.encodestring(open("pythonwin.gif", "rb").read())
# Never forget to call lastpart!


More information about the Python-list mailing list