Python
  Home arrow Python arrow Page 4 - 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 - Sending Email Messages
    ( Page 4 of 4 )

    The first step in sending an email message is to instantiate an “SMTP” object with the IP address and port of the remote SMTP server where the client should connect. If these arguments aren’t supplied, you will have to call the “connect” method after instantiation to connect to the server. Once you’ve created the SMTP object, normally all you will need to use are the “sendmail” method and “quit” to close the connection. 

    In addition to these methods, the SMTP library also allows a client to authenticate with a username and password for servers that require authentication. Also, you can directly issue an SMTP command with the server using the “docmd” method.  This is great for writing your own SMTP extensions, or for beginning to implement your own SMTP class. You can also use the SMTP “VRFY” command with the “verify” method to find out if a server has a specific address. However, remember that this functionality is often disabled on servers today for security reasons and to prevent spammers from taking advantage of it to send junk email. 

    The SMTP class’s “sendmail” function is the most interesting for this article. When you call this method, you must send strings with the “To:” and “From:” addresses as well as the full message represented as a string. In order to make parsing email useful, you will probably want to send the messages you’ve parsed at some point, or use a built “Message” object structure to send an email. You will need to get a string representation of your “Message” objects. This is as easy as calling the “as_string” method on the top “Message” object in the hierarchy. This returns a string encoded representation of the message ready for sending with the SMTP object. Following is an example of sending a message. Again we will assume that “emailMessage” is a Message object with data about some email.

    server = SMTP(“123.231.12.1”)
    server.login(“username”, “password”)
    server.sendmail(emailMessage[‘from’], emailMessage[‘to’], emailMessage.as_string())
    server.quit()

     

    Conclusion

    In general, sending email with Python is really very easy. You can take as much or as little control over the process as you wish. It’s certainly possible to jump in with just the built-in classes that are provided and build most applications for email.  However, if you need to go deeper, many tools are provided for you to do just that, all within the great framework of the Python scripting engine. 



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