Python
  Home arrow Python arrow Page 2 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python Email Libraries, part 2: IMAP
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Python Email Libraries, part 2: IMAP - Authentication
    ( Page 2 of 4 )

    Once you have successfully connected to the server, you need to log in to do anything useful. The function to do this is called “login,” and it takes two arguments, the username and password. Once you have logged in, you have several choices as to what you want to do. Since IMAP organizes mail into various “mailboxes” (which are basically folders), you can move to another mailbox, create or delete a mailbox, and fetch various parts of messages.

                    server.login(“test”, “testpass”)

          mboxes = server.list()[1]

    server.create(“Old Mail”)    

          r = server.select(“Old Mail”)

    The above code logs in to the mail server and then does some work with mailboxes. The second line gets a list of mailboxes from the server. This response gives you a Python list data structure of the server’s responses to the “list” request. As with the POP3 library, the first element of these returned results is the server’s “response,” which lets you know if the command completed successfully; since we are interested primarily in the data from this function, we strip the first element away. 

    The “list” function returns a list of strings. Each of these strings has three elements. The first part of the string contains the mailbox attributes flag. This gives you some basic information about the mailbox, such as whether the server thinks it’s interesting or not. The next element is where this mailbox lives, basically its parent directory.  This is useful when setting up a nested folder system on the email server, as you must supply the full path to a new mailbox in order to create it within another mailbox. 

    Finally, the last element is the folder name itself. If you want to find out how many messages there are in a mailbox, you must then select that mailbox. The server will return the result of the operation as well as the number of messages in the mailbox. It is important to know that you must select some mailbox before you can do any actual searching or fetching of messages. By default, the “select” function selects the “Inbox,” but it must be called before anything else can be done with individual messages.



     
     
    >>> More Python Articles          >>> More By Michael Swanson
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek