SunQuest
 
       Python
  Home arrow Python arrow Page 3 - Python Email Libraries, part 2: IMAP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python Email Libraries, part 2: IMAP
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2005-05-16

    Table of Contents:
  • Python Email Libraries, part 2: IMAP
  • Authentication
  • Searching for Messages
  • Fetching Message Information

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python Email Libraries, part 2: IMAP - Searching for Messages


    (Page 3 of 4 )

    Once you have connected and entered the correct mailbox, you will want to search for specific messages. You can do this by downloading the pertinent information for all messages in the mailbox, and then processing all of that data locally. However, that is typically not necessary. IMAP defines a very useful search function that allows the server to do this for you. This is important for several obvious reasons. First of all, it greatly reduces the amount of network traffic sent, thus speeding up your program. It also reduces the amount of logic you have to code directly into your system. 

    There are many available attributes for you to search on. For instance, you can search for only new messages, messages of a certain size, messages that are from a particular person or email address, or messages that contain a specific string in the body. In general, all of the search criteria are strung together with ANDs, but it is possible to use the prefix operator OR to make an “or” condition. Some examples look like:

                r, data = server.search(None, “(FROM \”fred\”)”)

          r, data = server.search(None, “(SMALLER 20000)”)

          r, data = server.search(None, “(NEW)”)

          r, data = server.search(None, “(OR NEW SMALLER 20000)”)

    These examples should be relatively obvious. The first searches for all messages that have “fred” in the “FROM” header. The second searches for all messages smaller than 20kb (imaplib uses raw bit octets, so multiplying by 1000 is necessary), and the third searches for all messages that have not been viewed yet. Finally, the last searches for all messages that are either new or smaller than 20kb.

    Notice that the “OR” operator is a prefix, and must be put before the two search keys that are being operated on; this is also the case for the “NOT” operator. Another thing to note in this example is the strategy of putting a tuple on the left side of the equals sign, thus allowing you to break the return value immediately into the response part and the data part. This is somewhat different from the response processing shown above, but allows you to easily grab both the response and data. 

    The data portion of the returned information contains a list of the message sequence numbers that satisfy the search criteria. In all of the above statements, the first argument is something called a “charset” argument. It defines the sort of encoding that the messages should have. Usually, this is used for defining different character encoding standards to deal with different languages' alphabets. Here, we use the “None” arguments for simplicity, assuming that all messages use the same English encoding.

    More Python Articles
    More By Michael Swanson


       · just what i needed to get started!thanks a bunch!
       · Thanks! Glad it was helpful for you!Michael
       · Just what I needed to get me pointed in the right direct.
       · First good article really helped.r, data = server.fetch(‘2:5’,...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway