Besides allowing users to read and write email, PyMailGUI also lets users forward and reply to incoming email sent from others. 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 15-28 appears.
Figure 15-28. 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:
The “From” line is set to your email address in yourmailconfig module.
The “To:” line is initialized to the original message’s “From” address (we’re replying to the original sender, after all). Anemail.Utilscall processes the “To:”.
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).
The body of the reply is initialized with the signature line inmailconfig, 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.
Luckily, all of this is much easier than it may sound. Python’s standardemailmodule extracts all of the original message’s header lines, and a single stringreplacemethod 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 popup 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 15-29 shows the window created to forward the mail we originally wrote and received.
Figure 15-29. PyMailGUI forward compose window
Much like with replies, “From:” is filled frommailconfig, the original text is automatically quoted in the message body again, and the subject line is preset to the original message’s subject, prepended with the string “Fwd:”. I have to fill in the “To:” line manually, though, because this is not a direct reply (it doesn’t necessarily go back to the original sender).
Notice that I’m forwarding this message to two different addresses; multiple recipient addresses are separated with a semicolon (;) in the “To:”, “Cc:”, and “Bcc:” header fields. The Send button in this window fires the forwarded message off to all addresses listed in these headers.
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 15-30, they appear as messages 22 and 21 (the order they appear in may depend on timing issues at your server).
Figure 15-30. 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 email from the server to your computer. The three messages we just wrote (20 through 22) will also appear in any other email program you use on your account (e.g., in Outlook, or in a web mail 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 21 and press View, we see the forward message we sent, as in Figure 15-31. 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.
Figure 15-32 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 15-31 simply extracts bits and pieces out of the text shown in the raw display form.
Please check back next week for the continuation of this article.