The MimeWriter Module

The MimeWriter module (shown in Example 6-4) can be used to write "multipart" messages, as defined by the MIME mail standard.

Example 6-4. Using the MimeWriter Module

File: mimewriter-example-1.py

import MimeWriter

# data encoders
import quopri
import base64
import StringIO

import sys

TEXT = """
here comes the image you asked for. hope
it's what you expected.


FILE = "samples/sample.jpg"

file = sys.stdout

# create a mime multipart writer instance

mime = MimeWriter.MimeWriter(file)
mime.addheader("Mime-Version", "1.0")


# add a text message

part = mime.nextpart()
part.addheader("Content-Transfer-Encoding", "quoted-printable")

quopri.encode(StringIO.StringIO(TEXT), file, 0)

# add an image

part = mime.nextpart()
part.addheader("Content-Transfer-Encoding", "base64")

base64.encode(open(FILE, "rb"), file)


The output looks something like:

Content-Type: multipart/mixed;

Content-Type: text/plain
Context-Transfer-Encoding: quoted-printable

here comes the image you asked for. hope
it's what you expected.

Content-Type: image/jpeg
Context-Transfer-Encoding: base64



Example 6-5, which is a bit larger, uses a helper class that stores each subpart in the most suitable way.

Example 6-5. A Helper Class for the MimeWriter Module

File: mimewriter-example-2.py

import MimeWriter
import string, StringIO, sys
import re, quopri, base64

# check if string contains non-ascii characters
must_quote = re.compile("[177-377]").search

# encoders

def encode_quoted_printable(infile, outfile):
 quopri.encode(infile, outfile, 0)

class Writer:

 def _ _init_ _(self, file=None, blurb=None):
 if file is None:
 file = sys.stdout
 self.file = file
 self.mime = MimeWriter.MimeWriter(file)
 self.mime.addheader("Mime-Version", "1.0")

 file = self.mime.startmultipartbody("mixed")
 if blurb:

 def close(self):
 "End of message"
 self.mime = self.file = None

 def write(self, data, mimetype="text/plain"):
 "Write data from string or file to message"

 # data is either an opened file or a string
 if type(data) is type(""):
 file = StringIO.StringIO(data)
 file = data
 data = None

 part = self.mime.nextpart()

 typ, subtyp = string.split(mimetype, "/", 1)

 if typ == "text":

 # text data
 encoding = "quoted-printable"
 encoder = lambda i, o: quopri.encode(i, o, 0)

 if data and not must_quote(data):
 # copy, don't encode
 encoding = "7bit"
 encoder = None


 # binary data (image, audio, application, ...)
 encoding = "base64"
 encoder = base64.encode

 # write part headers

 if encoding:
 part.addheader("Content-Transfer-Encoding", encoding)


 # write part body

 if encoder:
 encoder(file, self.file)
 elif data:
 while 1:
 data = infile.read(16384)
 if not data:

# try it out

BLURB = "if you can read this, your mailer is not MIME-aware

mime = Writer(sys.stdout, BLURB)

# add a text message
here comes the image you asked for. hope
it's what you expected.
""", "text/plain")

# add an image
mime.write(open("samples/sample.jpg", "rb"), "image/jpeg")


Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

show all menu

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net