src/notify/email.lua

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

mercurial