I am looking to send Node-Red an Email to turn something on

How can i separate out the fields of an Email msg?
Re: Message from Node-RED : msg : Object

object

payload: "Test↵"

topic: "Re: Message from Node-RED"

date: "2020-06-20T10:58:44.000Z"

header: object

from: "XXXXX"

attachments: array[0]

_msgid: "2f16fe05.6e3a72"

The msg is an 'object' so msg is everythig. f you want to look at the payload you would use 'msg.payload., for 'topic' use msg.topic.

I think you would benefit from reading about objects

Hi Robert, welcome to the forum!
Using emails to control a node-RED flow can be a bit clunky, have you considered using push messages, such as Pushover, Telegram, Pusher, Pushbullet, etc?
Push messages seem very reliable and easy to integrate into node-RED.
However, your choice.

I actually found a pretty good use case for receiving emails. I've got various cheap network cameras and would like to receive a Pushover notification or possibly create a log entry when one of them detects motion.

The only common denominator between the cameras is sending email (or possibly to FTP the recordings). As in this case I'm only interested in the events, email seemed more straightforward. But to my surprise I couldn't find any properly working "smtp in" node. The usage would be local network only and it would be enough for Node-RED to just receive the emails as msg payloads. Sadly there was no such node. I was able to hack together a POC as a Node.js script but haven't yet looked into the feasibility of making it a contrib node.

Edit to clarify: the idea would be to use Node-RED to act as an open SMTP server and just create a msg object from any email sent through it.

You certainly wont find one of those since that is the wrong protocol. SMTP is a mail delivery protocol and so Node-RED would need to act as a mail server in order to be able to receive via that. Of course, that would be massively overkill.

What you really want is an IMAP or POP3 node.

https://flows.nodered.org/search?term=imap

Both protocols are able to retrieve emails from a mailbox on a mail server. IMAP is the one to use if possible since it is the most robust and gives you a lot more control.

Alternatively, if GMail is your thing, there is a node for that as well.

Nope I know what I'm talking about as I've run my own SMTP server at some point. The idea would be to allow the cameras to use a local "SMTP server" to send motion detection notification. The Node-RED node (or a Node.js script) would pretend to be an SMTP server but would just pass the sent data as a msg. Something like this: GitHub - kost/smtp2mqtt: Simple SMTP to MQTT relay/forwarder

Nothing stopping you from creating an SMTP send node then :smile:

Personally, I'd be happy to have a better IMAP node that uses an up-to-date library with all of the IMAP extensions. such as push.

However, there is no getting away from the fact that SMTP is a mail delivery protocol, yes you can send an email using it of course but having a local SMTP server is the only realistic scenario since many residential Internet and most enterprise network connections will block SMTP traffic. That is done - supposedly - to prevent spam bots from springing up everywhere.

1 Like

Although it may sound like an odd approach, I see it in reality to most straightforward. I don't want to setup the cameras to send an actual email to for example my Gmail box and then setup Node-RED to listen to it when I only need the camera to communicate with Node-RED. There's less latency and from there I can decide to send a push notification or just log/graph the detections.

I understand the use-case but it is a long way from the original question I think & your case is very much an edge-case in a modern setup since there are now so many ways for systems to communicate "locally". Indeed, it is unfortunate that modern low-cost camera's don't come with the ability to talk MQTT directly for notifications :smile:

2 Likes

I believe sometimes off topic conversations need to happen when something triggers a thought that's fun to discuss while it might not warrant a separate topic. If the OP was actively continuing the discussion, then it'd be appropriate to stay in topic. :slightly_smiling_face:

... so does that smtp2mqtt just work then ? - why not just use that ? (and mqtt of course :slight_smile:

3 Likes

Not yet tried this one actually. I just realised it's not a similarly named Python based solution I tried which had a lot of bugs, then proceeded to write a Node.js prototype using smtp-server and mailparser. With that setup I started getting ambitious to also try to get attachments as Buffers. :grin: It's still work in progress, I've got more pressing cottage projects to handle first.

For those curious - it turned out to be a lot easier than I expected. :upside_down_face:

Function node code (note that I'm using node-red-contrib-function-npm) :

const {SMTPServer} = require('smtp-server');
const {simpleParser} = require('mailparser');

const server = new SMTPServer({
  secure: false,
  logger: false,
  disabledCommands: ['AUTH', 'STARTTLS'],

  onData: function (stream, session, callback) {
    simpleParser(stream, {
      skipTextToHtml: true,
      skipTextLinks: true
    }).then(parsed => {
        node.send({payload: parsed});
      })
      .catch(err => {})
      .finally(callback);
  }
});

server.listen(1825);

return null;

2 Likes

Nice setup, useful with a local smtp server for several purposes I think, got this up running
Picture is shown in my configuration with this setting in the image viewer (if someone is still struggling?)

Yep same settings! This was off topic though so I doubt nobody was struggling. :smile:

Just as an aside. If you are regularly writing code like this, you can avoid an extra node by attaching require itself to your global variables in settings.js and then doing const require = global.get('require') at the top of your function.

Needless to say, either method is dangerous from a security perspective and shouldn't generally be used in production.


Certainly interesting though and gives me some ideas for enhancing my "new" home server.

If you want a more full-featured SMTP server, you might also look at https://haraka.github.io/ but of course, we are drifting off-topic again now (well I am!).

Would it not be possible to install smtp-server and mailparser once and then in settings.js add something like

    functionGlobalContext: {
        smtpsrvr:require('smtp-server'),
        mailparser:require('mailparser')
    },

And then the code could be written in a standard function node instead. Or not possible?

Yes, that is perfectly possible. And that is what I'd do for something that was more "live" than "dev". On my dev instance, I simply put require in to save faffing with settings.js :smile:

If doing it that way, you have to change your function node code to get the module from the global variable instead of using require of course.

Such things should still be used sparingly if you have multiple people who may edit your flows. But if it is just you at home and you don't connect the Editor to the Internet, all should be OK in any case.

Just a final note that not all node.js packages like being handled that way, you might occasionally come across one that doesn't work. Most seem to though.

Thanks. I am aware but I like to be able to quickly prototype things. The beauty of this contrib node is that it fetches the requirements on the fly on each deploy/startup so it's very easy to search stuff from npm and try them out. Unused dependencies will also automatically get cleaned up. This way I can skip testing first on a Node.js script or restarting.

I only access Node-RED locally/through VPN but I wonder how big of a security risk this is while still having the exec node around? I definitely wouldn't be using it if this was a production system (in work life). For my personal use I want to be able to hack things up quickly when I get the inspiration (much more rare than could be imagined based on my forum activity).

Agreed. I also use require on my dev machine & of course, the comment was aimed at people moving from local to production/enterprise installs as such things are easily overlooked if you are not an IT person. Plenty of other security traps in Node-RED too that can catch out the unwary. That's of course because Node-RED is such a powerful platform. Not a criticism.

It is possible to exclude nodes for production use and exec would be a prime candidate.