From 8fb8460e502081d739355865b5d2b61294666c8a Mon Sep 17 00:00:00 2001 From: Dany LE Date: Fri, 26 Jul 2024 20:19:44 +0200 Subject: [PATCH] feat(blog): implement sendmail using smtp lua module --- blog/controllers/ServiceController.lua | 70 ++++++++++++++++++++------ blog/router.lua | 1 + blog/views/default/post/posts.ls | 2 +- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/blog/controllers/ServiceController.lua b/blog/controllers/ServiceController.lua index 0d32fb2..9b2dd9e 100644 --- a/blog/controllers/ServiceController.lua +++ b/blog/controllers/ServiceController.lua @@ -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() diff --git a/blog/router.lua b/blog/router.lua index 691517c..5028f7d 100644 --- a/blog/router.lua +++ b/blog/router.lua @@ -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' diff --git a/blog/views/default/post/posts.ls b/blog/views/default/post/posts.ls index c5222d2..e24c4d2 100644 --- a/blog/views/default/post/posts.ls +++ b/blog/views/default/post/posts.ls @@ -1,5 +1,5 @@ - +