Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (135 page)

BOOK: Programming Python
5.18Mb size Format: txt, pdf, ePub
ads
Offline Processing with Save and Open

We’ve seen how to
fetch and view emails from a server, but PyMailGUI can
also be used in completely offline mode. To save mails in a local file
for offline processing, select the desired messages in any mail list
window and press the Save action button; as usual, any number of
messages may be selected for saving together as a set. A
standard
file-selection dialog appears,
like that in
Figure 14-12
, and the
mails are saved to the end of the chosen text file.

Figure 14-12. Save mail selection dialog

To view saved emails later, select the Open action at the bottom
of any list window and pick your save file in the selection dialog. A
new mail index list window appears for the save file and it is filled
with your saved messages eventually—there may be a slight delay for
large save files, because of the work involved. PyMailGUI runs file
loads and deletions in threads to avoid blocking the rest of the GUI;
these threads can overlap with operations on other open save-mail files,
server transfer threads, and the GUI at large.

While a mail save file is being loaded in a parallel thread, its
window title is set to “Loading…” as a status indication; the rest of
the GUI remains active during the load (you can fetch and delete server
messages, view mails in other files, write new messages, and so on). The
window title changes to the loaded file’s name after the load is
finished. Once filled, a message index appears in the save file’s
window, like the one captured in
Figure 14-13
(this window also
has three mails selected for processing).

Figure 14-13. List window for mail save file, multiple selections

In general, there can be one server mail list window and any
number of save-mail file list windows open at any time. Save-mail file
list windows like that in
Figure 14-13
can be opened at
any time, even before fetching any mail from the server. They are
identical to the server’s inbox list window, but there is no help bar,
the Load action button is omitted since this is not a server view, and
all other action buttons are mapped to the save file, not to the
server.

For example, View opens the selected message in a normal mail view
window identical to that in
Figure 14-5
,
but the mail originates from the local file. Similarly, Delete removes
the message from the save file, instead of from the server’s inbox.
Deletions from save-mail files are also run in a thread, to avoid
blocking the rest of the GUI—the window title changes to “Deleting…”
during the delete as a status indicator. Status indicators for loads and
deletions in the server inbox window use pop ups instead, because the
wait is longer and there is progress to display (see
Figure 14-8
).

Technically, saves always append raw message text to the chosen
file; the file is opened in
'a'
mode
to append text, which creates the file if it’s new and writes at its
end. The Save and Open operations are also smart enough to remember the
last directory you selected; their file dialogs begin navigation there
the next time you press Save or Open.

You can also save mails from a saved file’s window—use Save and
Delete to move mails from file to file. In addition, saving to a file
whose window is open for viewing automatically updates that file’s list
window in the GUI. This is also true for the automatically written
sent-mail save file, described in the next
section.

Sending Email and Attachments

Once we’ve
loaded email from the server or opened a local save file,
we can process our messages with the action buttons at the bottom of
list windows. We can also send new emails at any time, even before a
load or open. Pressing the Write button in any list window (server or
file) generates a mail composition window; one has been captured in
Figure 14-14
.

Figure 14-14. PyMailGUI write-mail compose window

This window is much like the message view window we saw in
Figure 14-5
, except there are no quick-access
part buttons in the middle (this window is a new mail). It has fields
for entering header line detail, action buttons for sending the email
and managing attachment files added to it when sent, and an attached
TextEditor
object component for
writing and editing the main text of the new email.

The PyEdit text editor
component at the bottom has no File menu in this role, but
it does have a Save button—useful for saving a draft of your mail’s text
in a file. You can cut and paste this temporary copy into a composition
window later if needed to begin composing again from scratch. PyEdit’s
separate Unicode policies apply to mail text drafts saved this way (it
may ask for an encoding—see
Chapter 11
).

For write operations, PyMailGUI automatically fills the From line
and inserts a signature text line (the last two lines shown), from
your
mailconfig
module
settings. You can change these to any text you like in the GUI, but the
defaults are filled in automatically from your
mailconfig
. When the mail is sent, an
email.utils
call handles date and time
formatting in the
mailtools
module in
Chapter 13
.

There is also a new set of action buttons in the upper left here:
Cancel closes the window (if verified), and Send delivers the mail—when
you press the Send button, the text you typed into the body of this
window is mailed to all the addresses you typed into the To, Cc, and Bcc
lines, after removing duplicates, and using Python’s
smtplib
module.
PyMailGUI adds the header fields you type as mail header lines in the
sent message (
exception
: Bcc
recipients receive the mail, but no header line is generated).

To send to more than one address, separate them with a comma
character in header fields, and feel free to use full “name”

pairs for recipients. In this mail, I fill in the To
header with my own email address in order to send the message to myself
for illustration purposes. New in this version, PyMailGUI also prefills
the Bcc header with the sender’s own address if this header is enabled
in
mailconfig
; this prefill sends a
copy to the sender (in addition to that written to the sent-mail file),
but it can be deleted if unwanted.

Also in compose windows, the Attach button issues a file selection
dialog for attaching a file to your message, as in
Figure 14-15
. The Parts button pops up
a dialog displaying files already attached, like that in
Figure 14-16
. When your message is
sent, the text in the edit portion of the window is sent as the main
message text, and any attached part files are sent as attachments
properly encoded according to their type.

Figure 14-15. Attachment file dialog for Attach

Figure 14-16. Attached parts list dialog for Parts

As we’ve seen,
smtplib
ultimately sends bytes to a server over a socket. Since this can be a
long-running operation, PyMailGUI delegates this operation to a spawned
thread, too. While the send thread runs, a nonblocking wait window
appears and the entire GUI stays alive; redraw and move events are
handled in the main program thread while the send thread talks to the
SMTP server, and the user may perform other tasks in parallel, including
other views and sends.

You’ll get an error pop up if Python cannot send a message to any
of the target recipients for any reason, and the mail composition window
will pop up so that you can try again or save its text for later use. If
you don’t get an error pop up, everything worked correctly, and your
mail will show up in the recipients’ mailboxes on their email servers.
Since I sent the earlier message to myself, it shows up in mine the next
time I press the main window’s Load button, as we see in
Figure 14-17
.

Figure 14-17. PyMailGUI main window after loading sent mail

If you look back to the last main window shot, you’ll notice that
there is only one new email now—PyMailGUI is smart enough to download
only the one new message’s header text and tack it onto the end of the
loaded email list. Mail send operations automatically save sent mails in
a save file that you name in your configuration module; use Open to view
sent messages in offline mode and Delete to clean up the sent mail file
if it grows too large (you can also save from the sent-mail file to
another file to copy mails into other save files per
category).

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

Other books

Let Loose the Dogs by Maureen Jennings
In Dreams by J. Sterling
Toad Words by T. Kingfisher
Rachel by Jill Smith
Monument to the Dead by Sheila Connolly
Antiques Roadkill by Barbara Allan
Dying in the Dark by Sally Spencer


readsbookonline.com Copyright 2016 - 2024