I l@ve RuBoard |
10.11 Bundling Files in a MIME MessageCredit: Matthew Dixon Cowles 10.11.1 ProblemYou want to create a multipart MIME message that includes all files in the current directory. 10.11.2 SolutionIf you often deal with composing or parsing mail messages, or mail-like messages such as Usenet news posts, the new email module gives you sparkling new opportunities: #!/usr/bin/env python import base64, quopri import mimetypes, email.Generator, email.Message import cStringIO, os # sample addresses toAddr="[email protected]" fromAddr="[email protected]" outputFile="dirContentsMail" def main( ): mainMsg = email.Message.Message( ) mainMsg["To"] = toAddr mainMsg["From"] = fromAddr mainMsg["Subject"] = "Directory contents" mainMsg["Mime-version"] = "1.0" mainMsg["Content-type"] = "Multipart/mixed" mainMsg.preamble = "Mime message\n" mainMsg.epilogue = "" # to ensure that message ends with newline # Get names of plain files (not subdirectories or special files) fileNames = [f for f in os.listdir(os.curdir) if os.path.isfile(f)] for fileName in fileNames: contentType,ignored = mimetypes.guess_type(fileName) if contentType==None: # If no guess, use generic opaque type contentType = "application/octet-stream" contentsEncoded = cStringIO.StringIO( ) f = open(fileName, "rb") mainType = contentType[:contentType.find("/")] if mainType=="text": cte = "quoted-printable" quopri.encode(f, contentsEncoded, 1) # 1 to encode tabs else: cte = "base64" base64.encode(f, contentsEncoded) f.close( ) subMsg = email.Message.Message( ) subMsg.add_header("Content-type", contentType, name=fileName) subMsg.add_header("Content-transfer-encoding", cte) subMsg.add_payload(contentsEncoded.getvalue( )) contentsEncoded.close( ) mainMsg.add_payload(subMsg) f = open(outputFile,"wb") g = email.Generator.Generator(f) g(mainMsg) f.close( ) return None if _ _name_ _=="_ _main_ _": main( ) 10.11.3 DiscussionThe email module, new in Python 2.2, makes manipulating MIME messages easier than it used to be (with the standard Python library modules already present in Python 2.1 and earlier). This is not a trivial point, so this recipe's example may be useful. See the standard Library Reference for detailed documentation about the email module. MIME (Multipurpose Internet Mail Extensions) is the Internet standard for sending files and non-ASCII data by email. The standard is specified in RFCs 2045-2049. There are a few points that are especially worth keeping in mind:
Not surprisingly, given all that, manipulating MIME messages is often considered to be a nuisance. Before Python 2.2, the standard library's modules for dealing with MIME messages were quite useful but rather miscellaneous. In particular, putting MIME messages together and taking them apart required two distinct approaches. The email module, new in Python 2.2, unifies and simplifies these two related jobs. 10.11.4 See AlsoRecipe 10.12 shows how the email module can be used to unpack a MIME message; documentation for the standard library modules email, smtplib, mimetypes, base64, quopri, and cStringIO in the Library Reference. attachments. |
I l@ve RuBoard |