Python
  Home arrow Python arrow Python Email Libraries, part 2: IMAP
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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
     
     
    The Best Selling PC Migration Utility.
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

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

    The first article in this series discussed how to access a POP3 server with a Python script. While that protocol is useful for learning the basics of how email works, IMAP is the protocol most used today. This article covers this more complicated protocol.

    Introduction

    In the previous article from this series, I described how to access a POP3 server with a Python script. While POP is a useful protocol for connecting to legacy servers and gaining a basic understanding of how email works, IMAP is the workhorse protocol for email today. IMAP is designed for users to store their email remotely on the server, rather than locally on their own server. It also allows for users to create a folder system in their inbox to organize and move messages around the mailbox. This all makes IMAP a much more complicated protocol than POP, which in turn translates to a more complicated library in Python. 

    Much of the IMAP library in Python is very closely tied to the actual IMAP protocol definition in RFC 2060. When you are working with this library, both the Python docs and that RFC are necessary references, as very often arguments to functions will need to be formatted as this RFC states, and responses will be returned in the format defined by that RFC.

    Connecting to the Server

    Again, the first task you’ll need to accomplish when working with an IMAP server is creating the IMAP object to communicate with the server:

                from imaplib import *

          …

          server = IMAP4(“test.com”)


    The objects necessary to work with IMAP servers are held in the “imaplib” library so it must be imported first. The library itself also contains a few other useful objects. First of all, there are two exception classes, “error” and “abort.” The “error” exception generally denotes some sort of failure on the client’s part, and the reason for that error is passed along with the exception. The “abort” exception usually results from some server-side error, and the connection must usually be reset to recover from this error. In addition, “imaplib” also contains several functions that can come in handy, for converting dates into the format used by IMAP and for parsing some IMAP server responses.

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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