From fb8aac38dd148bab05781143ca796a5255a93b92 Mon Sep 17 00:00:00 2001 From: LetMyPplGo <54865472+LetMyPplGo@users.noreply.github.com> Date: Fri, 6 Sep 2019 00:53:23 +0300 Subject: [PATCH 1/5] server address verification simple Email address check for from & to default port=25 for server address --- net/gsmtp/gsmtp.go | 51 ++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/net/gsmtp/gsmtp.go b/net/gsmtp/gsmtp.go index f66b3c2c3..a1e19961b 100644 --- a/net/gsmtp/gsmtp.go +++ b/net/gsmtp/gsmtp.go @@ -18,6 +18,7 @@ import ( "strings" ) +// SMTP is the structure for smtp connection type SMTP struct { Address string Username string @@ -38,35 +39,45 @@ func New(address, username, password string) *SMTP { // and then sends an email from address from, to addresses to, with // message msg. func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) error { - if s.Address == "" { - return fmt.Errorf("address is necessary") - } + server := "" + address := "" hp := strings.Split(s.Address, ":") - if len(hp) != 2 { - return fmt.Errorf("address format error") - } - - arr := strings.Split(tos, ";") - count := len(arr) - safeArr := make([]string, 0, count) - for i := 0; i < count; i++ { - if arr[i] == "" { - continue + if (s.Address == "") || (len(hp) > 2) { + return fmt.Errorf("Server address is either empty or incorrect: %s", s.Address) + } else if len(hp) == 1 { + server = s.Address + address = server + ":25" + } else if len(hp) == 2 { + if (hp[0] == "") || (hp[1] == "") { + return fmt.Errorf("Server address is either empty or incorrect: %s", s.Address) } - safeArr = append(safeArr, arr[i]) + server = hp[0] + address = s.Address } - if len(safeArr) == 0 { - return fmt.Errorf("tos invalid") + tosArr := []string{} + arr := strings.Split(tos, ";") + for _, to := range arr { + // TODO: replace with regex + if strings.Contains(to, "@") { + tosArr = append(tosArr, to) + } + } + + if len(tosArr) == 0 { + return fmt.Errorf("tos if invalid: %s", tos) + } + + if !strings.Contains(from, "@") { + return fmt.Errorf("from is invalid: %s", from) } - tos = strings.Join(safeArr, ";") b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") header := make(map[string]string) header["From"] = from - header["To"] = tos + header["To"] = strings.Join(tosArr, ";") header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject))) header["MIME-Version"] = "1.0" @@ -84,6 +95,6 @@ func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) } message += "\r\n" + b64.EncodeToString([]byte(body)) - auth := smtp.PlainAuth("", s.Username, s.Password, hp[0]) - return smtp.SendMail(s.Address, auth, from, strings.Split(tos, ";"), []byte(message)) + auth := smtp.PlainAuth("", s.Username, s.Password, server) + return smtp.SendMail(address, auth, from, tosArr, []byte(message)) } From 57244a9553a5a965432f9c4de5048e3a88462cf5 Mon Sep 17 00:00:00 2001 From: LetMyPplGo <54865472+LetMyPplGo@users.noreply.github.com> Date: Fri, 6 Sep 2019 01:18:41 +0300 Subject: [PATCH 2/5] added the example for gsmtp --- .example/net/gsmtp/gsmtp_sendMail.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .example/net/gsmtp/gsmtp_sendMail.go diff --git a/.example/net/gsmtp/gsmtp_sendMail.go b/.example/net/gsmtp/gsmtp_sendMail.go new file mode 100644 index 000000000..8496c43e9 --- /dev/null +++ b/.example/net/gsmtp/gsmtp_sendMail.go @@ -0,0 +1,24 @@ +// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. +package main + +import ( + "fmt" + + "github.com/gogf/gf/net/gsmtp" +) + +func main() { + + // create the SMTP connection + smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword") + // or you can specify the port explicitly + // smtpConnection := smtp.New("smtp.exmail.qq.com:25", "smtpUser@smtp.exmail.qq.com", "smtpPassword") + + // send the Email + fmt.Println(smtpConnection.SendMail("sender@local.host", "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi!

This is body")) + +} From 2eb0511ef5e7d454d4fd843aa685cd1a01d55946 Mon Sep 17 00:00:00 2001 From: wenzi Date: Sun, 8 Sep 2019 23:24:00 +0800 Subject: [PATCH 3/5] Failed to return when Gini file grammar error Failed to return when Gini file grammar error --- encoding/gini/gini.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/encoding/gini/gini.go b/encoding/gini/gini.go index a756a7413..deb9c523f 100644 --- a/encoding/gini/gini.go +++ b/encoding/gini/gini.go @@ -16,7 +16,7 @@ import ( "strings" ) -// Decode converts INI format to map. +//Decode converts INI format to map func Decode(data []byte) (res map[string]interface{}, err error) { res = make(map[string]interface{}) fieldMap := make(map[string]interface{}) @@ -72,10 +72,13 @@ func Decode(data []byte) (res map[string]interface{}, err error) { } + if haveSection == false { + return nil, fmt.Errorf("Failed to parse INI file, not found section") + } return res, nil } -// Encode converts map to INI format. +//Encode converts map to INI format func Encode(data map[string]interface{}) (res []byte, err error) { w := new(bytes.Buffer) @@ -102,11 +105,12 @@ func Encode(data map[string]interface{}) (res []byte, err error) { return res, nil } -// ToJson convert INI format to JSON. +//ToJson convert INI format to JSON func ToJson(data []byte) (res []byte, err error) { iniMap, err := Decode(data) if err != nil { return nil, err } + return json.Marshal(iniMap) } From ed3fc529ed9a4d45cde7dcbf8243fedf69b10eb5 Mon Sep 17 00:00:00 2001 From: wenzi Date: Sun, 8 Sep 2019 23:25:45 +0800 Subject: [PATCH 4/5] Failed to return when Gini file grammar error Failed to return when Gini file grammar error --- encoding/gini/gini_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/encoding/gini/gini_test.go b/encoding/gini/gini_test.go index da75c22a3..29a60dbd9 100644 --- a/encoding/gini/gini_test.go +++ b/encoding/gini/gini_test.go @@ -7,7 +7,6 @@ package gini_test import ( - "fmt" "github.com/gogf/gf/encoding/gini" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/test/gtest" @@ -39,7 +38,6 @@ func TestDecode(t *testing.T) { if err != nil { gtest.Fatal(err) } - fmt.Println(res) gtest.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1") gtest.Assert(res["addr"].(map[string]interface{})["port"], "9001") gtest.Assert(res["DBINFO"].(map[string]interface{})["user"], "root") @@ -47,6 +45,15 @@ func TestDecode(t *testing.T) { gtest.Assert(res["键"].(map[string]interface{})["呵呵"], "值") }) + gtest.Case(t, func() { + errContent := ` + a = b +` + _, err := gini.Decode([]byte(errContent)) + if err == nil { + gtest.Fatal(err) + } + }) } func TestEncode(t *testing.T) { From d9e7d4249f5b7d876f2ca4531f9540196148bd41 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Wed, 11 Sep 2019 16:48:00 +0800 Subject: [PATCH 5/5] improve dbBase.doBatchInsert() --- database/gdb/gdb_base.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/database/gdb/gdb_base.go b/database/gdb/gdb_base.go index 18d1019ce..30f32c6c4 100644 --- a/database/gdb/gdb_base.go +++ b/database/gdb/gdb_base.go @@ -484,15 +484,16 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt } // 构造批量写入数据格式(注意map的遍历是无序的) batchNum := gDEFAULT_BATCH_NUM - if len(batch) > 0 { + if len(batch) > 0 && batch[0] > 0 { batchNum = batch[0] } - for i := 0; i < len(listMap); i++ { + listMapLen := len(listMap) + for i := 0; i < listMapLen; i++ { for _, k := range keys { params = append(params, convertParam(listMap[i][k])) } values = append(values, valueHolderStr) - if len(values) == batchNum { + if len(values) == batchNum || (i == listMapLen-1 && len(values) > 0) { r, err := bs.db.doExec(link, fmt.Sprintf("%s INTO %s(%s) VALUES%s %s", operation, table, keyStr, strings.Join(values, ","), updateStr), @@ -510,22 +511,6 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt values = values[:0] } } - // 处理最后不构成指定批量的数据 - if len(values) > 0 { - r, err := bs.db.doExec(link, fmt.Sprintf("%s INTO %s(%s) VALUES%s %s", - operation, table, keyStr, strings.Join(values, ","), - updateStr), - params...) - if err != nil { - return r, err - } - if n, err := r.RowsAffected(); err != nil { - return r, err - } else { - batchResult.lastResult = r - batchResult.rowsAffected += n - } - } return batchResult, nil }