Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce39c3b

Browse files
committed
每周精选,163用户通过163账号发送
1 parent 386e05a commit ce39c3b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

config/env.sample.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ smtp_port = 25
3737
; 发件人
3838
from_email = [email protected]
3939

40+
; 可选。163 订阅通知,163会限制发送次数
41+
[email.163]
42+
smtp_username = [email protected]
43+
smtp_password = xxx
44+
smtp_host = smtp.163.com
45+
smtp_port = 25
46+
4047
[security]
4148
; 退订邮件使用的 token key
4249
unsubscribe_token_key = $d6YPdcFlOROhl0Cz*

src/logic/email.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ Content-Type: text/html;charset=UTF-8
4848
return nil
4949
}
5050

51+
// sendMailBy163 通过163账号发送电子邮件
52+
func (self EmailLogic) sendMailBy163(subject, content string, tos []string) error {
53+
emailConfig, err := config.ConfigFile.GetSection("email.163")
54+
if err != nil {
55+
return self.SendMail(subject, content, tos)
56+
}
57+
fromEmail := config.ConfigFile.MustValue("email", "from_email")
58+
message := `From: Go语言中文网 | Golang中文社区 | Go语言学习园地<` + fromEmail + `>
59+
To: ` + strings.Join(tos, ",") + `
60+
Subject: ` + subject + `
61+
Content-Type: text/html;charset=UTF-8
62+
63+
` + content
64+
65+
smtpAddr := emailConfig["smtp_host"] + ":" + emailConfig["smtp_port"]
66+
auth := smtp.PlainAuth("", emailConfig["smtp_username"], emailConfig["smtp_password"], emailConfig["smtp_host"])
67+
err = smtp.SendMail(smtpAddr, auth, emailConfig["smtp_username"], tos, []byte(message))
68+
if err != nil {
69+
logger.Errorln("Send Mail to", strings.Join(tos, ","), "error:", err)
70+
return err
71+
}
72+
logger.Infoln("Send Mail to", strings.Join(tos, ","), "Successfully")
73+
return nil
74+
}
75+
5176
// 保存uuid和email的对应关系(TODO:重启如何处理)
5277
var regActivateCodeMap = map[string]string{}
5378

@@ -177,7 +202,11 @@ func (self EmailLogic) EmailNotice() {
177202
continue
178203
}
179204

180-
self.SendMail("每周精选", content, []string{user.Email})
205+
if strings.HasSuffix(user.Email, "@163.com") {
206+
self.sendMailBy163("每周精选", content, []string{user.Email})
207+
} else {
208+
self.SendMail("每周精选", content, []string{user.Email})
209+
}
181210

182211
// 控制发信速度
183212
time.Sleep(30 * time.Second)

0 commit comments

Comments
 (0)