feature: 基于子域名转发到不同的邮箱中去 (#645) (#647)

This commit is contained in:
Zyx-A
2025-04-30 10:41:09 +08:00
committed by GitHub
parent 101a561894
commit 07833d5ca9
5 changed files with 52 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
import { Context } from 'hono';
import { HonoCustomType } from '../types';
import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getAnotherWorkerList, getSplitStringListValue } from '../utils';
import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getSubdomainForwardAddressList, getAnotherWorkerList, getSplitStringListValue } from '../utils';
import { CONSTANTS } from '../constants';
import { isS3Enabled } from '../mails_api/s3_attachment';
@@ -21,6 +21,7 @@ export default {
"MAX_ADDRESS_LEN": getIntValue(c.env.MAX_ADDRESS_LEN, 30),
"FORWARD_ADDRESS_LIST": getStringArray(c.env.FORWARD_ADDRESS_LIST),
"SUBDOMAIN_FORWARD_ADDRESS_LIST": getSubdomainForwardAddressList(c),
"DEFAULT_DOMAINS": getDefaultDomains(c),
"DOMAINS": getDomains(c),
"DOMAIN_LABELS": getStringArray(c.env.DOMAIN_LABELS),

View File

@@ -1,6 +1,7 @@
import { Context } from "hono";
import { getEnvStringList } from "../utils";
import { getSubdomainForwardAddressList } from "../utils";
import { sendMailToTelegram } from "../telegram_api";
import { Bindings, HonoCustomType, RPCEmailMessage, ParsedEmailContext } from "../types";
import { auto_reply } from "./auto_reply";
@@ -67,6 +68,30 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
console.error("forward email error", error);
}
// forward subdomain email
try {
// 遍历 FORWARD_ADDRESS_LIST
const subdomainForwardAddressList = getSubdomainForwardAddressList(env.SUBDOMAIN_FORWARD_ADDRESS_LIST)
for (const subdomainForwardAddress of subdomainForwardAddressList) {
// 检查邮件是否匹配 domains
if (subdomainForwardAddress.domains && subdomainForwardAddress.domains.length > 0) {
for (const domain of subdomainForwardAddress.domains) {
if (message.to.endsWith(domain)) {
// 转发邮件
await message.forward(subdomainForwardAddress.forward);
// 支持多邮箱转发收件,不进行截止
// break;
}
}
} else {
// 如果 domains 为空,则转发所有邮件
await message.forward(subdomainForwardAddress.forward);
}
}
} catch (error) {
console.error("subdomain forward email error", error);
}
// send email to telegram
try {
await sendMailToTelegram(

View File

@@ -4,6 +4,11 @@ export type UserRole = {
prefix: string | undefined | null
}
export type SubdomainForwardAddressList = {
domains: string[] | undefined | null,
forward: string
}
export type Bindings = {
// bindings
DB: D1Database
@@ -44,6 +49,7 @@ export type Bindings = {
COPYRIGHT: string | undefined
DISABLE_SHOW_GITHUB: string | boolean | undefined
FORWARD_ADDRESS_LIST: string | string[] | undefined
SUBDOMAIN_FORWARD_ADDRESS_LIST: string | SubdomainForwardAddressList[] | undefined
ENABLE_CHECK_JUNK_MAIL: string | boolean | undefined
JUNK_MAIL_CHECK_LIST: string | string[] | undefined

View File

@@ -1,6 +1,6 @@
import { Context } from "hono";
import { createMimeMessage } from "mimetext";
import { HonoCustomType, UserRole, AnotherWorker } from "./types";
import { HonoCustomType, UserRole, SubdomainForwardAddressList, AnotherWorker } from "./types";
export const getJsonObjectValue = <T = any>(
value: string | any
@@ -165,6 +165,22 @@ export const getUserRoles = (c: Context<HonoCustomType>): UserRole[] => {
return c.env.USER_ROLES;
}
export const getSubdomainForwardAddressList = (c: Context<HonoCustomType>): SubdomainForwardAddressList[] => {
if (!c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST) {
return [];
}
// check if SUBDOMAIN_FORWARD_ADDRESS_LIST is an array, if not use json.parse
if (!Array.isArray(c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST)) {
try {
return JSON.parse(c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST);
} catch (e) {
console.error("Failed to parse SUBDOMAIN_FORWARD_ADDRESS_LIST", e);
return [];
}
}
return c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST;
}
export const getAnotherWorkerList = (c: Context<HonoCustomType>): AnotherWorker[] => {
if (!c.env.ANOTHER_WORKER_LIST) {
return [];

View File

@@ -80,6 +80,8 @@ ENABLE_AUTO_REPLY = false
# TG_BOT_INFO = "{}"
# global forward address list, if set, all emails will be forwarded to these addresses
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
# subdomain forward address list, if set, subdomain emails will be forwarded to these addresses
# SUBDOMAIN_FORWARD_ADDRESS_LIST = [{"domains":[""],"forward":"xxx1@xxx.com",},{"domains":["subdomain-1.domain.com","subdomain-2.domain.com"],"forward":"xxx2@xxx.com"}]
# Frontend URL
# FRONTEND_URL = "https://xxxx.xxx"
# Enable check junk mail