In this user guide, we will show you how to send email alerts with Node-RED. It is very handy and practical to include email notifications in IoT projects where sensor reading monitoring, motion detection, message notifications and alarm alerts. After following this tutorial, you will be able to easily send emails to a recipient account using Node-RED in a fairly easy manner.
Before starting this tutorial, you should have Node-RED installed. You can install Node-RED on your computer or on a Raspberry Pi. Check the following tutorials:
- Install Node-RED on Raspberry Pi (32-bit and 64-bit RPI OS)
- Install Node-RED on Windows and Getting Started with Dashboard
Simple Mail Transfer Protocol server or SMTP server for short is responsible for sending and receiving emails between the senders and the receivers. Each email provider e.g., Gmail, Yahoo, Hotmail, Outlook, etc. has a unique SMTP address and settings. These can usually be accessed through your account settings. Whenever a user sends an email, the SMTP server processes it and sends it to the particular server accordingly. The recipient’s email service provides obtains the message which then turns up in the recipient’s inbox.
Setting up Sender Gmail Account
To send emails we would require two email accounts. One for the sender and one for the recipient. You can use your own email account as the sender account but it is recommended to create a new one just in case something goes wrong. For this article, we will use Gmail as the primary email provider. You can use any other provider as your preference.
Creating a new account in Gmail (Sender side)
Firstly, type https://www.google.com/gmail/about/ in your browser search tab and press enter. This will open the main page. Click ‘Create an Account as highlighted in the red rectangular box.
Now type all the relevant information and click ‘Next’. You can give in details according to your preference.
Proceed with all the steps accordingly to create your account successfully.
Generate App Password
After your account is created, we have to generate the App password in order for Node-RED to send emails from this account. This password will allow the device to access the sender Gmail account.
The first step is to turn the 2 step verification on. Head over to the following link which opens your Google Account. Head over to Security and go to ‘Signing in to Google.’ Then click 2-Step Verification and Get Started.
After following a number of steps, you will reach the final step where you will be asked to turn on the 2-Step verification. Click ‘TURN ON’ to proceed.
Note that 2-Step Verification is now On.
Now we will move ahead and generate the app password. Head over to Security > Signing in to Google > App Passwords.
Select app as ‘Mail’ and device as ‘Other.’ Give a name to your device according to your preference. We have set it as ‘Node-RED.’ Finally, click the GENERATE button to obtain the App password.
You will obtain the app password for your device. Save it and then click ‘DONE.’
Here you can view that the device Node-RED password has been successfully created.
SMTP Server Settings for Gmail Server
You can view the Gmail server settings as shown below. Knowing this is important because we have to include them in our program code to configure our account properly.
- Server: smtp.gmail.com
- Sender username: the complete email address of the sender account.
- Sender password: the password of the sender account
- Server port (TLS): 587
- Server port (SSL): 465
- SMTP TLS/SSL required: yes
If you are using a different email provider e.g., outlook or yahoo you can access its SMTP server settings online.
Setting up Node-Red for Email Alerts
We will use Node-Red to send emails. In order to get started with Node-Red on Raspberry Pi, refer to the guide:
To access Node-RED, we need the IP address of our Raspberry Pi and the port number on which Node-RED is accessible. By default, it starts on port 1880. Open any web browser and enter the RPI IP address followed by the port number.
Creating Flow to Send Email
This will open the Node-RED interface. You can start creating the flow.
Head over to Menu > Manage Palette > Install. Type node-red-node-email in the search bar and press enter. Install the Node-RED nodes to send and receive simple emails.
After the installation completes, the email nodes will be present in the palette as shown below:
Now drag an inject node and email node to the flow as shown below:
Now double click the inject node to edit its properties as shown below.
msg.payload holds the body of the email in the form of a string. Specify the contents that you want to send. ‘This is a test email sent from Node-RED’ is the email body that we want to send. Similarly, msg.topic is the email subject which we have set as ‘Node-Red Email Alert.’ Click the Done button when you have made the changes.
Now double click the email node and edit its properties as well.
Specify the recipient and sender email addresses. Moreover, enter the app password that we obtained previously for Node-RED to send emails. Click the Done button after making all the changes.
Wire the nodes as shown below:
Now deploy the changes by clicking the Deploy button found at the top.
Now click the left square on the inject node to send the email. You will receive a notification on the top showing that it was successfully injected.
Open the recipient’s email account. You will already have received the email from the sender account using Node-RED.
You may also like to read:
- ESP32 Send Emails (Plain text, HTML, and Attachments) through SMTP Server
- Arduino Send Email through IFTTT using Ethernet Shield
- ESP32-CAM Take and Send Photos via Email using an SMTP Server
- IoT Based Fire Detection using ESP32 and Flame Sensor with Email Alert
- ESP32 Fall Detection using MPU6050 with Email Alerts
- ESP8266 Send Emails (Plain text, HTML, and Attachments) through SMTP Server
- ESP32 IoT Motion Detection Web Server with Email Alert
- Send Email Alert Based on Temperature Threshold and Update Threshold value with ESP32 Web Server