simple example of mimelib? and embedding (not attaching) images in email sent with python.

Richard Jones richard at bizarsoftware.com.au
Tue Aug 14 20:45:53 EDT 2001


On Wednesday 15 August 2001 09:43, Jeff Shannon wrote:
> It is true that the current Python std library, as I've seen it (I work
> with 2.0), doesn't have much high-level support for MIME and email.  I
> haven't looked at the forthcoming Mimelib, yet, however, so I can't answer
> as to how well that package addresses the current weaknesses.  I have,
> however, cobbled together a usable "outbox" utility that allows me to
> easily send email, with MIME attachments, based on text-file contents. 
> It's not all *that* difficult, really.

I've made extensive use of python's mime libraries in several projects. 
There's a bit of an initial learning jump, but once you're there, I've found 
it to be quite good for both reading and writing MIME data. My major gripes 
are:

  1. the documentation for mimetools needs a good, simple example of creating 
a message, like:

    buffer = StringIO.StringIO()
    m = MimeWriter.MimeWriter(buffer)
    m.addheader('Subject', "This is a MIME example")
    m.startmultipartbody('mixed')
    for name, data, type, encoding in data_list:
        sub = m.nextpart()
        sub.addheader('Content-Description', name)
        if encoding == 'base64':
            data = binascii.b2a_base64(data)
            sub.addheader('Content-Transfer-Encoding', 'base64')
        f = sub.startbody(type, [('name', name)])
        f.write(data)
    message = buffer.getvalue()
    smtp = smtplib.SMTP('mail.foo.bar')
    smtp.sendmail('sender at foo.bar', ['recipient at bar.foo'], message)

where data_list is a list of tuples containing:

   . description of the data
   . data to send
   . mime type (eg 'text/plain', 'image/png')
   . encoding of part (eg '', 'base64')

  2. mimetools.Message doesn't currently have a getPart() method to simply 
extract a part from the message object. I've got a simple patch submitted to 
fix this though.

  3. MimeWriter doesn't offer the ability to post to HTTP - it isn't capable 
of producing the Content-Length header. That's going to be hard to fix 
though, in the current file-oriented framework.


       Richard




More information about the Python-list mailing list