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  
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 
IBM developerWorks
 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 - 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


       · 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 5 hosted by Hostway