Send and Receive Email Using the Node-Red Email Node

email-node
The node-red email node most commonly used is the node-red-node-email which consists of three nodes.

  • A send node
  • receive node
  •  MTA (transfer node)





Using the Send Node

This is the most commonly used node as many flows will need to send an email when a particular event occurs  (email notifications).

The send node using SMTP (Simple Mail Transfer Protocol) to send email.

Today most email is sent encrypted.

The following screen shot show my set up using the email of my hosting provider.

email-send node

You can specify the recipient in the node set up or more commonly pass it in from a preceding node.

Common ports are 465,587,25. This is what the documentation says about the ports

Use secure connection – If enabled the connection will use TLS when connecting to server. If disabled then TLS is used if server supports the STARTTLS extension. In most cases set this to enabled if you are connecting to port 465. For port 587 or 25 keep it disabled.

You can get the settings from your email provider. Here are mine:

email-settings-smtp

Incoming Parameters

The node accepts several parameters the most commonly used are:

  • msg.payload=email body can contain HTML
  • msg.topic=email subject
  • msg.cc =email cc
  • msg.bcc =email bcc
  • msg.from =the from address
  • msg.attachments = message attachments in nodemailer format

To use attachments you need to pass an array of objects as shown below:

msg.attachments=[{path:filename}];

example
msg.attachments=[{path:”/home/steve/.node-red/will/invoices/invoices/invoice-001.html”}];

Catching Send Errors

You need to use the catch node to catch email send errors.

send-errors

The error property of the message will contain the error messages as shown below.

catch-email-error

Receiving Emails- Email Mailboxes

A mailbox is a storage area were emails are stored.

A mailbox is independent of the protocol and client used to access it .

This means that multiple clients can access the same email mailbox and it can be accessed using IMAP$ or POP3 or both at the same time.

Receiving Emails IMAP

The receive node will receive email from POP3 and IMAP4 mailboxes.

IMAP4 is the more modern protocol and the one most commonly used.

It was designed so that email messages would be stored  on the email server and not on the email client.

You select it from the protocol setting

mail-protocol

As with SMTP this is usually done over an encrypted connection (SSL).

You will need to set the email server and port the details you can get from your email provider.

The settings for Ionos are shown below:

email-settings-imap4-ionos

The settings tab for the node are shown below. This changes slightly for POP3.

email-receive-node

The default setting for the node is to check for new mail every 300 seconds and to mark the mail as read.

This means that when you check again in mins you only see mail that has arrived since you last checked.

You can change this behaviour using the deposition and criteria settings.

In this mode there is no node input.

If you set the get mail property to triggered you get an input terminal and you can modify various settings. the main inputs are:

msg.criteria

Node outputs are

msg.payload =plain text msg
msg.html=html if message is an html message or may be absent
msg.topic=subject
msg.date=email date
msg.from – address of message sender.
msg.header =addition information about the message

An example message is shown below:

email-message
IMAP Folders

An IMAP mailbox can contain many folders.

Some are standard folders like INBOX and others depend on the mail service provider.

Receiving Emails POP3

POP3 is the older email protocol and was designed so that email messages would be stored on the email client and not on the email server.

The default behaviour is to download the messages from the server and delete them on the server.

Most email clients offer the ability to leave them on the server but the email node doesn’t.

So the first time you connect the node-red email node to a maibox using POP3 it will delete all of the emails in the  INBOX of that mailbox

Most email providers use different domain names for the IMAP and POp3 Email server and so you will need change this as well as the port. The settings for Ionos are shown below:

POP3-settings-ionos

What is StartTLS

Opportunistic TLS (Transport Layer Security) refers to extensions in plain text communication protocols, which offer a way to upgrade a plain text connection to an encrypted (TLS or SSL) connection instead of using a separate port for encrypted communication. Several protocols use a command named “STARTTLS” for this purpose.  –Wiki

Using Google Gmail

The email node is commonly used with Gmail and was the way I originally used it.

However Google have tightened their email security for IMAP4 clients and it has stopped working.

There is a work around here but I think you are probably better off choosing another email provider that uses pop3, IMAP4 and SMTP.

Google Gmail does provide support for these protocols but it is primarily an http (web based) based email service.

Common Tasks

Applications often send alert notifications via email and so a common task is to parse the incoming message to decode it.

I have also worked on flows were meter readings have been sent via email and it was necessary to extract the address, reference and reading from the email.

If you would like me to do some examples then leave a note in the comments section

Video-How to Send Email and SMS Notifications



Related Tutorials and Resources

Click to rate this post!
[Total: 0 Average: 0]

4 comments

  1. Hi Steve,
    Thanks for your great work here.
    I downloaded your example flow and got it working aok .. however I get the following error ..

    Warning: msg properties can no longer override set node properties. See bit.ly/nr-override-msg-props

    Any thoughts?

    1. Bob
      Nothing to worry about it just means that you need to only set things in the node or in the incoming message and not in both locations.
      Does that make sense.
      Rgds
      Steve

  2. Sorry, that should be manage Google account>Security>Signing inti Google>App passwords.
    after you use the temp password you will get an email with a link to verify.

    Yahoo is similar.

  3. You have to authorize new apps in your Google account >data-and-privacy. Need to get a one time password, then it works like it used to.

Leave a Reply

Your email address will not be published. Required fields are marked *