Email with guaranteed delivery (to email server)

This flow can be passed messages suitable for passing to the node-red email node. It queues the messages and attempts to send them. If the email send fails (due to network failure for example) then the flow waits a bit and then tries again until it succeeds.

If persistent context is configured then the queue will be retained there so that the queue will survive a power down.

To use the flow paste it in and then configure the email node appropriately. Send it email messages via the Link In node. The messages should be in the format that the email node normally requires.

The wait time between attempts following a failure is set in the flow to 5 minutes (300000 ms). This can be adjusted by changing that value in the onTransition method in the dsm node. In addition a minimum time of 15 seconds is imposed between each successful email send (I did that so that in case of bugs it would not run away and send millions of emails). That time can also be adjusted in onTransition.

The maximum number of messages that will be queued at any one time can be set using Max Queue in the q-gate node.

The flow uses node-red-contrib-queue-gate which must be at least version 1.4.0 and node-red-contrib-dsm (tested with 0.14.1)

[{"id":"1ba759e3.65f436","type":"inject","z":"431bd56e.ffacd4","name":"","topic":"Start","payload":"","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":81,"y":331,"wires":[["3057226f.881216"]]},{"id":"a9c850f.1a6ba3","type":"e-mail","z":"431bd56e.ffacd4","server":"","port":"465","secure":true,"tls":false,"name":"","dname":"","x":130,"y":620,"wires":[]},{"id":"5dc696bd.162c28","type":"complete","z":"431bd56e.ffacd4","name":"","scope":["a9c850f.1a6ba3"],"uncaught":false,"x":154,"y":663,"wires":[["f03d6617.fd872"]]},{"id":"c95fda29.bc43","type":"catch","z":"431bd56e.ffacd4","name":"","scope":["a9c850f.1a6ba3"],"uncaught":false,"x":129,"y":708,"wires":[["5589ee8d.be1a18"]]},{"id":"90adc27.220d3c","type":"q-gate","z":"431bd56e.ffacd4","name":"","controlTopic":"control","defaultState":"queueing","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"","statusCmd":"","maxQueueLength":"100","keepNewest":false,"qToggle":false,"persist":true,"x":305,"y":192,"wires":[["a1b91a46.416f7"]]},{"id":"3057226f.881216","type":"dsm","z":"431bd56e.ffacd4","name":"email dsm","sm_config":"{\n    \"currentState\": \"Initialise\",\n    \"states\": {\n        \"Initialise\": {\n            \"Start\": \"Waiting_for_messages_in_queue\"\n        },\n        \"Waiting_for_messages_in_queue\": {\n            \"Messages_in_queue\": \"Waiting_for_message\"\n        },\n        \"Waiting_for_message\": {\n            \"Message\": \"Sending\"\n        },\n        \"Sending\": {\n            \"Success\": \"Dropping_from_queue\",\n            \"Failed\": \"Long_waiting\"\n        },\n        \"Dropping_from_queue\": {\n            \"Timeout\": \"Waiting_for_messages_in_queue\"\n        },\n        \"Long_waiting\": {\n            \"Timeout\": \"Waiting_for_messages_in_queue\"\n        }\n    },\n    \"data\": {\n        \"timeoutTime\": 15000\n    },\n    \"methods\": {\n        \"onTransition\": [\n            \"sm.data.timeoutTime = sm.currentState == 'Long_waiting' ? 300000 : 15000;\",\n            \"if (timeout.id) clearTimeout(timeout.id);\",\n            \"timeout.id = setTimeout(function() {\",\n                \"timeout.id = 0;\",\n                \"resume('Timeout', msg)\",\n            \"},sm.data.timeoutTime);\"\n        ],\n        \"status\": {\n            \"fill\": \"green\",\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.currentState;\"\n            }\n        }\n    }\n}\n","x":129,"y":415,"wires":[["b52108fa.7a3e48"]]},{"id":"b52108fa.7a3e48","type":"switch","z":"431bd56e.ffacd4","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Waiting_for_messages_in_queue","vt":"str"},{"t":"eq","v":"Waiting_for_message","vt":"str"},{"t":"eq","v":"Dropping_from_queue","vt":"str"},{"t":"eq","v":"Sending","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":288,"y":402,"wires":[["a04ef06f.965098"],["daa210c3.fba158"],["9b72907a.ce6c88"],["ac6cd74.19b1f28"]]},{"id":"daa210c3.fba158","type":"change","z":"431bd56e.ffacd4","name":"Peek oldest message","rules":[{"t":"set","p":"payload","pt":"msg","to":"peek","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":539,"y":332,"wires":[["90adc27.220d3c"]]},{"id":"a1b91a46.416f7","type":"change","z":"431bd56e.ffacd4","name":"Save and set topic","rules":[{"t":"move","p":"topic","pt":"msg","to":"topic_saved","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"Message","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":526,"y":178,"wires":[["3057226f.881216"]]},{"id":"ac6cd74.19b1f28","type":"change","z":"431bd56e.ffacd4","name":"Restore topic","rules":[{"t":"move","p":"topic_saved","pt":"msg","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":434,"wires":[["a9c850f.1a6ba3"]]},{"id":"9b72907a.ce6c88","type":"change","z":"431bd56e.ffacd4","name":"Drop oldest message","rules":[{"t":"set","p":"payload","pt":"msg","to":"drop","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":568,"y":367,"wires":[["90adc27.220d3c"]]},{"id":"5589ee8d.be1a18","type":"change","z":"431bd56e.ffacd4","name":"Failed","rules":[{"t":"set","p":"topic","pt":"msg","to":"Failed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":557,"y":689,"wires":[["3057226f.881216"]]},{"id":"f03d6617.fd872","type":"change","z":"431bd56e.ffacd4","name":"Success","rules":[{"t":"set","p":"topic","pt":"msg","to":"Success","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":376,"y":654,"wires":[["3057226f.881216"]]},{"id":"f1e66af9.19ebe8","type":"status","z":"431bd56e.ffacd4","name":"","scope":["90adc27.220d3c"],"x":299,"y":128,"wires":[["1535d1ac.b41a0e"]]},{"id":"1535d1ac.b41a0e","type":"function","z":"431bd56e.ffacd4","name":"Messages in queue?","func":"// expects msg.status.text in form \"queuing: <number>\"\n// sends \"Messages_in_queue\" if > 0 messages\nvalue = parseInt(msg.status.text.split(\":\")[1])\nif (value > 0) {\n    msg.topic = \"Messages_in_queue\"\n    msg.payload = value\n} else {\n    msg = null\n}\nreturn msg;","outputs":1,"noerr":0,"x":487,"y":128,"wires":[["3057226f.881216"]]},{"id":"a04ef06f.965098","type":"change","z":"431bd56e.ffacd4","name":"Messages in queue?","rules":[{"t":"set","p":"payload","pt":"msg","to":"status","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":498,"y":295,"wires":[["90adc27.220d3c"]]},{"id":"74ea2982.ff3a88","type":"link in","z":"431bd56e.ffacd4","name":"email in","links":["46853e4b.9446b8","6de1b815.ad0c48","76516f40.e992","57a5f572.28c6a4","ca8496f1.04e57"],"x":129,"y":73,"wires":[["90adc27.220d3c"]]},{"id":"8e511c39.d57ab8","type":"comment","z":"431bd56e.ffacd4","name":"Expects message in payload, subject in topic, recipient in msg.to","info":"","x":246,"y":32,"wires":[]}]

Flow Info

Created 4 months, 1 week ago
Rating: not yet rated

Owner

Node Types

Core
  • catch (x1)
  • change (x7)
  • comment (x1)
  • complete (x1)
  • function (x1)
  • inject (x1)
  • link in (x1)
  • status (x1)
  • switch (x1)
Other

Tags

  • email
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option