Update email.go
This commit is contained in:
parent
dccd66b852
commit
b065d6e5e8
@ -7,10 +7,40 @@ import (
|
|||||||
"net/smtp"
|
"net/smtp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
type loginAuth struct {
|
||||||
|
username, password string
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoginAuth(username, password string) smtp.Auth {
|
||||||
|
return &loginAuth{username, password}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||||||
|
return "LOGIN", []byte(a.username), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||||||
|
if more {
|
||||||
|
switch string(fromServer) {
|
||||||
|
case "Username:":
|
||||||
|
return []byte(a.username), nil
|
||||||
|
case "Password:":
|
||||||
|
return []byte(a.password), nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("Unknown from server")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
func SendEmail(subject string, receiver string, content string) error {
|
func SendEmail(subject string, receiver string, content string) error {
|
||||||
if SMTPFrom == "" { // for compatibility
|
if SMTPFrom == "" { // for compatibility
|
||||||
SMTPFrom = SMTPAccount
|
SMTPFrom = SMTPAccount
|
||||||
|
}
|
||||||
|
tlsconfig := &tls.Config {
|
||||||
|
ServerName: host,
|
||||||
}
|
}
|
||||||
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
||||||
mail := []byte(fmt.Sprintf("To: %s\r\n"+
|
mail := []byte(fmt.Sprintf("To: %s\r\n"+
|
||||||
@ -18,7 +48,8 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
"Subject: %s\r\n"+
|
"Subject: %s\r\n"+
|
||||||
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
|
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
|
||||||
receiver, SystemName, SMTPFrom, encodedSubject, content))
|
receiver, SystemName, SMTPFrom, encodedSubject, content))
|
||||||
auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer)
|
|
||||||
|
//auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer)
|
||||||
addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort)
|
addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort)
|
||||||
to := strings.Split(receiver, ";")
|
to := strings.Split(receiver, ";")
|
||||||
var err error
|
var err error
|
||||||
@ -34,6 +65,14 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
client, err := smtp.NewClient(conn, SMTPServer)
|
client, err := smtp.NewClient(conn, SMTPServer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
if err = client.StartTLS(tlsconfig); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
auth := LoginAuth(SMTPAccount, SMTPToken)
|
||||||
|
|
||||||
|
if err = client.Auth(auth); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
if err = client.Auth(auth); err != nil {
|
if err = client.Auth(auth); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user