Using Raspberry Pi and Python to Send Email Alerts
Any engineer who has written a computer script has experienced their share of coding frustrations. For computational engineers, scripts run for large periods of time because of limited processing power and massive amounts of data. An experimental engineer may start an experiment and expect a computer to log data continually, without interruption, for several days. Unfortunately, many burgeoning engineers blindly rely on their written routines and squander precious time and resources because of errors in coding. My solution to this devastating pitfall is to encourage setting up quality error handling through email exceptions.
For example, if an experiment needs to run unsupervised, I recommend setting up exceptions that notify the user that an exception has been raised, and perhaps a description of the location of the error and the time of day. This prevents naive dependence on written routines by immediately notifying the user when something has gone wrong. Below is a simple example of a Python SMTP email excerpt.
## Sending Email Alerts via Zoho # # import smtplib server = smtplib.SMTP_SSL('smtp.zoho.com',port=465) #server for sending the email server.ehlo() # simple starting of the connection server.login('test_email@zoho.com','pwd_12345') # login credentials and password msg = """From:test_email@zoho.com Subject: Test Email \n To: recipient_email@gmail.com \n""" # This is where the email content goes. It could be information about the error, time of day, where in the script, etc. server.sendmail('test_email@zoho.com','recipient_email@gmail.com',msg) # this is where the email is sent to the recipient server.quit() # exit the connection
Notice the email host called Zoho.com. I recommend using an email host different from your primary so you don't have to turn off the security protocols on your Gmail (or other secure host). Using a lesser known email allows the email exchange to be impersonal and minimizes any potential for hacking of sensitive personal information.
There are also advantages to email notifications beyond error handling. One potential implementation is in data analytics. If someone is trading stocks and they want to know when a certain stock price changes, rather than constantly watching the ticker an email alert can notify the user of percent changes, price minimums, trade times, etc. Email alerts are great ways to indicate that something has occurred without needing to check the computer or be present to monitor the computation. I personally use them for home automation alerts (motion detectors, reed switch trips, etc.), personal notifications about meetings or events, and of course the reasons mentioned above. Email alerts can be as simple or complex as desired; they can be personalized and, if you're like me and you check your email often, they can help you navigate the daily obstacles encountered in life as an engineer.
Example of an email alert executed as a function. This function can be called anywhere in the code and will email a message with relevant details.
## Function for Sending Emails in Code # # import smtplib def email_alert(examp_str=""): server = smtplib.SMTP_SSL('smtp.zoho.com', port=465) print("Server Started...") server.login('test_email@zoho.com','pwd123') print("Server Login Successful") msg ="""From:test_email@zoho.com\nTo:recipient_email@gmail.com\nSubject: Testing Email\n """ msg = msg+"Number = "+examp_str try: server.sendmail('test_email@zoho.com','recipient_email@gmail.com',msg) except: return server print("Sending Message...") server.quit() print("Quit Server") for ii in range(0,50): if ii==35: server=email_alert(str(ii)) else: continue
-blog title image courtesy of The Raspberry Pi Foundation
See more in Python programming: