changeset 0:bd4802730bab

its working
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 18 May 2023 21:20:59 -0600
parents
children 6cd68fe047c7
files .hgignore serve.sh src/index.html src/init.luan src/send.json.luan src/site.css
diffstat 6 files changed, 208 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,5 @@
+syntax: glob
+
+err
+push.sh
+mine/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serve.sh	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,1 @@
+luan luan:http/serve.luan src 2>&1 | tee err
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/index.html	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,127 @@
+<!doctype html>
+<html>
+	<head>
+		<meta name="viewport" content="width=device-width, initial-scale=1">
+		<style>
+			@import "/site.css";
+
+			h1 {
+				margin-bottom: 8px;
+			}
+			h4 {
+				margin-top: 0;
+			}
+
+			textarea {
+				width: 100%;
+			}
+			textarea[name=server] {
+				height: 120px;
+			}
+			textarea[name=mail] {
+				height: 260px;
+			}
+		</style>
+		<script>
+			let server = {
+				host: 'mail.smtp2go.com',
+				username: 'luan.software',
+				password: 'not telling',
+				port: 2525,
+			};
+			server = JSON.stringify(server,null,'\t');
+
+			let mails = {};
+
+			mails.simple = {
+				From: 'mailer@reactionary.software',
+				To: 'someone@reactionary.software',
+				Subject: 'Simple',
+				body: 'Google sucks.',
+			};
+
+			mails.html = {
+				From: 'mailer@reactionary.software',
+				To: 'someone@reactionary.software',
+				Subject: 'HTML',
+				'Content-Type': 'multipart/alternative',
+				body: [
+					{
+						'Content-Type': 'text/plain; charset="UTF-8"',
+						body: 'Google sucks.',
+					},
+					{
+						'Content-Type': 'text/html; charset="UTF-8"',
+						body: '<a href="https://www.google.com/">Google</a> sucks.',
+					},
+				],
+			};
+
+			mails.attachment = {
+				From: 'mailer@reactionary.software',
+				To: 'someone@reactionary.software',
+				Subject: 'Attachment',
+				'Content-Type': 'multipart/mixed',
+				body: [
+					{
+						'Content-Type': 'multipart/alternative',
+						body: [
+							{
+								'Content-Type': 'text/plain; charset="UTF-8"',
+								body: 'Google sucks.',
+							},
+							{
+								'Content-Type': 'text/html; charset="UTF-8"',
+								body: '<a href="https://www.google.com/">Google</a> sucks.',
+							},
+						],
+					},
+					{
+						'Content-Disposition': 'attachment; filename="test.txt"',
+						body: 'test file',
+					},
+				]
+			};
+
+			function set(what) {
+				let s = JSON.stringify( mails[what], null, '\t' );
+				document.querySelector('textarea[name=mail]').textContent = s;
+			}
+
+			function init() {
+				document.querySelector('textarea[name=server]').textContent = server;
+				set('simple');
+			}
+		</script>
+		<title>Mailer REST API</title>
+	</head>
+	<body>
+		<h1>Mailer REST API</h1>
+		<h4><a href="http://www.reactionary.software/">Reactionary Software</a> by <a href="https://www.linkmystyle.com/fschmidt">fschmidt</a></h4>
+
+		<p>Send emails by a POST request.</p>
+
+		<form method=post action="/send.json">
+			<p>
+				SMTP Server<br>
+				<textarea name=server></textarea><br>
+				<small>Update to your server.</small>
+			</p>
+			<p>
+				Mail<br>
+				<textarea name=mail></textarea><br>
+				<button type=button onclick="set('simple')">Simple</button>
+				<button type=button onclick="set('html')">HTML</button>
+				<button type=button onclick="set('attachment')">Attachment</button>
+			</p>
+			<p>
+				<input type=submit>
+			</p>
+		</form>
+
+		<p>Note that all <b>mail</b> fields except <b>body</b> are simply passed directly to SMTP.</p>
+
+		<p>Here is <a href="https://hg.reactionary.software/repo/mailer/">the source</a> of this website.  This service is implemented in <a href="http://www.luan.software/">Luan</a>.  The entire implemention is in the file <a href="/send.json.luan">send.json.luan</a>.  And here are discussion threads on <a href="https://communities.win/c/programming/p/16b6N5KIjr/mailer-rest-api/c">Scored</a> and <a href="http://www.mikraite.org/Mailer-REST-API-tp3435.html">Reactionary Software</a> if you want to comment.</p>
+	</body>
+	<script> init(); </script>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/init.luan	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,6 @@
+local Http = require "luan:http/Http.luan"
+local Hosted = require "luan:host/Hosted.luan"
+
+Hosted.set_https and Hosted.set_https(Http.domain=="mailer.reactionary.software")
+
+return true
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/send.json.luan	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,53 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local new_error = Luan.new_error or error()
+local stringify = Luan.stringify or error()
+local Parsers = require "luan:Parsers.luan"
+local json_parse = Parsers.json_parse or error()
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Mail = require "luan:mail/Mail.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "send.json"
+
+
+local function user_error(msg)
+	local e = new_error(msg)
+	e.error = {
+		okay = false
+		error = msg
+	}
+	e.throw()
+end
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	try 
+		local server = Http.request.parameters.server or user_error "parameter 'server' missing"
+		local mail = Http.request.parameters.mail or user_error "parameter 'mail' missing"
+		try
+			server = json_parse(server)
+		catch e
+			user_error "invalid JSON in 'server'"
+		end
+		try
+			mail = json_parse(mail)
+		catch e
+			user_error "invalid JSON in 'mail'"
+		end
+		try
+			Mail.sender(server).send(mail)
+		catch e
+			logger.warn(e..stringify(server).."\n"..stringify(mail))
+			user_error(e.get_message())
+		end
+		%><%=json_string{okay=true}%><%
+	catch e
+		if e.error ~= nil then
+			%><%=json_string(e.error)%><%
+			return
+		end
+		e.throw()
+	end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/site.css	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,16 @@
+* {
+ 	box-sizing: border-box;
+}
+
+body {
+	font-family: Sans-Serif;
+	margin-left: 3%;
+	margin-right: 3%;
+}
+
+a {
+	text-decoration: none;
+}
+a:hover {
+	text-decoration: underline;
+}