mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-07-06 06:41:32 +08:00
feat: telegram bot TelegramSettings && webhook (#244)
* feat: telegram bot TelegramSettings * feat: webhook
This commit is contained in:
51
worker/src/email/index.ts
Normal file
51
worker/src/email/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Context } from "hono";
|
||||
|
||||
import { sendMailToTelegram } from "../telegram_api";
|
||||
import { Bindings } from "../types";
|
||||
import { auto_reply } from "./auto_reply";
|
||||
import { trigerWebhook } from "../mails_api/webhook_settings";
|
||||
|
||||
|
||||
async function email(message: ForwardableEmailMessage, env: Bindings, ctx: ExecutionContext) {
|
||||
if (env.BLACK_LIST && env.BLACK_LIST.split(",").some(word => message.from.includes(word))) {
|
||||
message.setReject("Missing from address");
|
||||
console.log(`Reject message from ${message.from} to ${message.to}`);
|
||||
return;
|
||||
}
|
||||
const rawEmail = await new Response(message.raw).text();
|
||||
const message_id = message.headers.get("Message-ID");
|
||||
// save email
|
||||
const { success } = await env.DB.prepare(
|
||||
`INSERT INTO raw_mails (source, address, raw, message_id) VALUES (?, ?, ?, ?)`
|
||||
).bind(
|
||||
message.from, message.to, rawEmail, message_id
|
||||
).run();
|
||||
if (!success) {
|
||||
message.setReject(`Failed save message to ${message.to}`);
|
||||
console.log(`Failed save message from ${message.from} to ${message.to}`);
|
||||
}
|
||||
|
||||
// send email to telegram
|
||||
try {
|
||||
await sendMailToTelegram(
|
||||
{ env: env } as Context<{ Bindings: Bindings }>,
|
||||
message.to, rawEmail);
|
||||
} catch (error) {
|
||||
console.log("send mail to telegram error", error);
|
||||
}
|
||||
|
||||
// send webhook
|
||||
try {
|
||||
await trigerWebhook(
|
||||
{ env: env } as Context<{ Bindings: Bindings }>,
|
||||
message.to, rawEmail
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("send webhook error", error);
|
||||
}
|
||||
|
||||
// auto reply email
|
||||
await auto_reply(message, env);
|
||||
}
|
||||
|
||||
export { email }
|
||||
Reference in New Issue
Block a user