Message-Driven Workflow – In Action!

Tom Mairs
Jun. 25, 2012 by Tom Mairs

Twenty years ago, there was only one web browser and it was actually called the “World Wide Web” browser. There was only one search engine and its name was Archie. About that same time I remember reading about the research team at the Cambridge University’s computer lab where they had a non-computer problem to solve – the department coffee pot was always empty when they wanted a cup.

Being computer science geeks, they of course, found a computer science solution to the problem – rig up a camera and network it so they could always see when the coffee pot was full. That way they could make sure there was going to be coffee in the pot before they made the journey down the hall to fill a cup. This is how the first web cam was born.

What does this have to do with email? When I was thinking of radical, esoteric ways to show how email can be used to automate external processes, guess what I thought of first? Yes, that’s right – coffee. In practical terms, my coffee pot is a flight of stairs away and, like the computer geeks at Cambridge, I too would like to know that there is actually coffee in it before I make the trek. So let’s do this: let’s create a message-driven workflow that will bring some certainty and clarity to the coffee situation in my office.

Getting started:

What we need to make this work is an understanding of the Lua language – don’t worry, I’ll walk you through it. One of the things the Momentum software is very good at is processing mail – accepting messages, doing something with them, and then responding to / delivering messages. It is the “do something” part that we’re going to concentrate on here. In this case we are going to use Momentum’s embedded Lua script engine to check the incoming mail and read the subject line. If the subject line says “Show My Coffee Pot” then we want Momentum to do several things: a) get the current image from my “coffee cam” b) generate a reply email to the sender with c) an image of the coffee pot in it. The web cam watching the coffee pot captures an image every 30 seconds and saves it as a file on the server, so we can just attach that file to the mail and it will always be the current image.

Here’s the Lua script that will enable Momentum to perform our coffee pot check:

— ############################################### —

— load Message Systems helper extensions




local mod = {};


function mod:validate_data(msg, str, accept, vctx)

— define the variable tables we will need for the message components

local ctx = { ec_message = msg };

local headers = {};

local pparts = {};

local attachments = {};

local subject = msg:header(“subject”);


if string.lower(subject[1]) == “show my coffee pot” then

— create a message container and define mailfrom and rcptto

local imsg = msys.core.ec_message_new(now);

local imailfrom = “[email protected]”;

local ircptto = msg:mailfrom();


— define the headers and message parts

headers[“To”] = ircptto;

headers[“From”] = “[email protected]”;

headers[“Subject”] = “Current image from CoffeeCam”;

pparts[“text/plain; charset=utf8”] = “The CofeeCam image is attached.”;

pparts[“text/html”] = “<b>! CoffeeCam !</b> <br /> Latest image is attached.”;

local files1={};

files1[“type”] = “image/jpg”;

files1[“name”] = “coffee.jpg”;

— load the webcam image to the attachments table

local file_name = “/opt/msys/ecelerity/etc/conf/default/lua/coffee.jpg”;

local io = msys.core.io_wrapper_open(file_name, msys.core.O_RDONLY, 0666)

files1[“content”] = io;

files1[“disposition”] = “inline”;

attachments[1] = files1;


— build and send

imsg:build(headers, pparts, attachments);

imsg:inject(imailfrom, ircptto);


return msys.core.VALIDATE_CONT;



msys.registerModule(“coffee”, mod);

— ############################################### —

Toggle Comments »

The above script simply needs to be applied to a current version of Momentum and it will work. Don’t have Momentum? No worries – you can see the result in our demonstration system by sending an email to [email protected] with the subject “Show My Coffee Pot” and the system will send you back the last available image of the coffee pot. Please note that the script running on this demo system may not be here forever. If it is ever disabled, I will try to remember to update this blog.

So what does this prove? Well, admittedly, the coffee pot example is frivolous and not exactly something to base a business model on, but imagine what you can do in your business if you can enable other processes based on message activity. In this case, we automatically send an image back to an email address based on a matching subject line, but what if we send back an image from a security camera based on receiving an SMS and a security code? Or maybe we could generate an alert message to the system administrator if a particular set of key words were in the body of a message. How about automatically printing off a paper report summarizing mail activity for every 1 million messages delivered? Or getting your bank balance texted to you?

The uses for this kind of automation are endless, and any of the above scenarios would only require minor tweaks to the basic code above. If this sounds like something your business could leverage, I’d be happy to set up a meeting for you with the appropriate people at Message Systems.

(Editor’s note: Please note that Tom’s live coffee cam might not be online 24×7, but emailing to [email protected] should still return the latest image on the server as outlined above.)


  • Tom – how widely used is Lua at this point? Is Sieve++ still seen as the standard language in the messaging industry, or is that changing?

  • John, Lua is the primary scripting engine in Momentum version 3.x but we do still support Sieve++ as well. The power in Lua is quite compelling and I recommend migrating any existing Sieve scripts to Lua, even if it is just for the ease of managing one script versus several. I would say that the vast majority of our customers who are using version 3.x of Momentum are using Lua in some way.

    Sieve is used primarily as a server-side tool in IMAP servers like Dovecot to help end users build sorting rules and spam filters. I don’t see that kind of use going away any time soon, but that is very elementary functionality IE: “If this is spam discard” . Lua offers a whole new dimension of control that is unthinkable in Sieve. The above code is a great example of something that would be extremely difficult, if not impossible in Sieve.

    We have encouraged everyone upgrading from version 2.x to consider rewriting their Sieve scripts to Lua and most have done so.

  • I’d like to point out that this was built as an experiment and offered as an example only. If you plan to use something like this in production, it should be vetted in a development system first. In particular, this sample does not do any field validation and probably should be run in the data_spool phase as opposed to the data phase for improved reliability.

Related Content

Building an Email Archiving System: Searching and Displaying the Data - Part 4

In the fourth installment of his blog series on building an email archive, Senior Messaging Engineer, Jeff Goldstein explains phase one of the Archive UI.

read more

Sending Personal Health Information (PHI) with SparkPost

Read up on how healthcare organizations might consider sending Personal Health Information (PHI/ePHI) to maintain HIPPA compliance.

read more

What Does It Take to Send Billions of Emails?

High-volume email can be mind-boggling to someone who isn’t steeped in the business. Here’s some real-world advice for tuning Momentum email infrastructure.

read more

Get started and start sending

Try SparkPost and see how easy it is to deliver your app’s email on time and to the inbox.

Try Now

Send this to a friend