Sending Emails From a Program
This page introduces several options for programmatically sending emails to send data or alerts with code. This functionality allows the data from a remote LabJack to be accessible from almost anywhere. Email alerts are best suited for infrequent communication such as notifications or small, sparse data sets. For frequent or high volume data transfer such as logging, see our LabJack via Internet App Note or our Google Sheets Logging App Note.
SendGrid Email Delivery Service
SendGrid is one of the largest and most common services that provides programmatic email functionality and offers an API that makes sending emails simple and effective. The benefits of SendGrid include:
Free: Free plans allow users to send 100 emails/day, which suits most basic notification needs. There are also paid plans which allow for a higher volume of daily emails.
Secure: SendGrid allows you to send emails from a program without needing your email account's password. It uses a one-time verification to link to your SendGrid account.
Multi-Language Support: SendGrid's email API supports Python, Java, C#, Node.js, Ruby, Go, PHP, and cURL. The Python API is especially easy to integrate with a program interfacing with a LabJack.
Simple Setup: Making a SendGrid account and getting started with sending your first email is quick and easy.
SendGrid with LabVIEW
To send email from LabVIEW, see the basic LabVIEW package attached below. Please note that it doesn't perform error handling.
This package works for LabVIEW version 7.1 and up, and contains a Mac OS and Windows version. When running the .vi, it will call on the executable file that uses SendGrid in the LabVIEW package at the same file location as the .vi.
As with Python, you'll need to set up SendGrid according to the instructions below.
LabVIEW Email App.zip (A LabVIEW .vi without error checking that sends email)
Setting Up SendGrid for Emailing in Python
While SendGrid has API libraries in many languages, this App Note focuses on Python support. The instructions below are likely pretty close to how to set up SendGrid in other languages.
LabJack provides Python support:
T-series: labjack-ljm
U3/U6/UE9/U12: LabJackPython
Using labjack-ljm or LabJackPython in tandem with SendGrid allows you to write Python programs that read data from a LabJack and email that data after a certain interval. You can also send an email alert if a value read from the LabJack goes above or below a certain threshold.
Install Python and SendGrid Package
Ensure that you have a SendGrid-compatible version of Python downloaded and installed. Check SendGrid's documentation for what versions of Python are compatible. Once you have Python installed, install SendGrid's Python package via pip by executing the command:
pip install sendgrid
Create SendGrid Account
Visit https://sendgrid.com/ and click StartForFree
. Then, sign up for a new SendGrid account. The email address that you use to sign up for your SendGrid Account does not need to be the address that you intend to be the sending email address for your emails sent with the SendGrid API. You will set this up later. Finally, log into your SendGrid account and verify your email address (SendGrid limits accounts with unverified emails).
Setup Sending Email Account
Now it is time to set up your sending email address. This will be the actual address that appears as the sender of any emails you send with your SendGrid account.
First, in the navigation bar on the left side of your SendGrid account select Settings
→ Sender Authentication
. Then, click Get Started
in the Single Sender Verification section. Complete all of the fields required for your sending email. You must use a real email address for the sending email as you will have to verify it later.
You may want to make a whole new email account/address dedicated specifically to sending emails for your application. That way all of your emails for the application will be organized in one place. Also, if your API key were to fall into the wrong hands, they would only be able to send emails from this dedicated email and not your main email.
Finally, verify your sending email by clicking Verify Single Sender in the email that is sent to your sending address after it is registered. Now you have a sending email linked to your SendGrid account! For more information on registering a sending email, see SendGrid's Single Sender Verification Documentation.
Generate API Key
Now it is time to get an API key and integrate SendGrid into your Python application. In the navigation bar on the left side of your SendGrid account select Email API
→ Integration Guide
. Choose Web API
as your email method and then choose Python
as your language. Then, choose a name for your API key and then click Create Key
. Copy your API key and paste it somewhere where you can access it later.
Add API Key as an Environment Variable
Best practice is to place the API key in an environment variable, but it can also simply be placed in a text file until it is embedded in your script. To add your API key to an environment variable on Linux/Mac run the following commands in your terminal (these commands can also be found in the Integration Guide):
Handling SendGrid API Key - bash
echo "export SENDGRID_API_KEY='YOUR API KEY HERE'" > sendgrid.env echo "sendgrid.env" >> .gitignore source ./sendgrid.env
To add your API key to an environment variable on Windows, execute the following command in your command prompt:
setx SENDGRID_API_KEY "YOUR API KEY HERE"
For both of these examples, replace YOUR API KEY HERE
with the actual API key generated for you by SendGrid in the Integration guide.
Send Your First Python Email
Now it is time to send your first email. Download the simple email example Python script (also attached below), and replace from_email@example.com
on line 8 with the sending email that you configured. You must use an email address that has been verified as a single sender as described in the section Setup Sending Email Account above. Replace to@example.com
on line 9 with the destination email that you want your message sent to. Finally, update the subject and body of the email on lines 10 and 11 respectively.
Run the Python script and your email should be sent to the destination email address that you entered. If the program runs without errors but you don't see the email in your inbox, check your spam/promotions folder. If the email was sent to your spam folder, manually mark the email/sender as not spam so further emails show up in your inbox.
Now you can integrate this code into any Python program to send emails! For more information or help with setup see SendGrid's Integration Guide or Python Quick Start Guide.
simple_email.zip (Basic Python script to send an email)
SendGrid with Other Languages
For other languages, see SendGrid’s GitHub. If the language you use isn't supported, you can create a script in a SendGrind-supported language and call it from your language. Generally, languages can call scripts using "system" calls or calling "shell" commands. For example, you could use Matlab to collect data, then use the system
command to call a Python script similar to the one attached below.
Using Email for LabJack Data Alerts
LabJack devices are often used to monitor a physical value and check that it is within a safe or desired range. The email capabilities of SendGrid synergize very well with this use case for LabJacks and allow users to send an email to themselves or a technician if a physical value goes outside of its desired range. See our example email alert Python script for a very simple implementation of this functionality. The script will measure and print the voltage on AIN0 every 3 seconds and verify that AIN0 is between 2V and 4V. If AIN0 goes outside of this range, an email alert is sent to the to_email_addr specified on line 9.
lj_email_alert.zip (A Python script that sends an email if AIN0 exceeds a range)
Text Messaging Options
Text API options are less accessible for basic notification uses, as spam laws and message tolls make verifying phone numbers more difficult. Twilio Comms offers free SMS messaging capabilities as long as you have a verified toll-free number.
A simple way to send SMS/MMS messages is by using emails. To send an email to a phone number, you must use the number and specific carrier gateway address:
AT&T:
number@txt.att.net
for a normal text message (SMS), ornumber@mms.att.net
for a multimedia message (MMS)Verizon:
number@vtext.com
for both SMS and MMS messagesSprint PCS:
number@messaging.sprintpcs.com
for both SMS and MMS messagesT-Mobile:
number@tmomail.net
for both SMS and MMS messages
Alternate Emailing Options
Although SendGrid is one of the most prominent email API options, there are several other alternatives for sending emails from within a program:
cURL is a command line tool designed specifically for network operations. cURL has a simple SMTP email option to allow sending emails from the command line.
libcurl is a client-side URL transfer library with an API that enables many network operations in C. It provides several SMTP email options for sending emails in C with varying degrees of security.
mailgun is an email delivery service which is very similar to SendGrid. It offers simple multi-language API support for sending emails and has larger commercial plans for bigger companies. However, it currently only offers a short trial for free.