mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
Merge branch 'master' of https://github.com/gogf/gf
This commit is contained in:
24
.example/net/gsmtp/gsmtp_sendMail.go
Normal file
24
.example/net/gsmtp/gsmtp_sendMail.go
Normal file
@ -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! <br><br> This is body"))
|
||||
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user