-
-
Save kdxhub/9965e18e4b8432428cb9f85ae6c50fc8 to your computer and use it in GitHub Desktop.
基于Workers和MailChannel API的简易电邮发送。 此Fork相较于原版主要修改了发送UI部分:进行汉化、使用SoberJS UI库重绘GUI、Textarea支持自适应高度而无需手动调整。见于https://kdxhub.github.io/blogs/2024/28
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function readRequestBody(request) { | |
const { headers } = request; | |
const contentType = headers.get('content-type') || ''; | |
if (contentType.includes('application/json')) { | |
return JSON.stringify(await request.json()); | |
} else if (contentType.includes('form')) { | |
const formData = await request.formData(); | |
const body = {}; | |
for (const entry of formData.entries()) { | |
body[entry[0]] = entry[1]; | |
} | |
let data = JSON.parse(JSON.stringify(body)); | |
let combine = `{"personalizations":[{"to":[{"email":"${data.to}","name":"${data.ton}"}],"dkim_domain":"${data.dkim}","dkim_selector":"${data.dkims}","dkim_private_key":"${data.dkimpk}"}],"from":{"email":"${data.from}","name":"${data.fromn}"},"reply_to":{"email":"${data.rep}","name":"${data.repn}"},"subject":"${data.sbj}","content":[{"type":"${data.type}","value":"${data.body}"}]}`; | |
return combine; | |
} else { | |
return '{"success":false}'; | |
} | |
} | |
async function handleRequest(request) { | |
let start = Date.now(); | |
let reqBody = await readRequestBody(request); | |
let send_request = new Request("https://api.mailchannels.net/tx/v1/send", { | |
"method": "POST", | |
"headers": { | |
"content-type": "application/json", | |
}, | |
"body": reqBody | |
}); | |
let resp = await fetch(send_request); | |
let respText = await resp.text(); | |
let end = Date.now(); | |
let total = end - start; | |
return new Response(respText, { | |
headers: { | |
"X-MC-Status": resp.status, | |
"X-Response-Time": total | |
} | |
}); | |
} | |
const htmlForm =`<!DOCTYPE html> | |
<html> | |
<head> | |
<meta content="width=device-width,initial-scale=1" name="viewport"> | |
<meta charset="UTF-8" /> | |
<title id="pagetitle">邮件发送平台</title> | |
<script src="https://unpkg.com/sober@0.2.13/dist/sober.min.js"></script> | |
<link rel="stylesheet" href="https://rs.kdxiaoyi.top/res/css/sober-theme-turquoise.css" /> | |
<link rel="stylesheet" href="https://rs.kdxiaoyi.top/res/css/main.css" /> | |
<link rel="stylesheet" href="https://rs.kdxiaoyi.top/res/css/font.css" /> | |
<body> | |
<!-- 标题栏 --> | |
<s-appbar> | |
<div id="titlebar" slot="headline"> 邮件发送平台 </div> | |
</s-appbar> | |
<s-page weigh="100vw" height="100vh" id="app" class="app"><s-scroll-view><div class="appScroll" style="height: 100vh;width: 100vw"> | |
<div class="welcome-text"> | |
<br><p>注*为必填项目</p> | |
<form action="/" method="POST" autocomplete="on"> | |
<input value="" name="from" type="email" placeholder="发件邮箱地址 *" required><br> | |
<input value="" name="fromn" type="text" placeholder="发件昵称"><br> | |
<input name="to" type="email" placeholder="收件邮箱地址 *" required><br> | |
<input name="ton" type="text" placeholder="收件人"><br> | |
<input name="rep" type="email" placeholder="回复到指定邮箱地址"><br> | |
<input name="repn" type="text" placeholder="接收回复信息人"><br> | |
<input name="dkim" type="text" placeholder="DKIM Domain"><br> | |
<input name="dkims" type="text" placeholder="DKIM Selector"><br> | |
<textarea name="dkimpk" rows="4" cols="23" type="text" placeholder="DKIM私钥"></textarea><br> | |
<select name="type"> | |
<option value="text/html; charset=utf-8">支持HTML语法的邮件</option> | |
<option value="text/plain; charset=utf-8" selected>纯文本邮件</option> | |
</select> | |
<input name="sbj" type="text" placeholder="邮件主题(标题)*" required><br> | |
<textarea id="mail-body" name="body" rows="7" cols="23" placeholder="邮件内容 *" required></textarea><br> | |
<input type="submit" value="发送"> | |
</form> | |
</div> | |
</div></s-scroll-view></s-page> | |
<style> | |
input {width:100%} | |
textarea {width:100%} | |
</style> | |
<script> | |
document.getElementById("mail-body").addEventListener("input", function() {this.style.height = "inherit";this.style.height = \`\${this.scrollHeight}px\`;}); | |
</script> | |
</body> | |
</html>`; | |
addEventListener('fetch', event => { | |
const { request } = event; | |
const { url } = request; | |
if (url.includes('submit')) { | |
return event.respondWith(new Response(htmlForm, {headers:{"Content-Type":"text/html"}})); | |
} | |
if (request.method === 'POST') { | |
return event.respondWith(handleRequest(request)); | |
} else if (request.method === 'GET') { | |
return event.respondWith(new Response(`The request was a GET`)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment