send_email_worker.lua

Thu, 19 Jan 2012 22:47:29 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 19 Jan 2012 22:47:29 +0000
changeset 0
a4670b074c77
permissions
-rw-r--r--

Initial commit

0
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 dofile(assert(arg[1], "No config file specified"));
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local config = assert(config, "No config!");
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local beanstalk = require "beanstalk".new();
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local smtp = require "socket.smtp";
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local json = require "json";
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local email_format = "%s <%s>";
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 beanstalk:connect();
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- A function that strips indentation and
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 -- replaces $variables with entries from the data table.
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function preprocess_message(data, message)
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local indentation = message:match("^%s*");
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 return (("\n"..message):gsub("\n"..indentation, "\n"):gsub("%$([%a_]+)", data):gsub("%s+$", ""));
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 repeat local job, err = beanstalk:reserve();
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not job then
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 print("EE", err);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 else
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local data = json.decode(job.data);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 assert(smtp.send {
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 from = email_format:format(config.from_name, config.from_address);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 rcpt = data.address;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 source = smtp.message {
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 headers = {
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 to = email_format:format(data.name or "", data.address);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 from = email_format:format(config.from_name, config.from_address);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 subject = config.subject;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 };
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 body = preprocess_message(data, config.message);
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 };
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 user = config.smtp_username;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 password = config.smtp_password;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 server = config.smtp_server;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 port = config.smtp_port;
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 });
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
a4670b074c77 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 until false;

mercurial