Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (136 page)

BOOK: Programming Python
11.28Mb size Format: txt, pdf, ePub
ads
Viewing Email and Attachments

Now let’s view the
mail message that was sent and received. PyMailGUI lets us
view email in formatted or raw mode. First, highlight (single-click) the
mail you want to see in the main window, and press the View button.
After the full message text is downloaded (unless it is already cached),
a formatted mail viewer window like that shown in
Figure 14-18
appears. If multiple
messages are selected, the View button will download all that are not
already cached (i.e., that have not already been fetched) and will pop
up a view window for each selected. Like all long-running operations,
full message downloads are run in parallel threads to avoid
blocking.

Figure 14-18. PyMailGUI view incoming mail window

Python’s
email
module
is used to parse out header lines from the raw text of the
email message; their text is placed in the fields in the top right of
the window. The message’s main text is fetched from its body and stuffed
into a new
TextEditor
object for
display at the window bottom. PyMailGUI uses heuristics to extract the
main text of the message to display, if there is one; it does not
blindly show the entire raw text of the mail. HTML-only mail is handled
specially, but I’ll defer details on this until later in this
demo.

Any other parts of the message attached are displayed and opened
with quick-access buttons in the middle. They are also listed by the
Parts pop up dialog, and they can be saved and opened all at once with
Split.
Figure 14-19
shows
this window’s Parts list pop up, and
Figure 14-20
displays this window’s Split dialog
in action.

Figure 14-19. Parts dialog listing all message parts

Figure 14-20. Split dialog selection

When the Split dialog in
Figure 14-20
is submitted, all message parts are saved to the directory you select,
and known parts are automatically opened. Individual parts are also
automatically opened by the row of quick-access buttons labeled with the
part’s filename in the middle of the view window, after being saved to a
temporary directory; this is usually more convenient, especially when
there are many attachments.

For instance,
Figure 14-21
shows the two
image parts attached to the mail we sent open on my Windows laptop, in a
standard image viewer on that platform; other platforms may open this in
a web browser instead. Click the image filenames’ quick-access buttons
just below the message headers in
Figure 14-18
to view them
immediately, or run Split to open all parts at once.

Figure 14-21. PyMailGUI opening image parts in a viewer or browser

By this point, the photo attachments displayed in
Figure 14-21
have really gotten
around: they have been MIME encoded, attached, and sent, and then
fetched, parsed, and MIME decoded. Along the way, they have moved
through multiple machines—from the client, to the SMTP server, to the
POP server, and back to the client, crossing arbitrary distances along
the way.

In terms of user interaction, we attached the images to the email
in
Figure 14-14
using the
dialog in
Figure 14-15
before we
sent the email. To access them later, we selected the email for viewing
in
Figure 14-17
and
clicked on their quick-access button in
Figure 14-18
. PyMailGUI encoded the
photos in Base64 form, inserted them in the email’s text, and later
extracted and decoded it to get the original photos. With Python email
tools, and our own code that rides above them, this all just works as
expected.

Notice how in Figures
14-18
and
14-19
the main message text counts as a
mail part, too—when selected, it opens in a PyEdit window, like that
captured in
Figure 14-22
, from
which it can be processed and saved (you can also save the main mail
text with the Save button in the View window itself). The main part is
included, because not all mails have a text part. For messages that have
only HTML for their main text part, PyMailGUI displays plain text
extracted from its HTML text in its own window, and opens a web browser
to view the mail with its HTML formatting. Again, I’ll say more on
HTML-only mails later.

Figure 14-22. Main text part opened in PyEdit

Besides images and plain text, PyMailGUI also opens HTML and XML
attachments in a web browser and uses the Windows Registry to open
well-known Windows document types. For example,
.doc
and
.docx
,
.xls
and
.xlsx
, and
.pdf
files usually open, respectively, in Word,
Excel, and Adobe Reader.
Figure 14-23
captures the
response to the
lp4e-pref.html
quick-access part
button in
Figure 14-18
on my
Windows laptop. If you inspect this screenshot closely, or run live for
a better look, you’ll notice that the HTML attachment is displayed in
both a web browser and a PyEdit window; the latter can be disabled in
mailconfig
, but is on by default to
give an indication of the HTML’s encoding.

The quick-access buttons in the middle of the
Figure 14-18
view window are a more
direct way to open parts than Split—you don’t need to select a save
directory, and you can open just the part you want to view. The Split
button, though, allows all parts to be opened in a single step, allows
you to choose where to save parts, and supports an arbitrary number of
parts. Files that cannot be opened automatically because of their type
can be inspected in the local save directory, after both Split and
quick-access button selections (pop up dialogs name the directory to use
for this).

After a fixed maximum number of parts, the quick-access row ends
with a button labeled “...”, which simply runs Split to save and open
additional parts when selected.
Figure 14-24
captures one such
message in the GUI; this message is available in
SavedMail
file
version30-4E
if you want to view it offline—a
relatively complex mail, with 11 total parts of mixed types.

Figure 14-23. Attached HTML part opened in a web browser

Figure 14-24. View window for a mail with many parts

Like much of PyMailGUI’s behavior, the maximum number of part
buttons to display in view windows can be configured in the
mailconfig.py
user settings module. That
setting specified eight buttons in
Figure 14-24
.
Figure 14-25
shows what the
same mail looks like when the part buttons setting has been changed to a
maximum of five. The setting can be higher than eight, but at some point
the buttons may become unreadable (use Split instead).

BOOK: Programming Python
11.28Mb size Format: txt, pdf, ePub
ads

Other books

No Angel by Vivi Andrews
Place Called Estherville by Erskine Caldwell
Cambodian Hellhole by Stephen Mertz
Nightingale Songs by Strantzas, Simon
Anita Blake 22.6 - Shutdown by Laurell K. Hamilton
The Counterfeit Mistress by Madeline Hunter
Cape Storm by Rachel Caine


readsbookonline.com Copyright 2016 - 2024