#!/usr/bin/python """Generic MIME writer. A MIME writer class to build multi-part emails. Classes: MimeWriter - the only thing here. """ from StringIO import StringIO import mimetools, mimetypes, base64, string #from MimeWriter import MimeWriter class MimeWriter: """ Generic MIME writer. Methods: __init__() addheader() flushheaders() startbody() startmultipartbody() nextpart() lastpart() A MIME writer is much more primitive than a MIME parser. It doesn't seek around on the output file, and it doesn't use large amounts of buffer space, so you have to write the parts in the order they should occur on the output file. It does buffer the headers you add, allowing you to rearrange their order. General usage is: f = w = MimeWriter(f) ...call w.addheader(key, value) 0 or more times... followed by either: f = w.startbody(content_type) ...call f.write(data) for body data... or: w.startmultipartbody(subtype) for each part: subwriter = w.nextpart() ...use the subwriter's methods to create the subpart... w.lastpart() The subwriter is another MimeWriter instance, and should be treated in the same way as the toplevel MimeWriter. This way, writing recursive body parts is easy. Warning: don't forget to call lastpart()! XXX There should be more state so calls made in the wrong order are detected. Some special cases: - startbody() just returns the file passed to the constructor; but don't use this knowledge, as it may be changed. - startmultipartbody() actually returns a file as well; this can be used to write the initial 'if you can read this your mailer is not MIME-aware' message. - If you call flushheaders(), the headers accumulated so far are written out (and forgotten); this is useful if you don't need a body part at all, e.g. for a subpart of type message/rfc822 that's (mis)used to store some header-like information. - Passing a keyword argument 'prefix=' to addheader(), start*body() affects where the header is inserted; 0 means append at the end, 1 means insert at the start; default is append for addheader(), but insert for start*body(), which use it to determine where the Content-Type header goes. """ def __init__(self, fp): self._fp = fp self._headers = [] def addheader(self, key, value, plist=[], prefix=0): for name, xvalue in plist: value = value + ';\n %s=\"%s\"' % (name, xvalue) lines = string.splitfields(value, "\n") while lines and not lines[-1]: del lines[-1] while lines and not lines[0]: del lines[0] for i in range(1, len(lines)): lines[i] = " " + string.strip(lines[i]) value = string.joinfields(lines, "\n") + "\n" line = key + ": " + value if prefix: self._headers.insert(0, line) else: self._headers.append(line) def flushheaders(self): self._fp.writelines(self._headers) self._fp.write("\n") self._headers = [] def startbody(self, ctype, plist=[], prefix=1): self.addheader("Content-Type", ctype, prefix=prefix, plist=plist) # self.flushheaders() return self._fp def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1): self._boundary = boundary or mimetools.choose_boundary() return self.startbody("multipart/" + subtype, [("boundary", self._boundary)] + plist, prefix=prefix) def nextpart(self): self._fp.write("\n--" + self._boundary + "\n") return self.__class__(self._fp) def lastpart(self): self._fp.write("\n--" + self._boundary + "--\n") # ----------------- def build_message(msgfp, attachments, subject="No subject"): """ build_message(msgfp, attachments) takes a file object and a list of attachment file names. the msgfp will contain a multi-part MIME message on return. """ # message header and body are built here msg = MimeWriter(msgfp) msg.addheader("subject", subject) msg.addheader("MIME-Version", "1.0") msg.startmultipartbody("mixed") msg.flushheaders() for part in attachments: att_type = mimetypes.guess_type(part) attachmentfp = open(part, "r") # attach it sp = msg.nextpart() if att_type[0] != "text/plain": # encode non-plaintext attachment in base64 attachment_outfp = StringIO() base64.encode(attachmentfp, attachment_outfp) f = sp.startbody(att_type[0], [("name", part)]) sp.addheader("Content-Transfer-Encoding", "base64") sp.addheader("Content-Disposition", "attachment", \ [("filename", part)]) sp.flushheaders() f.write(attachment_outfp.getvalue()) attachment_outfp.close() else: f = sp.startbody("text/plain") sp.flushheaders() for line in attachmentfp.readlines(): f.write(line) del sp attachmentfp.close() msg.lastpart() # end of build_message # --- def read_to_list(filename): """ read_to_list Reads a file containing a list of email addresses, one per line, and returns a list of the addresses. The list may have comment lines beginning with "#". """ tolist = [] try: fd = open(filename, "r") except IOError: print "WARNING: Cannot open", filename, "for reading." return [] try: for line in fd.readlines(): line = string.strip(line) if line[0] == '#': continue tolist.append(line) finally: fd.close() return tolist if __name__ == '__main__': print "To test the MimeWriter module, run mailer"