Blogger: use luasocket for mail sending

This commit is contained in:
DanyLE 2023-02-24 18:41:36 +01:00
parent 5dec0a2b56
commit 6354c48680
7 changed files with 100 additions and 66 deletions

View File

@ -1,30 +1,42 @@
local data = ... local data = ...
-- print(data.content) -- load the smtp support
local smtp = require("socket.smtp")
local from = string.format("<%s@iohub.dev>", data.user);
local mesgt = {
headers = {
from = string.format("Dany <%s@iohub.dev>", data.user),
to = "",
subject = data.title
},
body = data.content
}
local error_msg = {} local error_msg = {}
local iserror = false local iserror = false
local tmp_name = "/tmp/"..os.time(os.date("!*t"))
local file = io.open (tmp_name , "w") for k,v in pairs(data.to) do
if file then LOG_DEBUG("Send email to:"..v.email)
file:write("From: mrsang@lxsang.me\n") local rcpt = string.format("<%s>",v.email)
file:write("Subject: " .. data.title .. "\n") mesgt.headers.to = string.format("%s <%s>",v.text, v.email)
file:write( data.content.."\n") local r, e = smtp.send{
file:close() from = from,
for k,v in pairs(data.to) do rcpt = rcpt,
print("sent to:"..v) server = "iohub.dev",
local to = v domain = "iohub.dev",
local cmd = 'cat ' ..tmp_name .. '| sendmail ' .. to user = data.user,
--print(cmd) password = data.password,
local r = os.execute(cmd) source = smtp.message(mesgt)
if not r then }
iserror = true
table.insert(error_msg, v) local r = os.execute(cmd)
print("Unable to send mail to: "..v) if not r then
end iserror = true
table.insert(error_msg, v.email)
LOG_ERROR(string.format("Unable to send mail to %s: %s",v.email, e))
end end
else
iserror = true
table.insert(error_msg, "Cannot create mail file")
end end
local result = {} local result = {}
result.error = iserror result.error = iserror

File diff suppressed because one or more lines are too long

View File

@ -28,6 +28,8 @@
"Location": "Location", "Location": "Location",
"From": "From", "From": "From",
"Save": "Save", "Save": "Save",
"Content": "Content",
"IO Hub mail username/password": "IO Hub mail username/password",
"Send": "Send", "Send": "Send",
"Please select a parent category": "Please select a parent category", "Please select a parent category": "Please select a parent category",
"Please enter category name": "Please enter category name", "Please enter category name": "Please enter category name",
@ -36,6 +38,7 @@
"No email selected": "No email selected", "No email selected": "No email selected",
"Unable to send mail to: {0}": "Unable to send mail to: {0}", "Unable to send mail to: {0}": "Unable to send mail to: {0}",
"Error sending mail: {0}": "Error sending mail: {0}", "Error sending mail: {0}": "Error sending mail: {0}",
"Open/Create database": "Open/Create database",
"Open/create new database": "Open/create new database", "Open/create new database": "Open/create new database",
"Unable to init database file: {0}": "Unable to init database file: {0}", "Unable to init database file: {0}": "Unable to init database file: {0}",
"Select image file": "Select image file", "Select image file": "Select image file",

View File

@ -1,30 +1,42 @@
local data = ... local data = ...
-- print(data.content) -- load the smtp support
local smtp = require("socket.smtp")
local from = string.format("<%s@iohub.dev>", data.user);
local mesgt = {
headers = {
from = string.format("Dany <%s@iohub.dev>", data.user),
to = "",
subject = data.title
},
body = data.content
}
local error_msg = {} local error_msg = {}
local iserror = false local iserror = false
local tmp_name = "/tmp/"..os.time(os.date("!*t"))
local file = io.open (tmp_name , "w") for k,v in pairs(data.to) do
if file then LOG_DEBUG("Send email to:"..v.email)
file:write("From: mrsang@lxsang.me\n") local rcpt = string.format("<%s>",v.email)
file:write("Subject: " .. data.title .. "\n") mesgt.headers.to = string.format("%s <%s>",v.text, v.email)
file:write( data.content.."\n") local r, e = smtp.send{
file:close() from = from,
for k,v in pairs(data.to) do rcpt = rcpt,
print("sent to:"..v) server = "iohub.dev",
local to = v domain = "iohub.dev",
local cmd = 'cat ' ..tmp_name .. '| sendmail ' .. to user = data.user,
--print(cmd) password = data.password,
local r = os.execute(cmd) source = smtp.message(mesgt)
if not r then }
iserror = true
table.insert(error_msg, v) local r = os.execute(cmd)
print("Unable to send mail to: "..v) if not r then
end iserror = true
table.insert(error_msg, v.email)
LOG_ERROR(string.format("Unable to send mail to %s: %s",v.email, e))
end end
else
iserror = true
table.insert(error_msg, "Cannot create mail file")
end end
local result = {} local result = {}
result.error = iserror result.error = iserror

Binary file not shown.

View File

@ -175,7 +175,7 @@ namespace OS {
// this dialog is for send mail // this dialog is for send mail
export class BloggerSendmailDiaglog extends OS.GUI.BasicDialog { export class BloggerSendmailDiaglog extends OS.GUI.BasicDialog {
static template: string; static template: string;
private maillinglist: OS.GUI.tag.ListViewTag; private maillinglist: OS.GUI.tag.StackMenuTag;
// TODO: convert to SQLite handle // TODO: convert to SQLite handle
private subdb: API.VFS.BaseFileHandle; private subdb: API.VFS.BaseFileHandle;
constructor() { constructor() {
@ -184,7 +184,7 @@ namespace OS {
main() { main() {
super.main(); super.main();
this.maillinglist = this.find("email-list") as OS.GUI.tag.ListViewTag; this.maillinglist = this.find("email-list") as OS.GUI.tag.StackMenuTag;
const title = (new RegExp("^#+(.*)\n", "g")).exec(this.data.content); const title = (new RegExp("^#+(.*)\n", "g")).exec(this.data.content);
(this.find("mail-title") as HTMLInputElement).value = title[1]; (this.find("mail-title") as HTMLInputElement).value = title[1];
const content = (this.data.content.substring(0, 500)) + "..."; const content = (this.data.content.substring(0, 500)) + "...";
@ -196,16 +196,16 @@ namespace OS {
switch: true, switch: true,
checked: true checked: true
} }
}) });
this.maillinglist.data = mlist; console.log(mlist);
this.maillinglist.items = mlist;
return (this.find("bt-sendmail") as OS.GUI.tag.ButtonTag).onbtclick = (e: any) => { return (this.find("bt-sendmail") as OS.GUI.tag.ButtonTag).onbtclick = (e: any) => {
const items = this.maillinglist.data; const items = this.maillinglist.items;
const emails = []; const emails = [];
for (let v of items) { for (let v of items) {
if (v.checked === true) { if (v.checked === true) {
console.log(v.email); emails.push(v);
emails.push(v.email);
} }
} }
@ -216,15 +216,18 @@ namespace OS {
parameters: { parameters: {
to: emails, to: emails,
title: (this.find("mail-title") as HTMLInputElement).value, title: (this.find("mail-title") as HTMLInputElement).value,
content: (this.find("contentarea") as HTMLTextAreaElement).value content: (this.find("contentarea") as HTMLTextAreaElement).value,
user: (this.find("mail-user") as HTMLInputElement).value,
password: (this.find("mail-password") as HTMLInputElement).value,
} }
}; };
return this._api.apigateway(data, false) return this._api.apigateway(data, false)
.then((d: { error: any; result: { join: (arg0: string) => any; }; }) => { .then((d: { error: any; result: { join: (arg0: string) => any; }; }) => {
if (d.error) { return this.notify(__("Unable to send mail to: {0}", d.result.join(","))); } if (d.error) {
const str = d.result.join(',');
return this.notify(__("Unable to send mail to: {0}", str)); }
return this.quit(); return this.quit();
}).catch((e) => { }).catch((e) => {
console.log(e);
return this.error(__("Error sending mail: {0}", e.toString()), e); return this.error(__("Error sending mail: {0}", e.toString()), e);
}); });
}; };
@ -232,24 +235,25 @@ namespace OS {
} }
BloggerSendmailDiaglog.scheme = `\ BloggerSendmailDiaglog.scheme = `\
<afx-app-window data-id = "blogger-send-mail-win" apptitle="Send mail" width="500" height="400" resizable = "false"> <afx-app-window data-id = "blogger-send-mail-win" apptitle="Send mail" width="600" height="400" resizable = "false">
<afx-hbox> <afx-hbox padding="5">
<afx-menu data-width="150" data-id="email-list"></afx-menu> <afx-stack-menu data-width="200" data-id="email-list"></afx-stack-menu>
<afx-resizer data-width="3"></afx-resizer> <afx-resizer data-width="3"></afx-resizer>
<div data-width="5"></div>
<afx-vbox > <afx-vbox >
<div data-height="5"></div>
<afx-label data-height="20" text = "__(Title)"></afx-label> <afx-label data-height="20" text = "__(Title)"></afx-label>
<input type = "text" data-height="20" name="title" data-id = "mail-title"></input> <input type = "text" data-height="25" name="title" data-id = "mail-title"></input>
<afx-label data-height = "20" text = "Content" ></afx-label> <afx-label data-height = "20" text = "__(Content)" ></afx-label>
<textarea name="content" data-id = "contentarea" ></textarea> <textarea name="content" data-id = "contentarea" ></textarea>
<div data-height="5"></div> <afx-label data-height="20" text = "__(IO Hub mail username/password)"></afx-label>
<afx-hbox data-height="25">
<input type = "text" name="username" data-id = "mail-user"></input>
<input type = "password" name="password" data-id = "mail-password"></input>
</afx-hbox>
<afx-hbox data-height = "30"> <afx-hbox data-height = "30">
<div></div> <div></div>
<afx-button iconclass = "fa fa-paper-plane" data-id = "bt-sendmail" data-width="60" text = "__(Send)"></afx-button> <afx-button iconclass = "fa fa-paper-plane" data-id = "bt-sendmail" data-width="content" text = "__(Send)"></afx-button>
</afx-hbox> </afx-hbox>
</afx-vbox> </afx-vbox>
<div data-width="5"></div>
</afx-hbox> </afx-hbox>
</afx-app-window>`; </afx-app-window>`;
@ -257,7 +261,7 @@ namespace OS {
BloggerSendmailDiaglog.template = `\ BloggerSendmailDiaglog.template = `\
Hello, Hello,
Xuan Sang LE has just published a new post on his blog: https://blog.iohub.dev/post/id/{0} Dany LE has just published a new post on his blog: https://blog.iohub.dev/post/id/{0}
========== ==========
{1} {1}

View File

@ -28,6 +28,8 @@
"Location": "Location", "Location": "Location",
"From": "From", "From": "From",
"Save": "Save", "Save": "Save",
"Content": "Content",
"IO Hub mail username/password": "IO Hub mail username/password",
"Send": "Send", "Send": "Send",
"Please select a parent category": "Please select a parent category", "Please select a parent category": "Please select a parent category",
"Please enter category name": "Please enter category name", "Please enter category name": "Please enter category name",
@ -36,6 +38,7 @@
"No email selected": "No email selected", "No email selected": "No email selected",
"Unable to send mail to: {0}": "Unable to send mail to: {0}", "Unable to send mail to: {0}": "Unable to send mail to: {0}",
"Error sending mail: {0}": "Error sending mail: {0}", "Error sending mail: {0}": "Error sending mail: {0}",
"Open/Create database": "Open/Create database",
"Open/create new database": "Open/create new database", "Open/create new database": "Open/create new database",
"Unable to init database file: {0}": "Unable to init database file: {0}", "Unable to init database file: {0}": "Unable to init database file: {0}",
"Select image file": "Select image file", "Select image file": "Select image file",