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 15-17 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. Like all long-running operations, full message downloads are run in parallel threads to avoid blocking.
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 TextEditorobject for display (it is also displayed in a web browser automatically if it is HTML text). 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.
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 popup dialog, and they can be saved and opened all at once with Split. Figure 15-18 shows this window’s Parts list popup, and Figure 15-19 displays this window’s Split dialog in action.
When the Split dialog in Figure 15-19 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.
For instance, Figure 15-20 shows one of the image parts 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 filename’s quick-access button in Figure 15-17 to view it immediately, or run Split to open all parts at once.
By this point, the photo attachment displayed in Figure 15-20 has really gotten around: it has been encoded, attached, and sent, and then fetched, parsed, and decoded. Along the way, it has moved through multiple machines—from the client to the SMTP server, to the POP server, and back to the client.
Note that the main message text counts as a mail part, too—when selected, it opens in a PyEdit window, like that captured in Figure 15-21, 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 the HTML text in its window, and opens a web browser to view the mail with its HTML formatting.
PyMailGUI also opens HTML and XML parts in a web browser and uses the Windows Registry to open well-known Windows document types. For example, .doc, .xls, and .pdf files usually open, respectively, in Word, Excel, and Adobe Reader. Figure 15-22 captures the response to the calendar.html quick-access part button in Figure 15-17 on my Windows laptop (Firefox is my default web browser).
The quick-access buttons in the middle of the Figure 15-17 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 (popup dialogs name the directory to use).
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 15-23 captures one such message in the GUI; this was message 10 in Figure 15-9, if you’re keeping track—a very complex mail, with 5 photos and 12 total parts.
Like much of PyMailGUI’s behavior, the maximum number of part buttons to display in view windows can be configured in themailconfig.pyuser settings module.
That setting specified five buttons in Figure 15-23. Figure 15-24 shows a different mail with many attachments being viewed; the part buttons setting has been changed to a maximum of eight (this mail has seven parts). The setting can be higher, but at some point the buttons may become unreadable (use Split instead).
Figures 15-25 and 15-26 show what happens when the sousa.au and ch19.pdf buttons in Figure 15-24 are pressed on my Windows laptop. The results vary per machine; the audio file opens in Windows Media Player, the MP3 file opens in iTunes instead, and some platforms may open such files directly in a web browser.
Besides the nicely formatted view window, PyMailGUI also lets us see the raw text of a mail message. Double-click on a message’s entry in the main window’s list to bring up a simple unformatted display of the mail’s raw text (its full text is downloaded in a thread if it hasn’t yet been fetched and cached). The raw version of the mail I sent to myself in Figure 15-17 is shown in Figure 15-27.
This raw text display can be useful to see special mail headers not shown in the formatted view. For instance, the optional “X-Mailer:” header in the raw text display identifies the program that transmitted a message; PyMailGUI adds it automatically,
along with standard headers like “From:” and “To:”. Other headers are added as the mail is transmitted: the “Received:” headers name machines that the message was routed through on its way to our email server, and “Content-Type:” is added and parsed by Python’semail package.
And really, the raw text form is all there is to an email message—it’s what is transferred from machine to machine when mail is sent. The nicely formatted display of the GUI’s view windows simply parses out and decodes components from the mail’s raw text with standard Python tools, and places them in the associated fields of the display.
blog comments powered by Disqus