Sending Mail From Blockly

Blockly in OH3 makes it easy to develop basic rules. I have several rules that notify me of events via the OpenHAB mobile app. For some events, I also wanted to generate email notifications so I could expand the range of people getting alerts. The Mail Binding seemed overkill since I had already configured Linux mail when I set up the openhabian server and I could not figure out how to use the Mail Binding from Blockly.

I created mailOHalert.sh in /home/openhabian and made it executable. All arguments are sent in the body of the email to one or more target emails addresses.

#!/bin/sh
echo "$@" |  mail -s "OpenHAB Alert" "email1@domain1 <,email2@domain2>"

I added the script to /etc/openhab/misc/exec.whitelist:

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
/home/openhabian/mailOHalert.sh %2$s

I installed the Exec Binding and also the RegEx Transformation. Using the Main UI, I defined a Thing, provided a descriptive name, and set the Command to the fully qualified script name:

/home/openhabian/mailOHalert.sh %2$s

This automatically created five Channels: Output, Input, Exit Value, Running, Last Execution. I used “Add Equipment to Model” for later access.

Calling mailOHalert.sh from Blockly involves setting Input and Running:
image

The double-quotes around the Input argument prevents Linux syntax errors if the argument contains characters like parentheses.

Normally one would have published a Blockly Library to use an Action like that to the Marketplace. But it appears no one has published a library for the Mail binding yet. So for future readers, be sure to check the Marketplace first as it might have been published since this writing.

An alternative approach would be to use the inline script block.