Skip to content

Commit 3a8ce32

Browse files
committed
update
1 parent 7c854a8 commit 3a8ce32

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

internal/db/mail_content.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func MailContentRead(uid, mid int64) (string, error) {
4343

4444
func MailContentReadDb(mid int64) (string, error) {
4545
var r MailContent
46-
err := db.Model(&MailContent{}).Where("mid = ?", mid).First(&r).Error
46+
err := db.Model(&MailContent{}).Select("content").Where("mid = ?", mid).First(&r).Error
4747
if err != nil {
4848
return "", err
4949
}
@@ -71,9 +71,16 @@ func MailContentWrite(uid int64, mid int64, content string) error {
7171
}
7272

7373
func MailContentWriteDb(mid int64, content string) error {
74-
user := MailContent{Mid: mid, Content: content}
75-
result := db.Create(&user)
76-
return result.Error
74+
// 使用 UPSERT 操作,避免重复插入
75+
var mc MailContent
76+
result := db.Where("mid = ?", mid).First(&mc)
77+
if result.Error != nil {
78+
// 记录不存在,创建新记录
79+
mc = MailContent{Mid: mid, Content: content}
80+
return db.Create(&mc).Error
81+
}
82+
// 记录已存在,更新内容
83+
return db.Model(&mc).Update("content", content).Error
7784
}
7885

7986
func MailContentWriteHardDisk(uid int64, mid int64, content string) error {

internal/tools/mail/mail.go

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import (
1111
"github.com/midoks/imail/internal/tools"
1212
)
1313

14+
// 预编译正则表达式
15+
var (
16+
subjectRegex = regexp.MustCompile(`Subject: (.*)`)
17+
fromRegex = regexp.MustCompile(`From: (.*)`)
18+
)
19+
1420
func GetMailSubject(content string) string {
1521
var err error
16-
valid := regexp.MustCompile(`Subject: (.*)`)
17-
match := valid.FindAllStringSubmatch(content, -1)
22+
match := subjectRegex.FindAllStringSubmatch(content, -1)
23+
if len(match) == 0 {
24+
return ""
25+
}
1826

1927
val := match[0][0]
2028
tmp := strings.SplitN(val, ":", 2)
29+
if len(tmp) < 2 {
30+
return ""
31+
}
2132
val = strings.TrimSpace(tmp[1])
2233

2334
if strings.Contains(val, "=?utf-8?B?") || strings.Contains(val, "=?UTF-8?B?") {
@@ -47,22 +58,33 @@ func GetMailSubject(content string) string {
4758

4859
func GetMailFromInContent(content string) string {
4960
var err error
50-
valid := regexp.MustCompile(`From: (.*)`)
51-
match := valid.FindAllStringSubmatch(content, -1)
61+
match := fromRegex.FindAllStringSubmatch(content, -1)
62+
if len(match) == 0 {
63+
return ""
64+
}
5265

5366
val := match[0][0]
5467
tmp := strings.SplitN(val, ":", 2)
68+
if len(tmp) < 2 {
69+
return ""
70+
}
5571
val = strings.TrimSpace(tmp[1])
5672

5773
tmp = strings.SplitN(val, "<", 2)
74+
if len(tmp) < 2 {
75+
return ""
76+
}
5877
val = strings.TrimSpace(tmp[0])
5978
val = strings.Trim(val, "\"")
6079

6180
if strings.EqualFold(val, "") {
6281
val = tmp[1]
6382
val = strings.Trim(val, ">")
6483
tmp = strings.SplitN(val, "@", 2)
65-
return tmp[0]
84+
if len(tmp) > 0 {
85+
return tmp[0]
86+
}
87+
return ""
6688
}
6789

6890
if strings.Contains(val, "=?utf-8?B?") || strings.Contains(val, "=?UTF-8?B?") {
@@ -78,17 +100,57 @@ func GetMailFromInContent(content string) string {
78100
return val
79101
}
80102

81-
func GetMailSend(from string, to string, subject string, msg string) (string, error) {
82-
data, err := ioutil.ReadFile(conf.WorkDir() + "/conf/tpl/send.tpl")
103+
// 模板缓存
104+
var (
105+
sendTemplate string
106+
returnTemplate string
107+
returnHtmlTemplate string
108+
templateCacheLoaded bool
109+
)
110+
111+
// loadTemplates 加载模板文件到缓存
112+
func loadTemplates() error {
113+
if templateCacheLoaded {
114+
return nil
115+
}
116+
117+
workDir := conf.WorkDir()
118+
119+
// 加载发送模板
120+
data, err := ioutil.ReadFile(workDir + "/conf/tpl/send.tpl")
121+
if err != nil {
122+
return err
123+
}
124+
sendTemplate = string(data)
125+
126+
// 加载退信模板
127+
data, err = ioutil.ReadFile(workDir + "/conf/tpl/return_to_sender.tpl")
128+
if err != nil {
129+
return err
130+
}
131+
returnTemplate = string(data)
132+
133+
// 加载退信HTML模板
134+
data, err = ioutil.ReadFile(workDir + "/conf/tpl/return_to_sender_html.tpl")
83135
if err != nil {
136+
return err
137+
}
138+
returnHtmlTemplate = string(data)
139+
140+
templateCacheLoaded = true
141+
return nil
142+
}
143+
144+
func GetMailSend(from string, to string, subject string, msg string) (string, error) {
145+
if err := loadTemplates(); err != nil {
84146
return "", err
85147
}
86148

87149
sendTime := time.Now().Format("Mon, 02 Jan 2006 15:04:05 -0700 (MST)")
88150
sendVersion := fmt.Sprintf("imail/%s", conf.App.Version)
89151
boundaryRand := tools.RandString(20)
90152

91-
content := strings.Replace(string(data), "{MAIL_FROM}", from, -1)
153+
content := strings.Replace(sendTemplate, "{MAIL_FROM}", from, -1)
92154
content = strings.Replace(content, "{RCPT_TO}", to, -1)
93155
content = strings.Replace(content, "{SUBJECT}", subject, -1)
94156
content = strings.Replace(content, "{TIME}", sendTime, -1)
@@ -101,29 +163,23 @@ func GetMailSend(from string, to string, subject string, msg string) (string, er
101163

102164
// 邮件退信模板
103165
func GetMailReturnToSender(mailFrom, rcptTo string, err_to_mail string, err_content string, msg string) (string, error) {
166+
if err := loadTemplates(); err != nil {
167+
return "", err
168+
}
169+
104170
sendSubject := GetMailSubject(err_content)
105171

106172
sendTime := time.Now().Format("Mon, 02 Jan 2006 15:04:05 -0700 (MST)")
107173
sendVersion := fmt.Sprintf("imail/%s", conf.App.Version)
108174
boundaryRand := tools.RandString(20)
109175

110-
data, err := ioutil.ReadFile(conf.WorkDir() + "/conf/tpl/return_to_sender.tpl")
111-
if err != nil {
112-
return "", err
113-
}
114-
115-
dataHtml, err := ioutil.ReadFile(conf.WorkDir() + "/conf/tpl/return_to_sender_html.tpl")
116-
if err != nil {
117-
return "", err
118-
}
119-
120-
contentHtml := strings.Replace(string(dataHtml), "{TILTE}", "sc", -1)
176+
contentHtml := strings.Replace(returnHtmlTemplate, "{TILTE}", "sc", -1)
121177
contentHtml = strings.Replace(contentHtml, "{ERR_MSG}", msg, -1)
122178
contentHtml = strings.Replace(contentHtml, "{SEND_SUBJECT}", sendSubject, -1)
123179
contentHtml = strings.Replace(contentHtml, "{ERR_TO_MAIL}", err_to_mail, -1)
124180
contentHtml = strings.Replace(contentHtml, "{TIME}", sendTime, -1)
125181

126-
content := strings.Replace(string(data), "{MAIL_FROM}", mailFrom, -1)
182+
content := strings.Replace(returnTemplate, "{MAIL_FROM}", mailFrom, -1)
127183
content = strings.Replace(content, "{RCPT_TO}", rcptTo, -1)
128184
content = strings.Replace(content, "{SUBJECT}", "系统退信", -1)
129185
content = strings.Replace(content, "{TIME}", sendTime, -1)

0 commit comments

Comments
 (0)