Select to view content in your preferred language

Email when python encounters an error (9.3.1)

6743
3
07-07-2011 12:26 PM
FrankOgiamien
Emerging Contributor
Hi, wondering if anyone has a more efficient way of sending an email when a python script has crashed.  I have several scripts that run on a remote machine and would like to know when they fail.
My method is very crude and doesn't work quite right.
Currently using a Try/Except  which creates a .BAT file, the .BAT file runs a SQLPLUS statement that executes an Oracle function which sends me an email.   There must be a better way.

try:
   print "doing some stuff"

except:
   f = open('c:\\Fail_Notification.BAT', 'w')
   f.write("c:\n")
   f.write("cd" + "\\\n")
   f.write('SQLPLUS DB_USER/PASSWORD@DB1 @\\\\scripts\\python\\fail_notification.sql \"Frank@Hotmail.com\" \"Script has failed.\"')
   f.close()
   os.spawnv(os.P_WAIT, 'c:\\projects\\Fail_Notification.BAT', [])
   os.remove('c:\\Fail_Notification.BAT')
Tags (2)
0 Kudos
3 Replies
PatKeegan
Frequent Contributor
Frank

I use this function to send emails with errors or logs.

Just pass in an email, subject and body text....also set the SMTP server

-Pat



#==========================================================================
def emailThis(to, subject="",body="",files=[]):

    try:
        fro = "from@gmail.com"
        import smtplib
        from email.MIMEMultipart import MIMEMultipart
        from email.MIMEBase import MIMEBase
        from email.MIMEText import MIMEText
        from email.Utils import COMMASPACE, formatdate
        from email import Encoders
        msg = MIMEMultipart()
        msg['From'] = fro
        msg['To'] = to
        msg['Date'] = formatdate(localtime = True)
        msg['Subject'] = subject
        msg.attach(MIMEText(body))
        for file in files:
            part = MIMEBase('application',"octet-stream")
            part.set_payload(open(file,"rb").read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition','attachment; filename="%s"' %os.path.basename(file))
            msg.attach(part)
        smtp = smtplib.SMTP("some.smpt.server.local")
        smtp.sendmail(fro,to,msg.as_string())
        smtp.close()
        return True
    except:
        print("  error")                  
        return False


    return 1
0 Kudos
FrankOgiamien
Emerging Contributor
Cool, I was sure there was a better way.  Thanks for the help!
Frank
0 Kudos
AnthonyFarndon
Frequent Contributor
Although it ultimately does exactly the same as the example code from Pat, I would recommend taking a look at the mailer module, particularly if your email needs might grow, code/syntax a lot easier on the eye if you are not too familiar with all that mime stuff

from mailer import Mailer
from mailer import Message

message = Message(From="me@example.com",
                  To=["you@example.com", "him@example.com"],
                  Subject="Cute Cat")
message.Body = """Kittens with dynamite"""
message.attach("kitty.jpg")

sender = Mailer('smtp.example.com')
sender.send(message)


https://bitbucket.org/ginstrom/mailer
0 Kudos