mirror of
https://github.com/lxsang/antd-web-apps
synced 2024-12-27 09:58:20 +01:00
feat(blog): implement sendmail using smtp lua module
This commit is contained in:
parent
7ede87e962
commit
8fb8460e50
@ -19,24 +19,66 @@ end
|
||||
function ServiceController:sendmail()
|
||||
if not REQUEST.json then
|
||||
fail("unknown request")
|
||||
return
|
||||
end
|
||||
local setting = JSON.decodeFile(SMTP_SETTING)
|
||||
|
||||
local socket = require 'socket'
|
||||
local smtp = require 'socket.smtp'
|
||||
local ssl = require 'ssl'
|
||||
local https = require 'ssl.https'
|
||||
local ltn12 = require 'ltn12'
|
||||
|
||||
local sslCreate = function()
|
||||
local sock = socket.tcp()
|
||||
return setmetatable({
|
||||
connect = function(_, host, port)
|
||||
local r, e = sock:connect(host, port)
|
||||
if not r then return r, e end
|
||||
sock = ssl.wrap(sock, {mode='client', protocol='tlsv1_2'})
|
||||
return sock:dohandshake()
|
||||
end
|
||||
}, {
|
||||
__index = function(t,n)
|
||||
return function(_, ...)
|
||||
return sock[n](sock, ...)
|
||||
end
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
if not setting then
|
||||
fail("Dont know how to connect to SMTP server")
|
||||
return
|
||||
end
|
||||
local rq = (JSON.decodeString(REQUEST.json))
|
||||
local to = "mrsang@iohub.dev"
|
||||
local from = "From: " .. rq.email .. "\n"
|
||||
local suject = "Subject: " .. rq.subject .. "\n"
|
||||
local content = "Contact request from:" .. rq.name .. "\n Email: " .. rq.email .. "\n" .. rq.content .. "\n"
|
||||
|
||||
local cmd = 'echo "' .. utils.escape(from .. suject .. content) .. '"| sendmail ' .. to
|
||||
|
||||
--print(cmd)
|
||||
local r = os.execute(cmd)
|
||||
|
||||
if r then
|
||||
result(r)
|
||||
local to = "contact@iohub.dev"
|
||||
|
||||
local msg = {
|
||||
headers = {
|
||||
from = string.format("%s <%s>", rq.name, rq.email),
|
||||
to = string.format("Contact <%s>",to),
|
||||
subject = rq.subject
|
||||
},
|
||||
body = rq.content
|
||||
}
|
||||
LOG_INFO("Send mail on server %s user %s port %d: %s", setting.server, setting.user, setting.port, JSON.encode(msg))
|
||||
local ok, err = smtp.send {
|
||||
from = string.format("<%s>",rq.email),
|
||||
rcpt = string.format('<%s>', to),
|
||||
source = smtp.message(msg),
|
||||
user = setting.user,
|
||||
password = setting.password,
|
||||
server = setting.server,
|
||||
port = math.floor(setting.port),
|
||||
create = sslCreate
|
||||
}
|
||||
if not ok then
|
||||
fail(err)
|
||||
else
|
||||
fail("Cannot send email at the moment, the service may be down")
|
||||
result("Email sent")
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function ServiceController:subscribe()
|
||||
|
@ -9,6 +9,7 @@ enc = require("enc")
|
||||
WWW_ROOT = __ROOT__.."/blog"
|
||||
DB_LOC="/opt/www/databases"
|
||||
DB_FILE = DB_LOC.."/mrsang.db"
|
||||
SMTP_SETTING=DB_LOC.."/smtp.json"
|
||||
-- add aditional paths
|
||||
package.path = package.path..";"..WWW_ROOT .. '/?.lua'
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
<?lua if not HEADER.mobile then ?>
|
||||
<iframe width="980" height="410" src="https://mars.nasa.gov/layout/embed/send-your-name/future/certificate/?cn=792789419260" frameborder="0"></iframe>
|
||||
<!--iframe width="980" height="410" src="https://mars.nasa.gov/layout/embed/send-your-name/future/certificate/?cn=792789419260" frameborder="0"></iframe-->
|
||||
<?lua end ?>
|
||||
<?lua
|
||||
local datas = posts
|
||||
|
Loading…
Reference in New Issue
Block a user