Sending Emails from Node-RED: A Step-by-Step Guide
Introduction
In this article, we’ll explore how to send emails from Node-RED, a powerful flow-based programming tool. Despite the rise of various communication channels, emails remain popular for their reliability and versatility. We’ll focus on using a Google account to send emails, but note that other account types can also be used with a similar process. So, let’s dive in and learn how to integrate email functionality into your Node-RED projects!
Creating a Google/Gmail Account
To get started, we need to create a new Google account specifically for sending emails. While you can use your existing email account, it’s recommended to have a separate account for security purposes. This is because we’ll be generating an app password, which can slightly compromise the security of your account due to additional entry points. Let’s go through the account creation process:
- Visit https://google.com/account/about and click on “Create an account.”
- You have the option to use your own email address or create a new Gmail account.
- Follow the prompts, entering your date of birth, gender, and personalization settings as desired.
- Once your new Google account is set up, enable multi-factor authentication (MFA) for added security. Go to the “Security” section and then enable “Two-step verification.”
Creating a Google App Password
Now that MFA is enabled, we can create an app password, which will allow Node-RED to send emails on behalf of your Google account. Follow these steps:
- Log in to your Google account and go to the “App Passwords” section. If you don’t see the option then go to this link: myaccount.google.com/apppasswords
- Click on “Create a new app” and provide a custom name. Let’s name it “Node-RED.”
- A unique app password will be generated. Make sure to save it securely, as you’ll need it later.
Setting Up Node-RED
Now that we have our Google account and app password ready, let’s configure Node-RED to send emails. Here’s what you need to do:
- Install the “node-red-node-email” palette in Node-RED. Go to the hamburger menu, select “Manage palette,” and search for “node-red-node-email.” Install the top result, which should be the most recent version.
- Once installed, you’ll find the email node on the left side of the Node-RED interface.
- Drag the email node onto the workspace and add an inject node to trigger the email sending process.
- Double-click on the email node to open its configuration. Enter the GMail SMTP server and port settings, along with your email address for the username and the app password generated earlier for the password.
- Now, connect the inject node to the email node.
- To send an email, you’ll need to provide a payload with the email message and subject. You can find an example in the info panel of the inject node. Modify the payload and topic to your desired content.
GMail SMTP Settings:
SMTP Server: smtp.gmail.com
SMTP Username: Complete Gmail address
SMTP Password: App password
SMTP port (SSL): 465
Use Secure Connection: Ticked
Sending a Test Email
With the basic setup complete, let’s send a test email to verify everything is working correctly. Follow these steps:
- Configure the inject node to include the desired payload and topic for your test email.
- Deploy the flow.
- Click the inject node to trigger the email sending process.
- Check your Gmail inbox to confirm if the test email has been received.
Creating a Useful Example: Sending Emails Based on Sensor Data
Now that we’ve successfully sent a test email, let’s explore a more practical example that involves sending emails based on sensor data changes. For instance, we’ll send an email when a house alarm is disarmed. Here’s how to set it up:
- Use a Home Assistant “State Changed” node in Node-RED to monitor changes in a specific entity (in this case, the house alarm).
- Add a “Change Node” and configure the node so that the message payload and topic contains the email subject and body of the email
- Connect the change node to the send email node (you can reuse the send email node from the previous example).
- Deploy the flow.
- Arm and disarm the house alarm to trigger an email notification.
Conclusion
Congratulations! You’ve learned how to send emails from Node-RED using a Google account. We started by creating a dedicated Google account and generating an app password for secure email sending. Then, we configured Node-RED by installing the email node, setting up the email node’s credentials, and connecting it with other nodes to send emails. Finally, we explored a practical example of sending emails based on sensor data changes.
By integrating email functionality into your Node-RED projects, you can enhance your automation and notification systems. We hope this guide has been informative, and we’re excited to see the creative ways you incorporate email notifications into your own projects. If you have any questions or ideas, please share them in the comments.
Hello! I’m at work surfing around your blog from
my new iphone! Just wanted to say I love reading your blog and look forward to all your
posts! Keep up the superb work!