From 6ad7baeb2c27c74e21633f88bbe971c03bf1fe12 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 16 Nov 2021 23:39:45 +0800 Subject: [PATCH] remove gsmtp --- net/gsmtp/gsmtp.go | 118 ---------------------------------------- net/gsmtp/gsmtp_test.go | 78 -------------------------- text/gregex/gregex.go | 2 +- 3 files changed, 1 insertion(+), 197 deletions(-) delete mode 100644 net/gsmtp/gsmtp.go delete mode 100644 net/gsmtp/gsmtp_test.go diff --git a/net/gsmtp/gsmtp.go b/net/gsmtp/gsmtp.go deleted file mode 100644 index d7c437536..000000000 --- a/net/gsmtp/gsmtp.go +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright GoFrame Author(https://goframe.org). 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 gsmtp provides a simple SMTP client to access remote mail server. -// -// Eg: -// s := smtp.New("smtp.exmail.qq.com:25", "notify@a.com", "password") -// glog.Print(s.SendMail("notify@a.com", "ulric@b.com;rain@c.com", "subject", "body, red")) -package gsmtp - -import ( - "encoding/base64" - "fmt" - "net/smtp" - "strings" - - "github.com/gogf/gf/v2/errors/gcode" - "github.com/gogf/gf/v2/errors/gerror" -) - -// SMTP is the structure for smtp connection. -type SMTP struct { - Address string - Username string - Password string -} - -// New creates and returns a new SMTP object. -func New(address, username, password string) *SMTP { - return &SMTP{ - Address: address, - Username: username, - Password: password, - } -} - -var ( - // contentEncoding is the BASE64 encoding object for mail content. - contentEncoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") -) - -// SendMail connects to the server at addr, switches to TLS if -// possible, authenticates with the optional mechanism an if possible, -// and then sends an email from address `from`, to addresses `to`, with -// message msg. -// -// The parameter `contentType` specifies the content type of the mail, eg: html. -func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) error { - var ( - server = "" - address = "" - hp = strings.Split(s.Address, ":") - ) - if s.Address == "" || len(hp) > 2 { - return gerror.NewCodef( - gcode.CodeInvalidParameter, - "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 gerror.NewCodef( - gcode.CodeInvalidParameter, - "server address is either empty or incorrect: %s", - s.Address, - ) - } - server = hp[0] - address = s.Address - } - var ( - 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 gerror.NewCodef(gcode.CodeInvalidParameter, `invalid parameter "tos": %s`, tos) - } - - if !strings.Contains(from, "@") { - return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid parameter "from": %s`, from) - } - - header := map[string]string{ - "From": from, - "To": strings.Join(tosArr, ";"), - "Subject": fmt.Sprintf("=?UTF-8?B?%s?=", contentEncoding.EncodeToString([]byte(subject))), - "MIME-Version": "1.0", - "Content-Type": "text/plain; charset=UTF-8", - "Content-Transfer-Encoding": "base64", - } - if len(contentType) > 0 && contentType[0] == "html" { - header["Content-Type"] = "text/html; charset=UTF-8" - } - message := "" - for k, v := range header { - message += fmt.Sprintf("%s: %s\r\n", k, v) - } - message += "\r\n" + contentEncoding.EncodeToString([]byte(body)) - return smtp.SendMail( - address, - smtp.PlainAuth("", s.Username, s.Password, server), - from, - tosArr, - []byte(message), - ) -} diff --git a/net/gsmtp/gsmtp_test.go b/net/gsmtp/gsmtp_test.go deleted file mode 100644 index dd0aea9c3..000000000 --- a/net/gsmtp/gsmtp_test.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright GoFrame Author(https://goframe.org). 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 gsmtp_test - -import ( - "strings" - "testing" - - "github.com/gogf/gf/v2/net/gsmtp" -) - -func TestAddress(t *testing.T) { - errMessage := "address is either empty or incorrect" - - errValues := []string{ - "", - ":", - ":25", - "localhost:", - "local.host:25:28", - } - - for _, errValue := range errValues { - smtpConnection := gsmtp.New(errValue, "smtpUser@smtp.exmail.qq.com", "smtpPassword") - res := smtpConnection.SendMail("sender@local.host", "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi!

This is body") - if !strings.Contains(res.Error(), errMessage) { - t.Errorf("Test failed on Address: %s", errValue) - } - } -} - -func TestFrom(t *testing.T) { - errMessage := `invalid parameter "from"` - - errValues := []string{ - "", - "qwerty", - // "qwe@rty@com", - // "@rty", - // "qwe@", - } - - for _, errValue := range errValues { - smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword") - res := smtpConnection.SendMail(errValue, "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi!

This is body") - if !strings.Contains(res.Error(), errMessage) { - t.Errorf("Test failed on From: %s", errValue) - } - } - -} - -func TestTos(t *testing.T) { - errMessage := `invalid parameter "tos"` - - errValues := []string{ - "", - "qwerty", - "qwe;rty", - "qwe;rty;com", - // "qwe@rty@com", - // "@rty", - // "qwe@", - } - - for _, errValue := range errValues { - smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword") - res := smtpConnection.SendMail("from@domain.com", errValue, "This is subject", "Hi!

This is body") - if !strings.Contains(res.Error(), errMessage) { - t.Errorf("Test failed on Tos: %s", errValue) - } - } - -} diff --git a/text/gregex/gregex.go b/text/gregex/gregex.go index f23fe5936..a1dfc9686 100644 --- a/text/gregex/gregex.go +++ b/text/gregex/gregex.go @@ -75,7 +75,7 @@ func MatchAllString(pattern string, src string) ([][]string, error) { } } -// Replace replace all matched `pattern` in bytes `src` with bytes `replace`. +// Replace replaces all matched `pattern` in bytes `src` with bytes `replace`. func Replace(pattern string, replace, src []byte) ([]byte, error) { if r, err := getRegexp(pattern); err == nil { return r.ReplaceAll(src, replace), nil