def saveAttachments(message, parser, savedir='partsdownload'):
"""
save fetched email's parts to files on
server to be viewed in user's web browser
"""
import os
if not os.path.exists(savedir): # in CGI script's cwd on server
os.mkdir(savedir) # will open per your browser
for filename in os.listdir(savedir): # clean up last message: temp!
dirpath = os.path.join(savedir, filename)
os.remove(dirpath)
typesAndNames = parser.saveParts(savedir, message)
filenames = [fname for (ctype, fname) in typesAndNames]
for filename in filenames:
os.chmod(filename, 0o666) # some srvrs may need read/write
return filenames
form = cgi.FieldStorage()
user, pswd, site = commonhtml.getstandardpopfields(form)
pswd = secret.decode(pswd)
try:
msgnum = form['mnum'].value # from URL link
parser = mailtools.MailParser()
fetcher = mailtools.SilentMailFetcher(site, user, pswd)
fulltext = fetcher.downloadMessage(int(msgnum)) # don't eval!
message = parser.parseMessage(fulltext) # email pkg Message
parts = saveAttachments(message, parser) # for URL links
mtype, content = parser.findMainText(message) # first txt part
commonhtml.viewpage(msgnum, message, content, form, parts) # encoded pswd
except:
commonhtml.errorpage('Error loading message')
Again, much of the work here happens in the commonhtml module, listed later in this
section (see Example 16-14). This script adds logic to decode the input password (using
the configurable secret encryption module) and extract the selected mail’s headers and
text using the mailtools module package from Chapter 13 again. The full text of the
selected message is ultimately fetched, parsed, and decoded by mailtools, using the
standard library’s poplib module and email package. Although we’ll have to refetch
this message if viewed again, version 2.0 and later do not grab all mails to get just the
one selected.‡
Also new in version 2.0, the saveAttachments function in this script splits off the parts
of a fetched message and stores them in a directory on the web server machine. This
was discussed earlier in this chapter—the view page is then augmented with URL links
that point at the saved part files. Your web browser will open them according to their
filenames and content. All the work of part extraction, decoding, and naming is
‡ Notice that the message number arrives as a string and must be converted to an integer in order to be used
to fetch the message. But we’re careful not to convert with eval here, since this is a string passed over the Net
and could have arrived embedded at the end of an arbitrary URL (remember that earlier warning?).
1260 | Chapter 16: The PyMailCGI Server