Support send email with ssl
This commit is contained in:
@ -74,12 +74,13 @@ func (c AdminEmail) DoToImage(toImageBinPath string) revel.Result {
|
||||
return c.RenderJson(re)
|
||||
}
|
||||
|
||||
func (c AdminEmail) Set(emailHost, emailPort, emailUsername, emailPassword string) revel.Result {
|
||||
func (c AdminEmail) Set(emailHost, emailPort, emailUsername, emailPassword, emailSSL string) revel.Result {
|
||||
re := info.NewRe()
|
||||
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailHost", emailHost)
|
||||
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailPort", emailPort)
|
||||
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailUsername", emailUsername)
|
||||
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailPassword", emailPassword)
|
||||
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "emailSSL", emailSSL)
|
||||
|
||||
return c.RenderJson(re)
|
||||
}
|
||||
|
@ -49,7 +49,8 @@ func (this *ConfigService) InitGlobalConfigs() bool {
|
||||
this.adminUserId = userInfo.UserId.Hex()
|
||||
|
||||
configs := []info.Config{}
|
||||
db.ListByQ(db.Configs, bson.M{"UserId": userInfo.UserId}, &configs)
|
||||
// db.ListByQ(db.Configs, bson.M{"UserId": userInfo.UserId}, &configs)
|
||||
db.ListByQ(db.Configs, bson.M{}, &configs)
|
||||
|
||||
for _, config := range configs {
|
||||
if config.IsArr {
|
||||
@ -96,7 +97,7 @@ func (this *ConfigService) updateGlobalConfig(userId, key string, value interfac
|
||||
if _, ok := this.GlobalAllConfigs[key]; !ok {
|
||||
// 需要添加
|
||||
config := info.Config{ConfigId: bson.NewObjectId(),
|
||||
UserId: bson.ObjectIdHex(userId),
|
||||
UserId: bson.ObjectIdHex(userId), // 没用
|
||||
Key: key,
|
||||
IsArr: isArr,
|
||||
IsMap: isMap,
|
||||
@ -141,7 +142,8 @@ func (this *ConfigService) updateGlobalConfig(userId, key string, value interfac
|
||||
i["ValueStr"] = v
|
||||
this.GlobalStringConfigs[key] = v
|
||||
}
|
||||
return db.UpdateByQMap(db.Configs, bson.M{"UserId": bson.ObjectIdHex(userId), "Key": key}, i)
|
||||
// return db.UpdateByQMap(db.Configs, bson.M{"UserId": bson.ObjectIdHex(userId), "Key": key}, i)
|
||||
return db.UpdateByQMap(db.Configs, bson.M{"Key": key}, i)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/leanote/leanote/app/db"
|
||||
@ -29,12 +31,74 @@ var host = ""
|
||||
var emailPort = ""
|
||||
var username = ""
|
||||
var password = ""
|
||||
var ssl = false
|
||||
|
||||
func InitEmailFromDb() {
|
||||
host = configService.GetGlobalStringConfig("emailHost")
|
||||
emailPort = configService.GetGlobalStringConfig("emailPort")
|
||||
username = configService.GetGlobalStringConfig("emailUsername")
|
||||
password = configService.GetGlobalStringConfig("emailPassword")
|
||||
if configService.GetGlobalStringConfig("emailSSL") == "1" {
|
||||
ssl = true
|
||||
}
|
||||
}
|
||||
|
||||
//return a smtp client
|
||||
func dial(addr string) (*smtp.Client, error) {
|
||||
conn, err := tls.Dial("tcp", addr, nil)
|
||||
if err != nil {
|
||||
LogW("Dialing Error:", err)
|
||||
return nil, err
|
||||
}
|
||||
//分解主机端口字符串
|
||||
host, _, _ := net.SplitHostPort(addr)
|
||||
return smtp.NewClient(conn, host)
|
||||
}
|
||||
|
||||
func SendEmailWithSSL (auth smtp.Auth, to []string, msg []byte) (err error) {
|
||||
//create smtp client
|
||||
c, err := dial(host + ":" + emailPort)
|
||||
if err != nil {
|
||||
LogW("Create smpt client error:", err)
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if auth != nil {
|
||||
if ok, _ := c.Extension("AUTH"); ok {
|
||||
if err = c.Auth(auth); err != nil {
|
||||
LogW("Error during AUTH", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = c.Mail(username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, addr := range to {
|
||||
if err = c.Rcpt(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
w, err := c.Data()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Quit()
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
msg := []byte("To: " + to + "\r\nFrom: " + username + "<" + username + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
|
||||
send_to := strings.Split(to, ";")
|
||||
err := smtp.SendMail(host+":"+emailPort, auth, username, send_to, msg)
|
||||
|
||||
var err error
|
||||
if ssl {
|
||||
err = SendEmailWithSSL(auth, send_to, msg)
|
||||
} else {
|
||||
Log("no ssl")
|
||||
err = smtp.SendMail(host + ":" + emailPort, auth, username, send_to, msg)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
e = fmt.Sprint(err)
|
||||
|
25
app/tests/config_test.go
Normal file
25
app/tests/config_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"github.com/revel/revel"
|
||||
"github.com/leanote/leanote/app/db"
|
||||
"testing"
|
||||
// . "github.com/leanote/leanote/app/lea"
|
||||
"github.com/leanote/leanote/app/service"
|
||||
// "gopkg.in/mgo.v2"
|
||||
// "fmt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
revel.Init("dev", "github.com/leanote/leanote", "/Users/life/Documents/Go/package_base/src")
|
||||
db.Init("mongodb://localhost:27017/leanote", "leanote")
|
||||
service.InitService()
|
||||
service.ConfigS.InitGlobalConfigs()
|
||||
}
|
||||
|
||||
// 测试登录
|
||||
func TestSendMail(t *testing.T) {
|
||||
ok, err := service.EmailS.SendEmail("life@leanote.com", "你好", "你好吗")
|
||||
t.Log(ok)
|
||||
t.Log(err)
|
||||
}
|
@ -24,6 +24,10 @@
|
||||
<label>Password</label>
|
||||
<input type="text" class="form-control" name="emailPassword" value="{{.str.emailPassword}}" placeholder="">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>SSL ?</label>
|
||||
<input type="checkbox" class="form-control" name="emailSSL" {{if .str.emailSSL}}checked="checked"{{end}} value="1">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer class="panel-footer text-right bg-light lter">
|
||||
|
Reference in New Issue
Block a user