Skip to content

Commit ea1c01a

Browse files
feat: |Worker| add REMOVE_ALL_ATTACHMENT and REMOVE_EXCEED_SIZE_ATTACHMENT
1 parent a22add0 commit ea1c01a

File tree

8 files changed

+77
-1
lines changed

8 files changed

+77
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
## main(v0.8.5)
55

66
- feat: |mail-parser-wasm-worker| 修复 `initSync` 函数调用时的 `deprecated` 参数警告
7+
- feat: rpc headers covert & typo (#559)
8+
- fix: telegram mail page use iframe show email (#561)
9+
- feat: |Worker| 增加 `REMOVE_ALL_ATTACHMENT``REMOVE_EXCEED_SIZE_ATTACHMENT` 用于移除邮件附件,由于是解析邮件的一些信息会丢失,比如图片等.
710

811
## v0.8.4
912

vitepress-docs/docs/en/cli.md

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ ENABLE_AUTO_REPLY = false
138138
# JUNK_MAIL_CHECK_LIST = = ["spf", "dkim", "dmarc"]
139139
# junk mail force check pass list, if no status or status is not pass, will be marked as junk mail
140140
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]
141+
# remove attachment if size exceed 2MB, mail maybe mising some information due to parsing
142+
# REMOVE_EXCEED_SIZE_ATTACHMENT = true
143+
# remove all attachment, mail maybe mising some information due to parsing
144+
# REMOVE_ALL_ATTACHMENT = true
141145

142146
[[d1_databases]]
143147
binding = "DB"

vitepress-docs/docs/zh/guide/cli/worker.md

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ ENABLE_AUTO_REPLY = false
110110
# JUNK_MAIL_CHECK_LIST = = ["spf", "dkim", "dmarc"]
111111
# 垃圾邮件检查配置, 任何一项 不存在 或者 不通过 则被判定为垃圾邮件
112112
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]
113+
# 如果附件大小超过 2MB,则删除附件,邮件可能由于解析而丢失一些信息
114+
# REMOVE_EXCEED_SIZE_ATTACHMENT = true
115+
# 移除所有附件,邮件可能由于解析而丢失一些信息
116+
# REMOVE_ALL_ATTACHMENT = true
113117
# 是否开启其他 worker 处理邮件
114118
# ENABLE_ANOTHER_WORKER = false
115119
# 其他 worker 处理邮件的配置,可以配置多个其他 worker。

worker/src/admin_api/worker_config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export default {
4646
"JUNK_MAIL_CHECK_LIST": getStringArray(c.env.JUNK_MAIL_CHECK_LIST),
4747
"JUNK_MAIL_FORCE_PASS_LIST": getStringArray(c.env.JUNK_MAIL_FORCE_PASS_LIST),
4848

49+
"REMOVE_EXCEED_SIZE_ATTACHMENT": getBooleanValue(c.env.REMOVE_EXCEED_SIZE_ATTACHMENT),
50+
"REMOVE_ALL_ATTACHMENT": getBooleanValue(c.env.REMOVE_ALL_ATTACHMENT),
51+
4952
"ENABLE_ANOTHER_WORKER": getBooleanValue(c.env.ENABLE_ANOTHER_WORKER),
5053
"ANOTHER_WORKER_LIST": getAnotherWorkerList(c),
5154
})

worker/src/email/check_attachment.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Bindings, ParsedEmailContext } from "../types";
2+
import { getBooleanValue } from "../utils";
3+
import { commonParseMail } from "../common";
4+
import { createMimeMessage } from "mimetext";
5+
6+
export const remove_attachment_if_need = async (
7+
env: Bindings,
8+
parsedEmailContext: ParsedEmailContext,
9+
from_address: string,
10+
to_address: string,
11+
size: number
12+
): Promise<void> => {
13+
// if configured, remove all attachment
14+
const removeAllAttachment = getBooleanValue(env.REMOVE_ALL_ATTACHMENT);
15+
// if attachment size > 2MB, remove attachment
16+
const removeExceedSizeAttachment = getBooleanValue(env.REMOVE_EXCEED_SIZE_ATTACHMENT) && size >= 2 * 1024 * 1024;
17+
const shouldRemoveAttachment = removeAllAttachment || removeExceedSizeAttachment;
18+
if (!shouldRemoveAttachment) return;
19+
20+
const parsedEmail = await commonParseMail(parsedEmailContext);
21+
if (!parsedEmail) return;
22+
23+
const msg = createMimeMessage();
24+
if (parsedEmail?.headers) {
25+
for (const header of parsedEmail.headers) {
26+
msg.setHeader(header["key"], header["value"]);
27+
}
28+
}
29+
msg.setSender({
30+
name: parsedEmail?.sender || from_address,
31+
addr: from_address
32+
});
33+
msg.setRecipient(to_address);
34+
msg.setSubject(parsedEmail?.subject || "Failed to parse email subject");
35+
if (parsedEmail?.html) {
36+
msg.addMessage({
37+
contentType: 'text/html',
38+
data: parsedEmail.html
39+
});
40+
}
41+
if (parsedEmail?.text) {
42+
msg.addMessage({
43+
contentType: 'text/plain',
44+
data: parsedEmail.text
45+
});
46+
}
47+
parsedEmailContext.rawEmail = msg.asRaw();
48+
}

worker/src/email/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { auto_reply } from "./auto_reply";
77
import { isBlocked } from "./black_list";
88
import { triggerWebhook, triggerAnotherWorker, commonParseMail } from "../common";
99
import { check_if_junk_mail } from "./check_junk";
10+
import { remove_attachment_if_need } from "./check_attachment";
1011

1112

1213
async function email(message: ForwardableEmailMessage, env: Bindings, ctx: ExecutionContext) {
@@ -32,13 +33,20 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
3233
console.log("check junk mail error", error);
3334
}
3435

36+
// remove attachment if configured or size > 2MB
37+
try {
38+
remove_attachment_if_need(env, parsedEmailContext, message.from, message.to, message.rawSize);
39+
} catch (error) {
40+
console.log("remove attachment error", error);
41+
}
42+
3543
const message_id = message.headers.get("Message-ID");
3644
// save email
3745
try {
3846
const { success } = await env.DB.prepare(
3947
`INSERT INTO raw_mails (source, address, raw, message_id) VALUES (?, ?, ?, ?)`
4048
).bind(
41-
message.from, message.to, rawEmail, message_id
49+
message.from, message.to, parsedEmailContext.rawEmail, message_id
4250
).run();
4351
if (!success) {
4452
message.setReject(`Failed save message to ${message.to}`);

worker/src/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export type Bindings = {
5050
ENABLE_ANOTHER_WORKER: string | boolean | undefined
5151
ANOTHER_WORKER_LIST: string | AnotherWorker[] | undefined
5252

53+
REMOVE_ALL_ATTACHMENT: string | boolean | undefined
54+
REMOVE_EXCEED_SIZE_ATTACHMENT: string | boolean | undefined
5355

5456
// s3 config
5557
S3_ENDPOINT: string | undefined

worker/wrangler.toml.template

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ ENABLE_AUTO_REPLY = false
8080
# JUNK_MAIL_CHECK_LIST = = ["spf", "dkim", "dmarc"]
8181
# junk mail force check pass list, if no status or status is not pass, will be marked as junk mail
8282
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]
83+
# remove attachment if size exceed 2MB, mail maybe mising some information due to parsing
84+
# REMOVE_EXCEED_SIZE_ATTACHMENT = true
85+
# remove all attachment, mail maybe mising some information due to parsing
86+
# REMOVE_ALL_ATTACHMENT = true
8387
# Calling other woker to process email
8488
#ENABLE_ANOTHER_WORKER = false
8589
#ANOTHER_WORKER_LIST ="""

0 commit comments

Comments
 (0)