SMTP

Websites by Email

I'd just like to share with you these forty-seven lines of python code that amaze me. Not because they do anything special, but because they represent something that is pretty important to programming languages, modules, or libraries (depending on the language). Let's take a quick gander at the code:

  1. #!/usr/bin/python
  2. """
  3. #
  4. # This is a simple script to grab the contents of a a website,
  5. # encoded into a MIME message, and email it
  6. """
  7. import sys
  8. import smtplib
  9. import pycurl
  10. from cStringIO import StringIO
  11. from email.mime.text import MIMEText
  12. from email.mime.multipart import MIMEMultipart
  13.  
  14. data_buf = StringIO()
  15. curl = pycurl.Curl()
  16.  
  17. # Setup pycurl to grab the data
  18. curl.setopt(pycurl.URL, sys.argv[1])
  19. curl.setopt(pycurl.WRITEFUNCTION, data_buf.write )
  20. curl.perform()
  21. curl.close()
  22.  
  23. # Begin creating email
  24. #create html & text parts of the email
  25. part1 = MIMEText(data_buf.getvalue(), 'html')
  26. part2 = MIMEText(data_buf.getvalue(), 'text')
  27.  
  28. # next 5 lines put it all together
  29. msg = MIMEMultipart('alternative')
  30. msg['Subject'] = 'Website by Email'
  31.  
  32. msg.attach(part1)
  33. msg.attach(part2)
  34.  
  35. # email away
  36. # this code was copied from:
  37. # http://www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/
  38. # Geshi keeps adding extra lines here, I'm not sure why
  39. to = 'add_your_own'
  40. gmail_user = 'add_your_own'
  41. gmail_pwd = 'add_your_own'
  42. smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
  43. smtpserver.ehlo()
  44. smtpserver.starttls()
  45. smtpserver.login(gmail_user, gmail_pwd)
  46. smtpserver.sendmail(gmail_user, to, msg.as_string())
  47. print 'done!'
  48. smtpserver.quit()

This little script started out as a work assignment (which I have modified to make it more general). The task was to grab the contents of a particular website and email them. Not terribly complicated, but also not one I was thinking would be accomplished with twenty-eight lines of actual code.

Let's take a second to review which modules are being used above:

The code used by PycURL to grab the website, and deposit the data into the string.

StringIO module, to hold the contents of the website. (I wasn't able to get a regular string to work here. If you know how, please tell me.)

The MIME module for reformatting the website contents.

The SMTPlib module, for having all the code in it to properly communicate with a SMTP server, including TLS for encryption, to send said email through the web.

It wasn't until doing this project that I fully realized the importance of modules. It was these four modules that saved me uncountable hours of coding, testing, and debugging. Even if I had written out the functionality I needed from scratch, what I would have written would not have anywhere near the functionality that these other modules provided. In correlation to this, I wonder if there is some kind of connection between how popular a lanugage is and how easily extendable it is. I have no way to prove this of course, but both Perl & Python could be good examples (and also happen to be the languages I'm most familiar with). Both languages are popular, as shown by the normalized graph by langpop.com, and both are also extremely easy to add extra functionality to. The PycURL, CLyther, and PyCUDA modules are great cases in point. PycURL allows Python to tap into the CURL library. CLyther allows Python to use OpenCL, and PyCUDA allows Python to access the CUDA libraries. It makes my head hurt just to think about the amount of code that it would take to perform these same functions, if written from scratch.

After having this moment of realization, I find I am extremely grateful to all the programmers out there who put in their time to help create modules like these and to help them perform as well as they do. I tip my hat at you all.

If you made it this far down into the article, hopefully you liked it enough to share it with your friends. Thanks if you do, I appreciate it.

Bookmark and Share

Syndicate content