Moreover, both text and binary attachments of fetched mails are simply saved in binary
form and opened by filename in browsers when their links are clicked, relying again on
browsers to do the right thing. Text attachments for sends are also subject to the CGI
upload limitations described in the note just ahead. Beyond all this, Python 3.1 appears
to have an issue printing some types of Unicode text to the standard output stream in
the CGI context, which necessitates a workaround in the main utilities module here
that opens stdout in binary mode and writes text as encoded bytes (see the code for
more details).
This Unicode/i18n support is substantially less rich than that in PyMailGUI. However,
given that we can’t prompt for encodings here, and given that this book is running short
on time and space in general, improving this for cases and browsers where it might
matter is left as a suggested exercise.
For more on specific 3.0 fourth-edition changes made, see the comments marked with
“3.0” in the program code files listed ahead. In addition, all the features added for the
prior edition are still here, as described in the next section.
Limitation on Sending Attachments in This Edition
If you haven’t already done so, see “CGI File Upload Limitations in
3.1” on page 1225. In brief, in Python 3.1 the cgi module, as well as the email package’s
parser which it uses, fail with exceptions when requests submitted by web browsers
include raw binary data or incompatibly encoded text added for uploaded files. Un-
fortunately, because this chapter’s PyMailCGI system relies on CGI uploads for at-
tachments, this limitation means that this system does not currently support sending
emails with binary email attachments such as images and audio files. It did support this
in the prior edition under Python 2.X.
Such sent attachments still work in Chapter 14’s PyMailGUI desktop application, sim-
ply because attachment file data can be read directly from local files (using binary mode
if required, and MIME encoding if needed for inclusion in email). Because the Py-
MailCGI webmail system here relies on CGI uploads to transfer attachments to the
server as an extra first step, though, it’s fully at the mercy of the currently broken cgi
module’s upload support. Coding a cgi replacement is far too ambitious a goal for this
book.
A fix is expected for this in the future, and may be present by the time you read these
words. Being based on Python 3.1, though, this edition’s PyMailCGI simply cannot
support sending such attachments, though they can still be freely viewed in mails
fetched. In fact, although this edition’s PyMailCGI inherits some new features from
mailtools such as i18n header decoding and encoding, this attachment send limitation
is severe enough to preclude expanding this system’s feature set to the same degree as
this edition’s PyMailGUI. For example, Unicode policies are simple here, if not naive.
It’s possible that some client-side scripting techniques such as AJAX may be able to
transfer attachment files independently, and thus avoid CGI uploads altogether. How-
ever, such techniques would also require deploying frameworks and technologies
1234 | Chapter 16: The PyMailCGI Server