SunQuest
 
       Python
  Home arrow Python arrow Page 3 - Python Email Libraries: SMTP and Email...
IBM developerWorks
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 
SunQuest
 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: SMTP and Email Parsing
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2005-09-13

    Table of Contents:
  • Python Email Libraries: SMTP and Email Parsing
  • The Python Email Library
  • Manipulating Message Objects
  • Sending Email Messages

  • 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

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Python Email Libraries: SMTP and Email Parsing - Manipulating Message Objects


    (Page 3 of 4 )

               

    One of the major tasks you will want to accomplish when working with email messages is header manipulation. Basically, you can add, delete, modify, and list headers that are associated with an email message. Let's assume you have parsed a message as above, and the resulting “Message” objects are in the “emailMessage” object. We can now do the following actions to modify headers:

    fields = emailMessage.keys()
    if emailMessage.has_key(‘To’):
        emailMessage.__delitem__(‘To’)
    emailMessage.__setitem__(‘To’, ‘test@yourdomain.com’)
    subj = emailMessage.__getitem__(‘Subject’)
    print “The message contains the following keys:\n”
    for field in fields:
        print field + “\n”

    The preceding code grabs the header fields from the message. It then checks to see whether the message contains a “To” field. If it does, it deletes that field.  Next, it sets the “To” field to the value test@yourdomain.com. After that, it fetches the “Subject” field and then prints out all of the field names on separate lines. 

    The above code is useful for fairly simple actions with email, and for many applications, this will be sufficient to accomplish the overall goal. However, the Python email library allows for the manipulation of much more complex header fields very easily. This takes place through the various “param” methods. Some headers are expected to have parameters added to allow them to carry more data in a compact fashion and to associate that data with a particular task. The standard application of this is in the “Content-type” header. 

    Usually, when you are working with MIME messages, the constructors for the various MIME Message objects will take care of setting the "Content-Type"  header correctly, as will parsing a message using the parsing methods described above, and you won’t necessarily have to manually set this header.  However, if you are defining a content type or subtype that isn’t covered by the built in Python classes, this will be important. To set a parameter, you use the “set_param” method, and to retrieve a parameter “get_param.”  In addition, Python provides a ready made “set_type” which takes a string with the names of the main-type and sub-type and sets the Content-Type header appropriately.

    More Python Articles
    More By Michael Swanson


       · Thanks for taking the time to read my article. Feel free to post a comment of...
       · Please don't use __len__(). use len(x).Please don't use __(get|set)item__(y), use...
     

       

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

    Click Here




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