Python function to send email (via GMail) when script has completed

As I mentioned yesterday, I have been moving most of my computationally intensive work to Amazon's EC2 cloud computing service. One important thing to keep in mind when you are using EC2 is that ever minute counts, and Amazon is running a tab. In the interest of best-practices I decided to write a short Python function that would notify me an email via GMail when the script had finished. I also thought it would be useful to include the runtime of the script in the body of the email, both in terms of benchmarking and as a sort-of digital receipt.

For your enjoyment, here is that function:

Note: in the above example I am using GMail to send the email via SMTP, but it would be trivial to modify the above function to work with a different SMTP server.