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

Skip to content

Commit d23a492

Browse files
committed
发邮件支持 tls 加密
1 parent 6612f00 commit d23a492

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

config/env.sample.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ smtp_username = [email protected]
3333
smtp_password = xxx
3434
smtp_host = smtp.exmail.qq.com
3535
smtp_port = 25
36+
; 是否 tls 安全传输
37+
tls = 0
3638
; 发件人
3739
from_email = [email protected]
3840

getpkg.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ else
3131
"github.com/robfig/cron" "github.com/gorilla/sessions" "github.com/polaris1119/echoutils"
3232
"golang.org/x/net/websocket" "github.com/polaris1119/slices" "github.com/qiniu/api.v6"
3333
"github.com/polaris1119/times" "github.com/PuerkitoBio/goquery" "github.com/go-validator/validator"
34+
"github.com/polaris1119/email"
3435
"github.com/gorilla/schema" "github.com/facebookgo/grace/gracehttp")
3536

3637
for pkg in "${pkgs[@]}"; do

src/logic/email.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package logic
88

99
import (
1010
"bytes"
11+
"crypto/tls"
1112
"fmt"
1213
"global"
1314
"html/template"
@@ -16,6 +17,7 @@ import (
1617
"time"
1718

1819
"github.com/polaris1119/config"
20+
"github.com/polaris1119/email"
1921
"github.com/polaris1119/goutils"
2022
"github.com/polaris1119/logger"
2123

@@ -29,24 +31,35 @@ type EmailLogic struct{}
2931
var DefaultEmail = EmailLogic{}
3032

3133
// SendMail 发送电子邮件
32-
func (EmailLogic) SendMail(subject, content string, tos []string) error {
34+
func (EmailLogic) SendMail(subject, content string, tos []string) (err error) {
3335
emailConfig, _ := config.ConfigFile.GetSection("email")
34-
message := `From: ` + WebsiteSetting.Name + `<` + emailConfig["from_email"] + `>
35-
To: ` + strings.Join(tos, ",") + `
36-
Subject: ` + subject + `
37-
Content-Type: text/html;charset=UTF-8
3836

39-
` + content
37+
e := email.NewEmail()
38+
e.From = WebsiteSetting.Name + ` <` + emailConfig["from_email"] + `>`
39+
e.To = tos
40+
e.Subject = subject
41+
e.HTML = []byte(content)
4042

41-
smtpAddr := emailConfig["smtp_host"] + ":" + emailConfig["smtp_port"]
4243
auth := smtp.PlainAuth("", emailConfig["smtp_username"], emailConfig["smtp_password"], emailConfig["smtp_host"])
43-
err := smtp.SendMail(smtpAddr, auth, emailConfig["from_email"], tos, []byte(message))
44+
smtpAddr := emailConfig["smtp_host"] + ":" + emailConfig["smtp_port"]
45+
46+
if goutils.MustBool(emailConfig["tls"]) {
47+
err = e.Send(smtpAddr, auth)
48+
} else {
49+
tlsConfig := &tls.Config{
50+
InsecureSkipVerify: true,
51+
ServerName: emailConfig["smtp_host"],
52+
}
53+
54+
err = e.SendWithTLS(smtpAddr, auth, tlsConfig)
55+
}
56+
4457
if err != nil {
4558
logger.Errorln("Send Mail to", strings.Join(tos, ","), "error:", err)
46-
return err
59+
return
4760
}
4861
logger.Infoln("Send Mail to", strings.Join(tos, ","), "Successfully")
49-
return nil
62+
return
5063
}
5164

5265
// 保存uuid和email的对应关系(TODO:重启如何处理)

src/vendor/manifest

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@
266266
"revision": "5e14d4b37f74bad4fee32be3674c6d7bf7c1f5c3",
267267
"branch": "master"
268268
},
269+
{
270+
"importpath": "github.com/polaris1119/email",
271+
"repository": "https://github.com/polaris1119/email",
272+
"revision": "fab6eb4568abf395404252a598ff32d7fe445dbf",
273+
"branch": "master"
274+
},
269275
{
270276
"importpath": "github.com/polaris1119/goutils",
271277
"repository": "https://github.com/polaris1119/goutils",

0 commit comments

Comments
 (0)