Python
  Home arrow Python arrow Page 4 - Python Email Libraries: SMTP and Email...
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 
Moblin 
JMSL Numerical Library 
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: SMTP and Email Parsing
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    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


    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. 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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





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