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 a list window generates a mail composition window; one has been captured in Figure 15-13.
Figure 15-13. PyMailGUI write-mail compose window
This window is much like the message view window we saw in Figure 15-4, except there are no quick-access part buttons in the middle (this window is a new mail). It has fields for entering header line details, and an attachedTextEditorobject for writing the body of the new email.
For write operations, PyMailGUI automatically fills the “From:” line and inserts a signature text line (“--Mark...”), from yourmailconfig module settings. You can change these to any text you like in the GUI, but the defaults are filled in automatically from yourmailconfig. When the mail is sent, anemail.Utilscall handles date and time formatting in themailtools module in Chapter 14.
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 the addresses you typed into the “To:”, “Cc:”, and “Bcc:” lines, using Python’ssmtplibmodule. PyMailGUI adds the header fields you type as mail header lines in the sent message. To send to more than one address, separate them with a “;” character in header fields. 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.
Also in compose windows, the Attach button issues a file selection dialog for attaching a file to your message, as in Figure 15-14. The Parts button pops up a dialog displaying files already attached, like that in Figure 15-15. 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 15-14. Attachment file dialog for attach
Figure 15-15. 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; redrawand 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.
You’ll get an error popup 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 may try again or save its text for later use. If you don’t get an error popup, 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 15-16.
Figure 15-16. 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 massage’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).