Maker Portal

View Original

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. 

See this content in the original post

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.

See this content in the original post

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. 

See this content in the original post

-blog title image courtesy of The Raspberry Pi Foundation

See more in Python programming:

See this gallery in the original post