Email yourself the results of your post script

Wouldn’t it be great to get an email once a script has finished letting you know if it worked or failed?  Well it’s actually really easy to pull off.  Here’s how I did it:

Note for this example you would replace ‘smtp.example.com’ with your email host.

def SendEmail(sender, receivers, message):
    import sys, smtplib   
    try:
     smtpObj = smtplib.SMTP('smtp.example.com')
     smtpObj.sendmail(sender, receivers, message)
     print 'email sent'
    except:
     print 'error sending'
     sys.exit
then in your script you use the code like this:
—script did some stuff—
–script made some output—
message = "my output message" +  output  #for example.  use whatever you want here to convey the info you need
receivers = "myemail@myaddress.com"
sender = "myemail@myaddress.com"

SendEmail(sender, receivers, message)
Advertisements

2 thoughts on “Email yourself the results of your post script

  1. Hi Ann,
    I tried out your script today but got a little error message … there’s a small format error on line 4…. where you have ‘smpt.example.com’

    The punctuation before smtp is not the same as the end of the line …

    Otherwise, all good. Great examples!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s