Home arrow Python arrow Page 4 - Sending and Viewing Email with the PyMailGUI Client

Email Replies and Forwards - Python

In this third part of a six-part series, you will learn how to send and view email with attachments with the PyMailGUI client, as well as how to accomplish other tasks. This article is excerpted from chapter 15 of Programming Python, Third Edition, written by Mark Lutz (O'Reilly, 2006; ISBN: 0596009259). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Sending and Viewing Email with the PyMailGUI Client
  2. Sending Email and Attachments
  3. Viewing Email and Attachments
  4. Email Replies and Forwards
By: O'Reilly Media
Rating: starstarstarstarstar / 2
July 26, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

  1. The “From” line is set to your email address in yourmailconfig module.
  2. 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:”.
  3. 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).
  4. 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.



 
 
>>> More Python Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: