mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-06 00:33:43 +08:00
feat: add ForceEmailTLSVerify configuration and improve email sending error handling
closes #50
This commit is contained in:
@@ -109,6 +109,9 @@ var RequestInterval = time.Duration(requestInterval) * time.Second
|
|||||||
|
|
||||||
var SyncFrequency = env.Int("SYNC_FREQUENCY", 10*60) // unit is second
|
var SyncFrequency = env.Int("SYNC_FREQUENCY", 10*60) // unit is second
|
||||||
|
|
||||||
|
// ForceEmailTLSVerify is used to determine whether to force TLS verification for email
|
||||||
|
var ForceEmailTLSVerify = env.Bool("FORCE_EMAIL_TLS_VERIFY", false)
|
||||||
|
|
||||||
var BatchUpdateEnabled = false
|
var BatchUpdateEnabled = false
|
||||||
var BatchUpdateInterval = env.Int("BATCH_UPDATE_INTERVAL", 5)
|
var BatchUpdateInterval = env.Int("BATCH_UPDATE_INTERVAL", 5)
|
||||||
|
|
||||||
|
|||||||
3
common/env/helper.go
vendored
3
common/env/helper.go
vendored
@@ -3,13 +3,14 @@ package env
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Bool(env string, defaultValue bool) bool {
|
func Bool(env string, defaultValue bool) bool {
|
||||||
if env == "" || os.Getenv(env) == "" {
|
if env == "" || os.Getenv(env) == "" {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
return os.Getenv(env) == "true"
|
return strings.ToLower(os.Getenv(env)) == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int(env string, defaultValue int) int {
|
func Int(env string, defaultValue int) int {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
)
|
)
|
||||||
@@ -27,17 +28,17 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
}
|
}
|
||||||
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
||||||
|
|
||||||
// Extract domain from SMTPFrom
|
// Extract domain from SMTPFrom with fallback
|
||||||
|
domain := "localhost"
|
||||||
parts := strings.Split(config.SMTPFrom, "@")
|
parts := strings.Split(config.SMTPFrom, "@")
|
||||||
var domain string
|
if len(parts) > 1 && parts[1] != "" {
|
||||||
if len(parts) > 1 {
|
|
||||||
domain = parts[1]
|
domain = parts[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a unique Message-ID
|
// Generate a unique Message-ID
|
||||||
buf := make([]byte, 16)
|
buf := make([]byte, 16)
|
||||||
_, err := rand.Read(buf)
|
if _, err := rand.Read(buf); err != nil {
|
||||||
if err != nil {
|
return errors.Wrap(err, "failed to generate random bytes for Message-ID")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
messageId := fmt.Sprintf("<%x@%s>", buf, domain)
|
messageId := fmt.Sprintf("<%x@%s>", buf, domain)
|
||||||
|
|
||||||
@@ -50,59 +51,85 @@ func SendEmail(subject string, receiver string, content string) error {
|
|||||||
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 := net.JoinHostPort(config.SMTPServer, fmt.Sprintf("%d", config.SMTPPort))
|
||||||
to := strings.Split(receiver, ";")
|
|
||||||
|
// Clean up recipient addresses
|
||||||
|
receiverEmails := []string{}
|
||||||
|
for _, email := range strings.Split(receiver, ";") {
|
||||||
|
email = strings.TrimSpace(email)
|
||||||
|
if email != "" {
|
||||||
|
receiverEmails = append(receiverEmails, email)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(receiverEmails) == 0 {
|
||||||
|
return errors.New("no valid recipient email addresses")
|
||||||
|
}
|
||||||
|
|
||||||
if config.SMTPPort == 465 || !shouldAuth() {
|
if config.SMTPPort == 465 || !shouldAuth() {
|
||||||
// need advanced client
|
// need advanced client
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// Add connection timeout
|
||||||
|
dialer := &net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
if config.SMTPPort == 465 {
|
if config.SMTPPort == 465 {
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig := &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: !config.ForceEmailTLSVerify,
|
||||||
ServerName: config.SMTPServer,
|
ServerName: config.SMTPServer,
|
||||||
}
|
}
|
||||||
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
|
conn, err = tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
|
||||||
} else {
|
} else {
|
||||||
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort))
|
conn, err = dialer.Dial("tcp", addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to connect to SMTP server")
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := smtp.NewClient(conn, config.SMTPServer)
|
client, err := smtp.NewClient(conn, config.SMTPServer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to create SMTP client")
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
if shouldAuth() {
|
if shouldAuth() {
|
||||||
if err = client.Auth(auth); err != nil {
|
if err = client.Auth(auth); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "SMTP authentication failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = client.Mail(config.SMTPFrom); err != nil {
|
if err = client.Mail(config.SMTPFrom); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to set MAIL FROM")
|
||||||
}
|
}
|
||||||
receiverEmails := strings.Split(receiver, ";")
|
|
||||||
for _, receiver := range receiverEmails {
|
for _, receiver := range receiverEmails {
|
||||||
if err = client.Rcpt(receiver); err != nil {
|
if err = client.Rcpt(receiver); err != nil {
|
||||||
return err
|
return errors.Wrapf(err, "failed to add recipient: %s", receiver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := client.Data()
|
w, err := client.Data()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to create message data writer")
|
||||||
}
|
}
|
||||||
_, err = w.Write(mail)
|
|
||||||
if err != nil {
|
if _, err = w.Write(mail); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to write email content")
|
||||||
}
|
}
|
||||||
err = w.Close()
|
|
||||||
if err != nil {
|
if err = w.Close(); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to close message data writer")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = smtp.SendMail(addr, auth, config.SMTPAccount, to, mail)
|
|
||||||
|
// Use the same sender address in the SMTP protocol as in the From header
|
||||||
|
err := smtp.SendMail(addr, auth, config.SMTPFrom, receiverEmails, mail)
|
||||||
if err != nil && strings.Contains(err.Error(), "short response") { // 部分提供商返回该错误,但实际上邮件已经发送成功
|
if err != nil && strings.Contains(err.Error(), "short response") { // 部分提供商返回该错误,但实际上邮件已经发送成功
|
||||||
logger.SysWarnf("short response from SMTP server, return nil instead of error: %s", err.Error())
|
logger.SysWarnf("short response from SMTP server, return nil instead of error: %s", err.Error())
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user