Support send email with ssl

This commit is contained in:
lealife
2016-10-29 16:38:13 +08:00
parent 816af11db2
commit f49624d3eb
5 changed files with 108 additions and 5 deletions

View File

@@ -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)
}
}

View File

@@ -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)