feat: support smtp without auth (#1101)
This commit is contained in:
parent
c4fe57c165
commit
ec6ad24810
@ -6,11 +6,16 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
|
"net"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func shouldAuth() bool {
|
||||||
|
return config.SMTPAccount != "" || config.SMTPToken != ""
|
||||||
|
}
|
||||||
|
|
||||||
func SendEmail(subject string, receiver string, content string) error {
|
func SendEmail(subject string, receiver string, content string) error {
|
||||||
if receiver == "" {
|
if receiver == "" {
|
||||||
return fmt.Errorf("receiver is empty")
|
return fmt.Errorf("receiver is empty")
|
||||||
@ -41,16 +46,24 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
"Date: %s\r\n"+
|
"Date: %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, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
|
receiver, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
|
||||||
|
|
||||||
auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
|
auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
|
||||||
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
|
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
|
||||||
to := strings.Split(receiver, ";")
|
to := strings.Split(receiver, ";")
|
||||||
|
|
||||||
|
if config.SMTPPort == 465 || !shouldAuth() {
|
||||||
|
// need advanced client
|
||||||
|
var conn net.Conn
|
||||||
|
var err error
|
||||||
if config.SMTPPort == 465 {
|
if config.SMTPPort == 465 {
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig := &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
ServerName: config.SMTPServer,
|
ServerName: config.SMTPServer,
|
||||||
}
|
}
|
||||||
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
|
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
|
||||||
|
} else {
|
||||||
|
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort))
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -59,9 +72,11 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
if shouldAuth() {
|
||||||
if err = client.Auth(auth); err != nil {
|
if err = client.Auth(auth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if err = client.Mail(config.SMTPFrom); err != nil {
|
if err = client.Mail(config.SMTPFrom); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user