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:
#!/usr/bin/env python | |
# encoding: utf-8 | |
import smtplib | |
from datetime import datetime | |
def noticeEMail(starttime, usr, psw, fromaddr, toaddr): | |
""" | |
Sends an email message through GMail once the script is completed. | |
Developed to be used with AWS so that instances can be terminated | |
once a long job is done. Only works for those with GMail accounts. | |
starttime : a datetime() object for when to start run time clock | |
usr : the GMail username, as a string | |
psw : the GMail password, as a string | |
fromaddr : the email address the message will be from, as a string | |
toaddr : a email address, or a list of addresses, to send the | |
message to | |
""" | |
# Calculate run time | |
runtime=datetime.now() - starttime | |
# Initialize SMTP server | |
server=smtplib.SMTP('smtp.gmail.com:587') | |
server.starttls() | |
server.login(usr,psw) | |
# Send email | |
senddate=datetime.strftime(datetime.now(), '%Y-%m-%d') | |
subject="Your job has completed" | |
m="Date: %s\r\nFrom: %s\r\nTo: %s\r\nSubject: %s\r\nX-Mailer: My-Mail\r\n\r\n" % (senddate, fromaddr, toaddr, subject) | |
msg=''' | |
Job runtime: '''+str(runtime) | |
server.sendmail(fromaddr, toaddr, m+msg) | |
server.quit() | |
if __name__ == '__main__': | |
# Start time of script | |
starttime=datetime.now() | |
# Some long job...we'll count to 100,000,000 | |
count=0 | |
for i in xrange(100000000): | |
count+=1 | |
# Fill these in with the appropriate info... | |
urs='' | |
psw='' | |
fromaddr='' | |
toaddr='' | |
# Send notification email | |
noticeEMail(starttime, usr, psw, fromaddr, toaddr) |
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.