Update email.go

This commit is contained in:
thinker007 2023-07-29 00:54:27 +08:00 committed by GitHub
parent b065d6e5e8
commit bb1ce3f744
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,8 @@ import (
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net"
"errors"
"net/smtp" "net/smtp"
"strings" "strings"
) )
@ -39,9 +41,7 @@ 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"+
"From: %s<%s>\r\n"+ "From: %s<%s>\r\n"+
@ -50,15 +50,17 @@ func SendEmail(subject string, receiver string, content string) error {
receiver, SystemName, SMTPFrom, encodedSubject, content)) receiver, SystemName, SMTPFrom, encodedSubject, content))
//auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer) //auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer)
auth := LoginAuth(SMTPAccount, SMTPToken)
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
if SMTPPort == 465 { if SMTPPort == 465 {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
InsecureSkipVerify: true, //InsecureSkipVerify: true,
ServerName: SMTPServer, ServerName: SMTPServer,
} }
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", SMTPServer, SMTPPort), tlsConfig) //conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", SMTPServer, SMTPPort), tlsConfig)
conn, err := net.Dial("tcp", "smtp.office365.com:587")
if err != nil { if err != nil {
return err return err
} }
@ -66,14 +68,14 @@ func SendEmail(subject string, receiver string, content string) error {
if err != nil { if err != nil {
return err return err
} }
if err = client.StartTLS(tlsconfig); err != nil { if err = client.StartTLS(tlsConfig); err != nil {
return err return err
} }
auth := LoginAuth(SMTPAccount, SMTPToken)
if err = client.Auth(auth); err != nil {
if err = client.Auth(auth); err != nil {
return err return err
} }
defer client.Close() defer client.Close()
if err = client.Auth(auth); err != nil { if err = client.Auth(auth); err != nil {
return err return err