Email Notifications Driver using Node/sendmail on a PI

Not sure if people are interested but just created a very crude driver & Node App that allows you to send emails via a notification driver and a server running Node & sendmail.

The driver just sends a simple REST post call to the Node http://[your ip]:3000/email which parses the text and uses sendmail to send out.

I am using this in place of SMS alerts for now - US Cell providers have an email to txt message email address for each phone #.

Again this is very crude and unsecure. Will be looking to add stuff in the future.

****** UPDATE 11/28/18 ******
thanks to @Ryan780's suggestion added ability to set "To" and "Subject" fields in the device preferences. Then when setting a notification the device will simply use the notification text as the body of the email IF the text does not start with a "{" otherwise it will work as previously stated. Also fixed some over exuberant error logging.

2 Likes

Word of caution - I have not really done any extensive troubleshooting so be careful.

Have you tried using Pushover instead? It works great for me and doesn't require a man in the middle solution.

In any case, thanks for your contribution. Options are always great.

So I did not want to use an external service - kind of gets away from that whole local control thing. I used the pushover driver as a template. Also since I have stopped using WC I had a PI laying around!

1 Like

Ahh very true.
Maybe you should link up with @andrew.d.smith.2.
He has a post with a correlated needs/features but is pushing out SMS instead. I'm sure others would appreciate a combined local SMS and Email notification system.

2 Likes

Thanks for that link. Interesting failover solution..

My little Node server can be expanded - am looking at maybe adding a local DB for message storage/retrieval, adding some sort of security, other devices etc. Maybe even an HE app to manage things. Lots of ways to go with this..

At any rate the code is very basic right now..which is probably a good thing in terms of system impact.

Definitely going to give it a shot. I'm always worried about dropped SMSs since I fly every week. I almost never lose a text anymore but it has happened. Would be nice to have a FREE backup solution rather than paying for pushover. I'll let you know if I get it working. THANKS!! :+1:

Let me know. Happy to correct if you find something. Also you can have multiple recipients so you can email your phone and another address...

On the PI (or Linux server) make sure sendmail is installed as well as a recent version of Node.

Yeah...no idea how to get that working and not going to hunt and peck for a solution for hours. If you have a good how-to you can point me to then I'll give it a try. I have send mail installed but figuring out how to be able to send from another PC to the Pi running sendmail...sorry, that's over my head.

So you just get Node running on your server/pi then install the custom driver on the hubitat, add the virtual device and give it the url: http://[server's ip address]/email + a "from user"

Then when using the device for notifications simply put it in the form I listed.

Also on the server you can test sendmail via the command line to see if it works.

Okay...the node JS server is configured for the specific port then. I gotcha.
But where do I input who the email is supposed to go to? That hasn't gone anywhere yet. Also, I get the following error:

`dev:7882018-11-28 12:02:27.745 am errorgroovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is 'T' with an int value of 84
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
This is a test.
^ on line 52 (deviceNotification)`

What am I supposed to put for from? An email address? Name?

In the notification text of the rule/app you are using.. just add text like this:

{"To":"person1@someemail.com","Subject":"This is a Subject","Text":"This is the body of the email"}

The DTH converts the text string to a list/map (JsonSlurper!) that I am then able to parse for the relevant fields I need to send the email. Caution - for now the "To/Subject/Text" are case sensitive and required.

I did the input this way so you can use that one device for different email notifications.. thought it might be better than having multiple devices..

Also it turns out for things like the leak alert in HSM I can embed the variables as well like this:

{"To":"erktrek@someemail.com","Subject":"%device% is %value%","Text":"First detected: %date% %time%"}

1 Like

That really isn't very practical though. That means that you have to use this format in the rule or in HSM to send the notification. That means that if I am using two methods of notification, that this is going to have to be put in for that as well. That's not going to work for me unfortunately. Sorry. Has to be just the text I want sent in the email because I am also sending a Join notification from HSM. As I said, this is the backup.

I tried that, and this is the new error I receive.

dev:7882018-11-28 09:37:25.883 am errorProblem parsing notification text: {"To":"ryancasler@gmail.com","Subject":"This is a Subject","Text":"This is the body of the email"}, should be in JSON format with To, Subject, Text elements defined.

However, I did receive an email. But it was not from the user defined in the device. I would prefer if the device notification text was what was put into the email and the recipient and subject were defined in the preferences for the device. Until then, I won't be able to really use it.

So I just went ahead and updated the driver, it now tests for the first char. If that char is a "{" it will function as stated. If it is not then it will use the To/From/Subject fields defined in preferences and the message itself will be the message.

I did this in a rush so ymmv - will be back later to check out. Also I have been getting errors as well sometimes but everything goes through. Will look into that.

Took a sec for me to push update all good now..

That worked! I still get this error:

[dev:788](http://192.168.1.12/logs#dev788)2018-11-28 10:28:46.486 am [error](http://192.168.1.12/device/edit/788)Problem parsing notification text: This is a test email., should be in JSON format with To, Subject, Text elements defined.

[dev:788](http://192.168.1.12/logs#dev788)2018-11-28 10:28:46.470 am [error](http://192.168.1.12/device/edit/788)OK

But everything still worked correctly.

Thats great! Will look into the errors a little later.

Yeah, i'm not worried about them, since it worked. Looks more like a logging problem than an actual error.