类似于 abc+123@a.com 和 abc.123@b.com 的形式是我们比较熟悉的方式
方案一:只需要使用abc+123的格式
只需要在Email Routing 的配置里开启 Enable subaddressing
方案二:如果需要abc.123的格式
那么就是开启 Email Worker,配置如下代码
仅供参考,我采用的是 noreply.(anything)@abc.com
export default {
async email(message, env, ctx) {
// 1. Get the recipient address
const recipient = message.to;
// 2. Define the regex pattern for "noreply.*@abc.com"
// This matches addresses like "noreply.news@abc.com" or "noreply.alerts@abc.com"
// The 'i' flag makes it case-insensitive
const pattern = /^noreply\..*@abc\.com$/i;
// 3. Check if the recipient matches the pattern
if (pattern.test(recipient)) {
// Forward to the specific destination
await message.forward("noreply@gmail.com");
} else {
// 4. Otherwise, reject the email with a tip/message
message.setReject("Email rejected: This address does not exist or is not accepted.");
}
}
};
方案3: 开启方案2的缺点是不支持 +,如果想两者都支持,只要替换pattern即可
const pattern = /^noreply[\.\+].*@abc\.com$/i;
然后最重要的,是在Routing Rule 里边开启 Catch-All,并设置发送到 新创建的worker即可