Mercurial > prosody-modules
comparison mod_email/mod_email.lua @ 3762:74d7e59b3511
mod_email: Initial prototype module that allows other modules to send email
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 13 Dec 2019 12:46:44 +0000 |
| parents | |
| children | 070faeaf51bc |
comparison
equal
deleted
inserted
replaced
| 3761:0ae28bf0c546 | 3762:74d7e59b3511 |
|---|---|
| 1 module:set_global(); | |
| 2 | |
| 3 local moduleapi = require "core.moduleapi"; | |
| 4 | |
| 5 local smtp = require"socket.smtp"; | |
| 6 | |
| 7 local config = module:get_option("smtp", { origin = "prosody", exec = "sendmail" }); | |
| 8 | |
| 9 local function send_email(to, headers, content) | |
| 10 if type(headers) == "string" then -- subject | |
| 11 headers = { | |
| 12 Subject = headers; | |
| 13 From = config.origin; | |
| 14 }; | |
| 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 config.exec then | |
| 24 local pipe = io.popen(config.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 = config.user; password = config.password; | |
| 36 server = config.server; port = config.port; | |
| 37 domain = config.domain; | |
| 38 | |
| 39 from = config.origin; rcpt = to; | |
| 40 source = message; | |
| 41 }); | |
| 42 end | |
| 43 | |
| 44 assert(not moduleapi.send_email, "another email module is already loaded"); | |
| 45 function moduleapi:send_email(email) --luacheck: ignore 212/self | |
| 46 return send_email(email.to, email.headers or email.subject, email.body); | |
| 47 end |
