Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (137 page)

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

Figure 14-25. View window with part buttons setting decreased

As a sample of other attachments’ behavior, Figures
14-26
and
14-27
show what happens when the
sousa.au
and
chapter25.pdf
buttons in Figures
14-24
and
14-18
are pressed on my Windows laptop. The
results vary per machine; the audio file opens in Windows Media Player,
MP3 files open in iTunes instead, and some platforms may open such files
directly in a web browser.

Figure 14-26. An audio part opened by PyMailGUI

Figure 14-27. A PDF part opened in PyMailGUI

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). Part of the raw version of the mail I sent
to myself in
Figure 14-18
is
shown in
Figure 14-28
; in
this edition, raw text is displayed in a PyEdit pop-up window (its prior
scrolled-text display is still present as an option, but PyEdit adds
tools such as searching, saves, and so on).

Figure 14-28. PyMailGUI raw mail text view window (PyEdit)

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’s
email
package in response to
calls from PyMailGUI.

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.
Notice the Base64 encoding text of the image file at the end of
Figure 14-28
, for example; it’s
created when sent, transferred over the Internet, and decoded when
fetched to recreate the image’s original bytes. Quite a feat, but
largely automatic with the code and libraries
invoked.

Email Replies and Forwards and Recipient Options

In addition to
reading and writing email, PyMailGUI also lets users
forward and reply to incoming email sent from others. These are both
just composition operations, but they quote the original text and
prefill header lines as appropriate. To reply to an email, select its
entry in the main window’s list and click the Reply button. If I reply
to the mail I just sent to myself (arguably narcissistic, but
demonstrative), the mail composition window shown in
Figure 14-29
appears.

Figure 14-29. PyMailGUI reply compose window

This window is identical in format to the one we saw for the Write
operation, except that PyMailGUI fills in some parts automatically. In
fact, the only thing I’ve added in this window is the first line in the
text editor part; the rest is filled in by PyMailGUI:

  • The From line is set to your email address in your
    mailconfig
    module.

  • The To line is initialized to the original message’s From
    address (we’re replying to the original sender, after all).

  • The Subject line is set to the original message’s subject
    line, prepended with a “Re:”, the standard follow-up subject line
    form (unless it already has one, in uppercase or lowercase).

  • The optional Bcc line, if enabled in the
    mailconfig
    module, is prefilled with the
    sender’s address, too, since it’s often used this way to retain a
    copy (new in this version).

  • The body of the reply is initialized with the signature line
    in
    mailconfig
    , along with the
    original message’s text. The original message text is quoted with
    >
    characters and is prepended
    with a few header lines extracted from the original message to give
    some context.

  • Not shown in this example and new in this version, too, the Cc
    header in replies is also prefilled with all the original recipients
    of the message, by extracting addresses among the original To and Cc
    headers, removing duplicates, and removing your address from the
    result. In other words, Reply really is Reply-to-All by
    default—
    it replies to the sender and
    copies all other recipients as a group. Since the latter isn’t
    always desirable, it can be disabled in
    mailconfig
    so that replies only initialize
    To with the original sender. You can also simply delete the Cc
    prefill if not wanted, but you may have to add addresses to Cc
    manually if this feature is disabled. We’ll see reply Cc prefills at
    work later.

Luckily, all of this is much easier than it may sound. Python’s
standard
email
module extracts
all of the original message’s header lines, and a single string
replace
method call does the work of adding
the
>
quotes to the original
message body. I simply type what I wish to say in reply (the initial
paragraph in the mail’s text area) and press the Send button to route
the reply message to the mailbox on my mail server again. Physically
sending the reply works the same as sending a brand-new message—the mail
is routed to your SMTP server in a spawned send-mail thread, and the
send-mail wait pop up appears while the thread runs.

Forwarding a message is similar to replying: select the message in
the main window, press the Fwd button, and fill in the fields and text
area of the popped-up composition window.
Figure 14-30
shows the window created
to forward the mail we originally wrote and received after a bit of
editing.

Figure 14-30. PyMailGUI forward compose window

Much like replies, forwards fill From with the sender’s address in
mailconfig
; the original text is
automatically quoted in the message body again; Bcc is preset initially
the same as From; and the subject line is preset to the original
message’s subject prepended with the string “Fwd:”. All these lines can
be changed manually before sending if you wish to tailor. I always have
to fill in the To line manually, though, because a forward is not a
direct reply—it doesn’t necessarily go back to the original sender.
Further, the Cc prefill of original recipients done by Reply isn’t
performed for forwards, because they are not a continuation of group
discussions.

Notice that I’m forwarding this message to three different
addresses (two in the To, and one manually entered in the Bcc). I’m also
using full “name

” formats for email addresses. Multiple
recipient addresses are separated with a comma (,) in the To, Cc, and
Bcc header fields, and PyMailGUI is happy to use the full address form
anywhere you type an address, including your own in
mailconfig
. As demonstrated by the first To
recipient in
Figure 14-30
, commas
in address names don’t clash with those that separate recipients,
because address lines are parsed fully in this version. When we’re
ready, the Send button in this window fires the forwarded message off to
all addresses listed in these headers, after removing any duplicates to
avoid sending the same recipient the same mail more than once.

I’ve now written a new message, replied to it, and forwarded it.
The reply and forward were sent to my email address, too; if we press
the main window’s Load button again, the reply and forward messages
should show up in the main window’s list. In
Figure 14-31
, they appear as
messages 15 and 16 (the order they appear in may depend on timing issues
at your server, and I’ve stretched this horizontally in the GUI to try
to reveal the To header of the last of these).

Figure 14-31. PyMailGUI mail list after sends and load

Keep in mind that PyMailGUI runs on the local computer, but the
messages you see in the main window’s list actually live in a mailbox on
your email server machine. Every time we press Load, PyMailGUI downloads
but does not delete newly arrived emails’ headers from the server to
your computer. The three messages we just wrote (14 through 16) will
also appear in any other email program you use on your account (e.g., in
Outlook or in a webmail interface). PyMailGUI does not automatically
delete messages as they are downloaded, but simply stores them in your
computer’s memory for processing. If we now select message 16 and press
View, we see the forward message we sent, as in
Figure 14-32
.

This message went from my machine to a remote email server and was
downloaded from there into a Python list from which it is displayed. In
fact, it went to three different email accounts I have (the other two
appear later in this demo—see
Figure 14-45
). The third recipient
doesn’t appear in
Figure 14-32
here
because it was a Bcc blind-copy—it receives the message, but no header
line is added to the mail itself.

Figure 14-32. PyMailGUI view forwarded mail

Figure 14-33
shows
what the forward message’s raw text looks like; again, double-click on a
main window’s entry to display this form. The formatted display in
Figure 14-32
simply extracts bits and
pieces out of the text shown in the raw display form.

Figure 14-33. PyMailGUI view forwarded mail, raw

One last pointer on replies and forwards: as mentioned, replies in
this version reply to all original recipients, assuming that more than
one means that this is a continuation of a group discussion. To
illustrate,
Figure 14-34
shows an
original message on top, a forward of it on the lower left, and a reply
to it on the lower right. The Cc header in the reply has been
automatically prefilled with all the original recipients, less any
duplicates and the new sender’s address; the Bcc (enabled here) has also
been prefilled with the sender in both. These are just initial settings
which can be edited and removed prior to sends. Moreover, the Cc prefill
for replies can be disabled entirely in the configuration file. Without
it, though, you may have to manually cut-and-paste to insert addresses
in group mail scenarios. Open this version’s mail save file to view this
mail’s behavior live, and see the suggested enhancements later for more
ideas.

Figure 14-34. Reply-to-all Cc prefills

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

Other books

Made You Up by Francesca Zappia
Egyptian Cross Mystery by Ellery Queen
His Private Nurse by Arlene James
Gawain by Gwen Rowley
A Chancer by Kelman, James
In Plain View by Olivia Newport
HotTango by Sidney Bristol
Catch Me Falling by Elizabeth Sade