Skip to content

Commit f49624d

Browse files
committed
Support send email with ssl
1 parent 816af11 commit f49624d

5 files changed

Lines changed: 108 additions & 5 deletions

File tree

app/controllers/admin/AdminEmailController.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ func (c AdminEmail) DoToImage(toImageBinPath string) revel.Result {
7474
return c.RenderJson(re)
7575
}
7676

77-
func (c AdminEmail) Set(emailHost, emailPort, emailUsername, emailPassword string) revel.Result {
77+
func (c AdminEmail) Set(emailHost, emailPort, emailUsername, emailPassword, emailSSL string) revel.Result {
7878
re := info.NewRe()
7979
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailHost", emailHost)
8080
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailPort", emailPort)
8181
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailUsername", emailUsername)
8282
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailPassword", emailPassword)
83+
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailSSL", emailSSL)
8384

8485
return c.RenderJson(re)
8586
}

app/service/ConfigService.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func (this *ConfigService) InitGlobalConfigs() bool {
4949
this.adminUserId = userInfo.UserId.Hex()
5050

5151
configs := []info.Config{}
52-
db.ListByQ(db.Configs, bson.M{"UserId": userInfo.UserId}, &configs)
52+
// db.ListByQ(db.Configs, bson.M{"UserId": userInfo.UserId}, &configs)
53+
db.ListByQ(db.Configs, bson.M{}, &configs)
5354

5455
for _, config := range configs {
5556
if config.IsArr {
@@ -96,7 +97,7 @@ func (this *ConfigService) updateGlobalConfig(userId, key string, value interfac
9697
if _, ok := this.GlobalAllConfigs[key]; !ok {
9798
// 需要添加
9899
config := info.Config{ConfigId: bson.NewObjectId(),
99-
UserId: bson.ObjectIdHex(userId),
100+
UserId: bson.ObjectIdHex(userId), // 没用
100101
Key: key,
101102
IsArr: isArr,
102103
IsMap: isMap,
@@ -141,7 +142,8 @@ func (this *ConfigService) updateGlobalConfig(userId, key string, value interfac
141142
i["ValueStr"] = v
142143
this.GlobalStringConfigs[key] = v
143144
}
144-
return db.UpdateByQMap(db.Configs, bson.M{"UserId": bson.ObjectIdHex(userId), "Key": key}, i)
145+
// return db.UpdateByQMap(db.Configs, bson.M{"UserId": bson.ObjectIdHex(userId), "Key": key}, i)
146+
return db.UpdateByQMap(db.Configs, bson.M{"Key": key}, i)
145147
}
146148
}
147149

app/service/EmailService.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package service
22

33
import (
4+
"crypto/tls"
5+
"net"
46
"bytes"
57
"fmt"
68
"github.com/leanote/leanote/app/db"
@@ -29,12 +31,74 @@ var host = ""
2931
var emailPort = ""
3032
var username = ""
3133
var password = ""
34+
var ssl = false
3235

3336
func InitEmailFromDb() {
3437
host = configService.GetGlobalStringConfig("emailHost")
3538
emailPort = configService.GetGlobalStringConfig("emailPort")
3639
username = configService.GetGlobalStringConfig("emailUsername")
3740
password = configService.GetGlobalStringConfig("emailPassword")
41+
if configService.GetGlobalStringConfig("emailSSL") == "1" {
42+
ssl = true
43+
}
44+
}
45+
46+
//return a smtp client
47+
func dial(addr string) (*smtp.Client, error) {
48+
conn, err := tls.Dial("tcp", addr, nil)
49+
if err != nil {
50+
LogW("Dialing Error:", err)
51+
return nil, err
52+
}
53+
//分解主机端口字符串
54+
host, _, _ := net.SplitHostPort(addr)
55+
return smtp.NewClient(conn, host)
56+
}
57+
58+
func SendEmailWithSSL (auth smtp.Auth, to []string, msg []byte) (err error) {
59+
//create smtp client
60+
c, err := dial(host + ":" + emailPort)
61+
if err != nil {
62+
LogW("Create smpt client error:", err)
63+
return err
64+
}
65+
defer c.Close()
66+
67+
if auth != nil {
68+
if ok, _ := c.Extension("AUTH"); ok {
69+
if err = c.Auth(auth); err != nil {
70+
LogW("Error during AUTH", err)
71+
return err
72+
}
73+
}
74+
}
75+
76+
if err = c.Mail(username); err != nil {
77+
return err
78+
}
79+
80+
for _, addr := range to {
81+
if err = c.Rcpt(addr); err != nil {
82+
return err
83+
}
84+
}
85+
86+
w, err := c.Data()
87+
if err != nil {
88+
return err
89+
}
90+
91+
_, err = w.Write(msg)
92+
if err != nil {
93+
return err
94+
}
95+
96+
err = w.Close()
97+
if err != nil {
98+
return err
99+
}
100+
101+
return c.Quit()
38102
}
39103

40104
func (this *EmailService) SendEmail(to, subject, body string) (ok bool, e string) {
@@ -57,7 +121,14 @@ func (this *EmailService) SendEmail(to, subject, body string) (ok bool, e string
57121

58122
msg := []byte("To: " + to + "\r\nFrom: " + username + "<" + username + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
59123
send_to := strings.Split(to, ";")
60-
err := smtp.SendMail(host+":"+emailPort, auth, username, send_to, msg)
124+
125+
var err error
126+
if ssl {
127+
err = SendEmailWithSSL(auth, send_to, msg)
128+
} else {
129+
Log("no ssl")
130+
err = smtp.SendMail(host + ":" + emailPort, auth, username, send_to, msg)
131+
}
61132

62133
if err != nil {
63134
e = fmt.Sprint(err)

app/tests/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tests
2+
3+
import (
4+
"github.com/revel/revel"
5+
"github.com/leanote/leanote/app/db"
6+
"testing"
7+
// . "github.com/leanote/leanote/app/lea"
8+
"github.com/leanote/leanote/app/service"
9+
// "gopkg.in/mgo.v2"
10+
// "fmt"
11+
)
12+
13+
func init() {
14+
revel.Init("dev", "github.com/leanote/leanote", "/Users/life/Documents/Go/package_base/src")
15+
db.Init("mongodb://localhost:27017/leanote", "leanote")
16+
service.InitService()
17+
service.ConfigS.InitGlobalConfigs()
18+
}
19+
20+
// 测试登录
21+
func TestSendMail(t *testing.T) {
22+
ok, err := service.EmailS.SendEmail("life@leanote.com", "你好", "你好吗")
23+
t.Log(ok)
24+
t.Log(err)
25+
}

app/views/admin/email/set.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<label>Password</label>
2525
<input type="text" class="form-control" name="emailPassword" value="{{.str.emailPassword}}" placeholder="">
2626
</div>
27+
<div class="form-group">
28+
<label>SSL ?</label>
29+
<input type="checkbox" class="form-control" name="emailSSL" {{if .str.emailSSL}}checked="checked"{{end}} value="1">
30+
</div>
2731
</div>
2832

2933
<footer class="panel-footer text-right bg-light lter">

0 commit comments

Comments
 (0)