# 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: file.write(blurb) def close(self): "End of message" self.mime.lastpart() 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) else: 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 else: # binary data (image, audio, application, ...) encoding = "base64" encoder = base64.encode # # write part headers if encoding: part.addheader("Content-Transfer-Encoding", encoding) part.startbody(mimetype) # # write part body if encoder: encoder(file, self.file) elif data: self.file.write(data) else: while 1: data = infile.read(16384) if not data: break outfile.write(data) # # try it out BLURB = "if you can read this, your mailer is not MIME-aware\n" mime = Writer(sys.stdout, BLURB) # add a text message mime.write("""\ 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") mime.close()