-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
62 lines (56 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";
export default {
async fetch(request, env) {
/**
* validate the formdata
* or get the formdata
* @param {*} formdata
*/
function checkOrGetFormData (formdata) {
const fileds = ["sender_email", "sender_name", "recipient_email", "subject", "msg_data"]
const result = []
let error = ""
fileds.forEach(item => {
const value = formdata.get(item)
console.debug(item, value)
if (!value || value == "" || value == undefined || value == null) {
error = "Request params :" + item + " error"
}
result[item] = value
});
if (error != "") {
return null
}
return result
}
if (request.method !== "POST") {
console.debug("Request method must be POST");
return new Response("Request method must be POST");
}
const formdata = await request.formData();
const data = checkOrGetFormData(formdata)
if (!data) {
return new Response("Request params error");
}
const msg = createMimeMessage();
msg.setSender({ name: data.sender_name, addr: data.sender_email});
msg.setRecipient(data.recipient_email);
msg.setSubject(data.subject);
msg.addMessage({
contentType: 'text/plain',
data: data.msg_data
});
var message = new EmailMessage(
data.sender_email,
data.recipient_email,
msg.asRaw()
);
try {
await env.SEB.send(message);
} catch (e) {
return new Response(e.message);
}
return new Response("Send Email Successfully!");
},
};