Skip to main content
Skip table of contents

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: As of writing, 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.

As with Python, you'll need to set up SendGrid according to the instructions below.

LabVIEW Email (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:

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 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 SettingsSender 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 APIIntegration 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:


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 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 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 (Basic Python script to send an email)

SendGrid with Other Languages

For other languages, see SendGrid's list of languages. 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. (A Python script that sends an email if AIN0 exceeds a range)

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. 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.