Python
  Home arrow Python arrow Page 4 - Python on the Web
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 on the Web
By: Mark Lee Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 27
    2004-05-18

    Table of Contents:
  • Python on the Web
  • Creating a Warning
  • Cookies
  • Sending Email
  • A Last Example
  • Inside uploads()...

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python on the Web - Sending Email
    (Page 4 of 6 )

    Ok, what about sending email? What sounds like a big, scary subject is actually pretty simple once you get down to it. And because of Python's amazingly clear syntax it's very clean, not to mention compact.

    #!/usr/bin/env python

    import smtplib

    def mail(address, subject, message, host = 'localhost'):

     headers = 'From: %srnTo: %srnSubject: %srnrn%s'
     message = headers % (address[0], ','.join(address[1:]), subject, message)

     server = smtplib.SMTP(host)
     server.sendmail(address[0], address[1:], message)
     server.quit()

    if __name__ == '__main__':

    mail(('someone@somewhere.com', 'sometwo@somewhere.com'), 'subject', 'message')

    Admit it, how much smaller is that than you expected! Before we delve deeper into this function, you need to understand a little about how it's being called or chances are you'll end up with errors!

    mail(('from', 'to', ...), 'subject', 'message', ['host'])

    The first argument here is a sequence of two or more addresses, the first being the 'From' address and the rest 'To' addresses. Followed by the 'Subject' and 'Message'. You may also need to tell this function what host you want to use if one isn't available locally.

    For this example we started by importing the 'smtplib' module, which provides functions/classes for sending emails from Python... so mail() is really just a wrapper over what you would normally do to send email, but it does make things easier!

    That is to say, inside mail() we do two main things...

    1. Create a MIME header containing our 'From' and 'To' addresses... as well as any other data we want to send i.e. Subject, Message, Content-Type. When complete this is assigned to the 'message' variable.
    2. Connect to the mail server using the SMTP() class. If the connection is successful then the addresses and 'message' header are sent to using the sendmail() method before quitting.

    If you plan on playing with 'smptlib' or similar modules, then you'll probably end up thanking the programmer who wrote it, for the set_debuglevel() method! As is my experience, anything that could go wrong will go wrong the first few times i.e.:

    server = smtplib.SMTP(host)

    server.set_debuglevel(1)

    server.sendmail(from, to, message)
    server.quit()

    More Python Articles
    More By Mark Lee Smith


     

       

    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 4 hosted by Hostway