Python
  Home arrow Python arrow Page 3 - Python Email Libraries: SMTP and Email Parsing
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: SMTP and Email Parsing
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 21
    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:
      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: 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
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek