EMAIL NOTIFICATIONS

How to send email from my IoT Server?

IoT Server gives you power to create simple function. Those simple functions can be combined to create more complex functions and at the end full application. One of the function we will go through here is the e-mail alerts.

Pre-requisite

  • An IoT Server
  • A mail server

The dataflow

Here we have two ways to send email using an IoT Server:

  1. Using an email service provider such as mailjet
  2. Using the mail node and an email server (see the mail node documentation into Dataflow Manager)

In this article we will speak about the first one.

Request Creation:

As you can see in this example we are using Mailjet as an email service provider. Mailjet is expecting a payload containing the messages you want to send:

  • sender: from: {name: "a name", email: "name@mail.com"}
  • recipients: to: [{name: "name 1", email: "name1@mail.com"},{name: "name2", email: "name2@mail.com"}]
  • subject: subject: "a subject for my email"
  • Content in text format: TextPart: "this is the content of my email
  • Content in html format: HTMLPart: "this is <em> the content </em> of my email

Details about their API can be found here

const url = "https://api.mailjet.com/v3.1/send";
const basicUser = "*****"
const basicPwd = "*****";
const method= "POST";

/******************************
*** PREPARE MAILJET HEADER ***
******************************/
msg.headers = { 
    'Content-type': "application/json",
    'authorization': `Basic ${Buffer.from(basicUser+':'+basicPwd).toString('base64')}`
};
msg.url = url;
msg.method = method;

/******************************
*** PREPARE MAILJET CONTENT ***
******************************/

const sender = {name: "Braincube no-reply", email:"no-reply@braincube.com"};
const recipients = [{name: "First Name.", email:"first.name@braincube.com"}];
const subject = "An email using mailjet";
const contentText = "This email has beent sent using Mailjet";
const contentHTML = "This <em>email</em> has been sent using Mailjet";

msg.payload = {
    Messages: [
        {
            From: sender,
            To: recipients,
            Subject: subject,
            TextPart: contentText,
            HTMLPart: contentHTML
        }]
}

return msg;

Request sending:

Our https request node is running with a really light configuration has we made the choice to use the msg to send the URL and the method.

The output

Output of our HTTP REQUEST node is a JSON msg with some information such as:

  • statusCode: is my request sent, received and valid? Here we have a 200 so everything worked as expected
  • payload: some information sent by the server we contacted.
  • headers: Headers of the answer coming from our https request,
  • Others: others informations we have added in our msg before the https node

Implementation example

In the flow abose, the Alert application will output a message each time an alert is triggered and we are using this message to trigger our email Alerts. Here is an idea of how we can extract information coming from Alerts node to write our email.

const alertDefinition = msg.payload.alertDefinition;
const currentValue = msg.payload.triggerValue;
const alertDefinitionText = `Alert is: ${alertDefinition.condition.aggregator.count} ${alertDefinition.condition.aggregator.unit} ${alertDefinition.condition.aggregator.type} ${alertDefinition.condition.comparator} ${alertDefinition.condition.value}`;
const alertValueText = `Current value is: ${currentValue}`;
const name = msg.payload.name;
const extra = msg.payload.extra;

const subject = `Alert - ${name}`;
const recipients = [{name: "Matthieu H.", email: "matthieu.herwegh@braincube.com"}];
const content = ` ${alertDefinitionText}\n ${alertValueText}\n ${extra}`;

Was this article helpful?

Powered by Zendesk