mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-16 23:27:35 +08:00
feat: allow user delete mail && notify when send access changed (#132)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Hono } from 'hono'
|
||||
import { Jwt } from 'hono/utils/jwt'
|
||||
import { getSendbox } from './send_mail_api'
|
||||
import { sendAdminInternalMail } from './utils'
|
||||
|
||||
const api = new Hono()
|
||||
|
||||
@@ -164,7 +165,7 @@ api.get('/admin/address_sender', async (c) => {
|
||||
})
|
||||
|
||||
api.post('/admin/address_sender', async (c) => {
|
||||
let { address_id, balance, enabled } = await c.req.json();
|
||||
let { address, address_id, balance, enabled } = await c.req.json();
|
||||
if (!address_id) {
|
||||
return c.text("Invalid address_id", 400)
|
||||
}
|
||||
@@ -175,6 +176,10 @@ api.post('/admin/address_sender', async (c) => {
|
||||
if (!success) {
|
||||
return c.text("Failed to update address sender", 500)
|
||||
}
|
||||
await sendAdminInternalMail(
|
||||
c, address, "Account Send Access Updated",
|
||||
`You send access has been ${enabled ? "enabled" : "disabled"}, balance: ${balance}`
|
||||
);
|
||||
return c.json({
|
||||
success: success
|
||||
})
|
||||
|
||||
@@ -33,6 +33,17 @@ api.get('/api/mails', async (c) => {
|
||||
})
|
||||
})
|
||||
|
||||
api.delete('/api/mails/:id', async (c) => {
|
||||
const { address } = c.get("jwtPayload")
|
||||
const { id } = c.req.param();
|
||||
const { success } = await c.env.DB.prepare(
|
||||
`DELETE FROM raw_mails WHERE address = ? and id = ?`
|
||||
).bind(address, id).run();
|
||||
return c.json({
|
||||
success: success
|
||||
})
|
||||
})
|
||||
|
||||
api.get('/api/settings', async (c) => {
|
||||
const { address, address_id } = c.get("jwtPayload")
|
||||
if (address_id && address_id > 0) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { createMimeMessage } from "mimetext";
|
||||
|
||||
export const getDomains = (c) => {
|
||||
if (!c.env.DOMAINS) {
|
||||
return [];
|
||||
@@ -45,3 +47,33 @@ export const getAdminPasswords = (c) => {
|
||||
}
|
||||
return c.env.ADMIN_PASSWORDS;
|
||||
}
|
||||
|
||||
export const sendAdminInternalMail = async (c, toMail, subject, text) => {
|
||||
try {
|
||||
|
||||
const msg = createMimeMessage();
|
||||
msg.setSender({
|
||||
name: "Admin",
|
||||
addr: "admin@internal"
|
||||
});
|
||||
msg.setRecipient(toMail);
|
||||
msg.setSubject(subject);
|
||||
msg.addMessage({
|
||||
contentType: 'text/plain',
|
||||
data: text
|
||||
});
|
||||
const message_id = Math.random().toString(36).substring(2, 15);
|
||||
const { success } = await c.env.DB.prepare(
|
||||
`INSERT INTO raw_mails (source, address, raw, message_id) VALUES (?, ?, ?, ?)`
|
||||
).bind(
|
||||
"admin@internal", toMail, msg.asRaw(), message_id
|
||||
).run();
|
||||
if (!success) {
|
||||
console.log(`Failed save message from admin@internal to ${toMail}`);
|
||||
}
|
||||
return success;
|
||||
} catch (error) {
|
||||
console.log("sendAdminInternalMail error", error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user