src/notify/email.lua

changeset 0
6279a7d40ae7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/notify/email.lua	Tue Mar 09 12:16:56 2021 +0000
@@ -0,0 +1,46 @@
+local smtp = require"socket.smtp";
+local url = require"socket.url";
+local uuid = require"util.uuid";
+
+local function send_email(cfg, to, headers, content)
+	if type(headers) == "string" then
+		headers = {
+			Subject = headers;
+			From = ('"%s" <%s>'):format(cfg.project.name, cfg.smtp.origin);
+		};
+	end
+	if not headers["Message-ID"] then
+		local namespace = url.parse(cfg.base_url or "http://localhost/").host;
+		headers["Message-ID"] = ("<%s@%s>"):format(uuid.generate(), namespace);
+	end
+	headers.To = to;
+	headers["Content-Type"] = 'text/plain; charset="utf-8"';
+	local message = smtp.message{
+		headers = headers;
+		body = content;
+	};
+
+	if cfg.smtp.exec then
+		local pipe = io.popen(cfg.smtp.exec ..
+			" '"..to:gsub("'", "'\\''").."'", "w");
+
+		for str in message do
+			pipe:write(str);
+		end
+
+		return pipe:close();
+	end
+
+	return smtp.send({
+		user = cfg.smtp.user; password = cfg.smtp.password;
+		server = cfg.smtp.server; port = cfg.smtp.port;
+		domain = cfg.smtp.domain;
+
+		from = cfg.smtp.origin; rcpt = to;
+		source = message;
+	});
+end
+
+return {
+	send = send_email;
+};

mercurial