Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat(conf): add conf option for specific email tpl for admins
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Feb 3, 2023
1 parent 19c6c9e commit e2674a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ const (

// # Redis 配置
// redis:
// network: "tcp"
// username: ""
// password: ""
// db: 0
//
// network: "tcp"
// username: ""
// password: ""
// db: 0
type RedisConf struct {
Network string `koanf:"network" json:"network"` // tcp or unix
Username string `koanf:"username" json:"username"`
Expand Down Expand Up @@ -239,6 +240,7 @@ type AdminNotifyConf struct {
type AdminEmailConf struct {
Enabled bool `koanf:"enabled" json:"enabled"` // 管理员总开关
MailSubject string `koanf:"mail_subject" json:"mail_subject"` // 管理员邮件标题
MailTpl string `koanf:"mail_tpl" json:"mail_tpl"` // 管理员专用邮件模板
}

type NotifyTelegramConf struct {
Expand Down
2 changes: 1 addition & 1 deletion lib/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func AsyncSend(notify *model.Notify) {

receiveUser := notify.FetchUser()

mailBody := RenderEmailBody(notify)
mailBody := RenderEmailBody(notify, receiveUser.IsAdmin)
mailSubject := ""
if !receiveUser.IsAdmin {
mailSubject = RenderCommon(config.Instance.Email.MailSubject, notify)
Expand Down
11 changes: 10 additions & 1 deletion lib/email/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@ func HandleEmoticonsImgTagsForNotify(str string) string {
}

// 渲染邮件 Body 内容
func RenderEmailBody(notify *model.Notify) string {
func RenderEmailBody(notify *model.Notify, isSendToAdmin bool) string {
tplName := config.Instance.Email.MailTpl

// 发送给管理员的邮件单独使用管理员邮件模板
if isSendToAdmin {
tplName = config.Instance.AdminNotify.Email.MailTpl
}

// 配置文件未指定邮件模板路径,使用内置默认模板
if tplName == "" {
tplName = "default"
}
Expand All @@ -133,6 +140,8 @@ func RenderEmailBody(notify *model.Notify) string {
if _, err := os.Stat(tplName); errors.Is(err, os.ErrNotExist) {
tpl = GetInternalEmailTpl(tplName)
} else {
// TODO 反复文件 IO 操作会导致性能下降,
// 之后优化可以改成程序启动时加载模板文件到内存中
tpl = GetExternalTpl(tplName)
}

Expand Down

0 comments on commit e2674a2

Please sign in to comment.