Browse Source

fix: send smtp mail using ssl

master 1.5.4
OhYee 8 months ago
parent
commit
0fc51bc373
  1. 4
      api/pkg/comment/main.go
  2. 74
      api/pkg/email/main.go
  3. 16
      api/pkg/email/main_test.go
  4. 1
      api/pkg/variable/type.go
  5. 1
      go.mod

4
api/pkg/comment/main.go

@ -251,7 +251,7 @@ func GetInfo(url string, id primitive.ObjectID) Info { @@ -251,7 +251,7 @@ func GetInfo(url string, id primitive.ObjectID) Info {
// SendEmail for comment
func SendEmail(url string, raw string, html string, replyObjectID primitive.ObjectID) {
emailAddr, user, username, password, address, root, blogName, err := email.GetSMTPData()
emailAddr, user, username, password, address, ssl, root, blogName, err := email.GetSMTPData()
if err != nil {
return
}
@ -270,7 +270,7 @@ func SendEmail(url string, raw string, html string, replyObjectID primitive.Obje @@ -270,7 +270,7 @@ func SendEmail(url string, raw string, html string, replyObjectID primitive.Obje
output.Debug("Send email to %+v", to)
err = email.Send(
address, username, user, password, "博客评论通知",
address, username, user, password, ssl, "博客评论通知",
fmt.Sprintf(
"<html><body>您在<a href='%s'>《%s》</a>( %s )的评论收到一条回复<br><br>%s</body></html>",
root+url, info.Title, root+url, html,

74
api/pkg/email/main.go

@ -1,18 +1,70 @@ @@ -1,18 +1,70 @@
package email
import (
"crypto/tls"
"fmt"
"net"
"net/smtp"
"strings"
"github.com/OhYee/blotter/api/pkg/variable"
"github.com/pkg/errors"
)
// Send email
func Send(host, username, user, password, subject, body string, to ...string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
func Send(hostWithPort, username, user, password string, ssl bool, subject, body string, to ...string) (err error) {
defer func() {
if err != nil {
err = errors.Wrap(err, "Send email failed")
}
}()
hp := strings.Split(hostWithPort, ":")
host := hp[0]
// make connection
var conn net.Conn
if ssl {
conn, err = tls.Dial("tcp", hostWithPort, nil)
if err != nil {
return
}
} else {
conn, err = net.Dial("tcp", hostWithPort)
if err != nil {
return
}
}
fmt.Println("a ...interface{12}")
// initiate connection
client, err := smtp.NewClient(conn, host)
if err != nil {
return
}
// smtp auth
auth := smtp.PlainAuth("", user, password, host)
if err = client.Auth(auth); err != nil {
return
}
// set sender
if err = client.Mail(user); err != nil {
return
}
// set receiver
for _, receiver := range to {
if err = client.Rcpt(receiver); err != nil {
return
}
}
// Write data
w, err := client.Data()
if err != nil {
return
}
msg := []byte(fmt.Sprintf(
"To: %s\r\nFrom: %s<%s>\r\nSubject: %s\r\nContent-Type: text/html;charset=UTF-8\r\n\r\n%s",
strings.Join(to, ","),
@ -20,14 +72,21 @@ func Send(host, username, user, password, subject, body string, to ...string) er @@ -20,14 +72,21 @@ func Send(host, username, user, password, subject, body string, to ...string) er
subject, body,
))
err := smtp.SendMail(host, auth, user, to, msg)
if _, err = w.Write(msg); err != nil {
return
}
if err = w.Close(); err != nil {
return
}
err = client.Quit()
return err
}
// GetSMTPData get smtp information from database
func GetSMTPData() (email, user, username, password, address, root, blogName string, err error) {
func GetSMTPData() (email, user, username, password, address string, ssl bool, root, blogName string, err error) {
v, err := variable.Get(
"email", "smtp_user", "smtp_password", "smtp_address",
"email", "smtp_user", "smtp_password", "smtp_address", "ssl",
"smtp_username", "root", "blog_name",
)
if err != nil {
@ -49,6 +108,9 @@ func GetSMTPData() (email, user, username, password, address, root, blogName str @@ -49,6 +108,9 @@ func GetSMTPData() (email, user, username, password, address, root, blogName str
if err = v.SetString("smtp_address", &address); err != nil {
return
}
if err = v.SetBool("smtp_ssl", &ssl, false); err != nil {
return
}
if err = v.SetString("root", &root); err != nil {
return
}

16
api/pkg/email/main_test.go

@ -10,18 +10,26 @@ import ( @@ -10,18 +10,26 @@ import (
func TestSend(t *testing.T) {
for _, arg := range os.Args {
if arg == "mail" {
email, user, username, password, address, root, blogName, err := GetSMTPData()
// email, user, username, password, address, root, blogName, err := GetSMTPData()
email := "me@ohyee.cc"
user := "sender@oyohyee.com"
username := "sender@oyohyee.com"
password := "QKpyTQc1RHJ8"
address := "smtppro.zoho.com:465"
root := "http://localhost:8080"
blogName := "oyohyee"
var err error = nil
if err != nil {
t.Errorf("%s", err)
t.Errorf("%+v", err)
t.FailNow()
}
err = Send(address, username, user, password, "测试", fmt.Sprintf(
err = Send(address, username, user, password, true, "测试", fmt.Sprintf(
"测试邮件功能 %s <a href='%s'>%s</a>",
time.Now().Format("2006-01-02 15:04:05"), root, blogName,
), email)
if err != nil {
t.Errorf("%s", err)
t.Errorf("%+v", err)
t.FailNow()
}
break

1
api/pkg/variable/type.go

@ -190,6 +190,7 @@ type BlotterVariables struct { @@ -190,6 +190,7 @@ type BlotterVariables struct {
SMTPPassword string `json:"smtp_password" bson:"smtp_password"`
SMTPUser string `json:"smtp_user" bson:"smtp_user"`
SMTPUsername string `json:"smtp_username" bson:"smtp_username"`
SMTPSSL bool `json:"smtp_ssl" bson:"smtp_ssl"`
View int `json:"view" bson:"view"`
Vmess string `json:"vmess" bson:"vmess"`
Zhihu string `json:"zhihu" bson:"zhihu"`

1
go.mod

@ -27,6 +27,7 @@ require ( @@ -27,6 +27,7 @@ require (
github.com/klauspost/compress v1.11.3 // indirect
github.com/mitchellh/mapstructure v1.4.0
github.com/mmcdole/gofeed v1.1.3
github.com/pkg/errors v0.9.1
github.com/qiniu/api.v7/v7 v7.7.0
github.com/refraction-networking/utls v0.0.0-20201112193908-f7e7360167ed // indirect
github.com/robfig/cron v1.2.0

Loading…
Cancel
Save