Skip to content

Commit 1315578

Browse files
authored
支持自定义接收邮箱地址 (#2973)
* 支持自定义接收邮箱地址 * 新增支持多个接收邮箱,同步到node和系统内置版本
1 parent a1ae08d commit 1315578

4 files changed

Lines changed: 43 additions & 14 deletions

File tree

back/services/notify.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ export default class NotificationService {
9191
return true;
9292
}
9393

94+
private parseMailRecipients(value?: string) {
95+
const recipients = (value || '')
96+
.split(/[;]/)
97+
.map((item) => item.trim())
98+
.filter(Boolean);
99+
return recipients.length > 0 ? recipients : undefined;
100+
}
101+
94102
private async gotify() {
95103
const { gotifyUrl, gotifyToken, gotifyPriority = 1 } = this.params;
96104
try {
@@ -592,6 +600,7 @@ export default class NotificationService {
592600

593601
private async email() {
594602
const { emailPass, emailService, emailUser, emailTo } = this.params;
603+
const recipients = this.parseMailRecipients(emailTo) || emailUser;
595604

596605
try {
597606
const transporter = nodemailer.createTransport({
@@ -604,7 +613,7 @@ export default class NotificationService {
604613

605614
const info = await transporter.sendMail({
606615
from: `"青龙快讯" <${emailUser}>`,
607-
to: emailTo ? emailTo.split(';') : emailUser,
616+
to: recipients,
608617
subject: `${this.title}`,
609618
html: `${this.content.replace(/\n/g, '<br/>')}`,
610619
});

sample/config.sample.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ export SMTP_SERVER=""
195195
## SMTP 发送邮件服务器是否使用 SSL,填写 true 或 false
196196
export SMTP_SSL=""
197197

198-
## smtp_email 填写 SMTP 收发件邮箱,通知将会由自己发给自己
198+
## smtp_email 填写 SMTP 发件邮箱
199199
export SMTP_EMAIL=""
200200
## smtp_password 填写 SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定
201201
export SMTP_PASSWORD=""
202202
## smtp_name 填写 SMTP 收发件人姓名,可随意填写
203203
export SMTP_NAME=""
204+
## smtp_email_to 填写 SMTP 收件邮箱,多个用英文;分隔,不填默认发给发件邮箱
205+
export SMTP_EMAIL_TO=""
204206

205207
## 17. PushMe
206208
## 官方说明文档:https://push.i-i.me/

sample/notify.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ const push_config = {
121121

122122
SMTP_SERVICE: '', // 邮箱服务名称,比如 126、163、Gmail、QQ 等,支持列表 https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json
123123
SMTP_EMAIL: '', // SMTP 发件邮箱
124-
SMTP_TO: '', // SMTP 收件邮箱,默认通知将会发给发件邮箱
124+
SMTP_TO: '', // SMTP 收件邮箱,兼容旧参数名,默认通知将会发给发件邮箱
125+
SMTP_EMAIL_TO: '', // SMTP 收件邮箱,多个分号分隔,默认发给发件邮箱
125126
SMTP_PASSWORD: '', // SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定
126127
SMTP_NAME: '', // SMTP 收发件人姓名,可随意填写
127128

@@ -1051,8 +1052,14 @@ function fsBotNotify(text, desp) {
10511052
}
10521053

10531054
async function smtpNotify(text, desp) {
1054-
const { SMTP_EMAIL, SMTP_TO, SMTP_PASSWORD, SMTP_SERVICE, SMTP_NAME } =
1055-
push_config;
1055+
const {
1056+
SMTP_EMAIL,
1057+
SMTP_TO,
1058+
SMTP_EMAIL_TO,
1059+
SMTP_PASSWORD,
1060+
SMTP_SERVICE,
1061+
SMTP_NAME,
1062+
} = push_config;
10561063
if (![SMTP_EMAIL, SMTP_PASSWORD].every(Boolean) || !SMTP_SERVICE) {
10571064
return;
10581065
}
@@ -1068,9 +1075,20 @@ async function smtpNotify(text, desp) {
10681075
});
10691076

10701077
const addr = SMTP_NAME ? `"${SMTP_NAME}" <${SMTP_EMAIL}>` : SMTP_EMAIL;
1078+
const recipients = [SMTP_EMAIL_TO, SMTP_TO].reduce((list, value) => {
1079+
if (!value) {
1080+
return list;
1081+
}
1082+
return list.concat(
1083+
value
1084+
.split(/[;]/)
1085+
.map((item) => item.trim())
1086+
.filter(Boolean),
1087+
);
1088+
}, []);
10711089
const info = await transporter.sendMail({
10721090
from: addr,
1073-
to: SMTP_TO ? SMTP_TO.split(';') : addr,
1091+
to: recipients.length ? recipients : SMTP_EMAIL,
10741092
subject: text,
10751093
html: `${desp.replace(/\n/g, '<br/>')}`,
10761094
});

sample/notify.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def print(text, *args, **kw):
107107

108108
'SMTP_SERVER': '', # SMTP 发送邮件服务器,形如 smtp.exmail.qq.com:465
109109
'SMTP_SSL': 'false', # SMTP 发送邮件服务器是否使用 SSL,填写 true 或 false
110-
'SMTP_EMAIL': '', # SMTP 收发件邮箱,通知将会由自己发给自己
110+
'SMTP_EMAIL': '', # SMTP 发件邮箱
111+
'SMTP_EMAIL_TO': '', # SMTP 收件邮箱,多个分号分隔,默认发给发件邮箱
111112
'SMTP_PASSWORD': '', # SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定
112113
'SMTP_NAME': '', # SMTP 收发件人姓名,可随意填写
113114

@@ -694,19 +695,18 @@ def smtp(title: str, content: str) -> None:
694695
return
695696
print("SMTP 邮件 服务启动")
696697

698+
email_to = push_config.get("SMTP_EMAIL_TO") or push_config.get("SMTP_EMAIL")
699+
email_to_list = [
700+
item.strip() for item in re.split(r"[;;]", email_to) if item.strip()
701+
]
697702
message = MIMEText(content, "plain", "utf-8")
698703
message["From"] = formataddr(
699704
(
700705
Header(push_config.get("SMTP_NAME"), "utf-8").encode(),
701706
push_config.get("SMTP_EMAIL"),
702707
)
703708
)
704-
message["To"] = formataddr(
705-
(
706-
Header(push_config.get("SMTP_NAME"), "utf-8").encode(),
707-
push_config.get("SMTP_EMAIL"),
708-
)
709-
)
709+
message["To"] = ",".join(email_to_list)
710710
message["Subject"] = Header(title, "utf-8")
711711

712712
try:
@@ -720,7 +720,7 @@ def smtp(title: str, content: str) -> None:
720720
)
721721
smtp_server.sendmail(
722722
push_config.get("SMTP_EMAIL"),
723-
push_config.get("SMTP_EMAIL"),
723+
email_to_list,
724724
message.as_bytes(),
725725
)
726726
smtp_server.close()

0 commit comments

Comments
 (0)