May 2001
Intermediate to advanced
304 pages
6h 12m
English
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.
</F>"""
FILE = "samples/sample.jpg"
file = sys.stdout
#
# create a mime multipart writer instance
mime = MimeWriter.MimeWriter(file)
mime.addheader("Mime-Version", "1.0")
mime.startmultipartbody("mixed")
# add a text message
part = mime.nextpart()
part.addheader("Content-Transfer-Encoding", "quoted-printable")
part.startbody("text/plain")
quopri.encode(StringIO.StringIO(TEXT), file, 0)
# add an image
part = mime.nextpart()
part.addheader("Content-Transfer-Encoding", "base64")
part.startbody("image/jpeg")
base64.encode(open(FILE, "rb"), file)
mime.lastpart()The output looks something like:
Content-Type: multipart/mixed; boundary='host.1.-852461.936831373.130.24813' --host.1.-852461.936831373.130.24813 Content-Type: text/plain Context-Transfer-Encoding: quoted-printable here comes the image you asked for. hope it's what you expected. </F> --host.1.-852461.936831373.130.24813 Content-Type: image/jpeg Context-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIy ... 1e5vLrSYbJnEVpEgjCLx5mPU0qsVK0UaxjdNlS+1U6pfzTR8IzEhj2HrVG6m8m18xc8cIKSC ...
Read now
Unlock full access