src/notify/email.lua

Tue, 09 Mar 2021 12:16:56 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 09 Mar 2021 12:16:56 +0000
changeset 0
6279a7d40ae7
permissions
-rw-r--r--

Initial commit

0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local smtp = require"socket.smtp";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local url = require"socket.url";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local uuid = require"util.uuid";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local function send_email(cfg, to, headers, content)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 if type(headers) == "string" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 headers = {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 Subject = headers;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 From = ('"%s" <%s>'):format(cfg.project.name, cfg.smtp.origin);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 };
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if not headers["Message-ID"] then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local namespace = url.parse(cfg.base_url or "http://localhost/").host;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 headers["Message-ID"] = ("<%s@%s>"):format(uuid.generate(), namespace);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 headers.To = to;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 headers["Content-Type"] = 'text/plain; charset="utf-8"';
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local message = smtp.message{
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 headers = headers;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 body = content;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 };
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if cfg.smtp.exec then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local pipe = io.popen(cfg.smtp.exec ..
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 " '"..to:gsub("'", "'\\''").."'", "w");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 for str in message do
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 pipe:write(str);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return pipe:close();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 return smtp.send({
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 user = cfg.smtp.user; password = cfg.smtp.password;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 server = cfg.smtp.server; port = cfg.smtp.port;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 domain = cfg.smtp.domain;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 from = cfg.smtp.origin; rcpt = to;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 source = message;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 });
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 send = send_email;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 };

mercurial