2023-04-22 12:39:27 +00:00
|
|
|
package common
|
|
|
|
|
2023-05-13 07:30:09 +00:00
|
|
|
import (
|
2023-11-24 12:52:59 +00:00
|
|
|
"crypto/rand"
|
2023-05-13 10:57:27 +00:00
|
|
|
"crypto/tls"
|
2023-05-13 13:41:52 +00:00
|
|
|
"encoding/base64"
|
2023-05-13 07:30:09 +00:00
|
|
|
"fmt"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
2023-05-13 07:30:09 +00:00
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
2023-11-24 12:56:53 +00:00
|
|
|
"time"
|
2023-05-13 07:30:09 +00:00
|
|
|
)
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
func SendEmail(subject string, receiver string, content string) error {
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.SMTPFrom == "" { // for compatibility
|
|
|
|
config.SMTPFrom = config.SMTPAccount
|
2023-05-13 14:04:36 +00:00
|
|
|
}
|
2023-05-13 13:41:52 +00:00
|
|
|
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
2023-11-24 12:52:59 +00:00
|
|
|
|
|
|
|
// Extract domain from SMTPFrom
|
2024-01-21 15:21:42 +00:00
|
|
|
parts := strings.Split(config.SMTPFrom, "@")
|
2023-11-24 12:52:59 +00:00
|
|
|
var domain string
|
|
|
|
if len(parts) > 1 {
|
|
|
|
domain = parts[1]
|
|
|
|
}
|
|
|
|
// Generate a unique Message-ID
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
_, err := rand.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
messageId := fmt.Sprintf("<%x@%s>", buf, domain)
|
|
|
|
|
2023-05-13 07:30:09 +00:00
|
|
|
mail := []byte(fmt.Sprintf("To: %s\r\n"+
|
|
|
|
"From: %s<%s>\r\n"+
|
|
|
|
"Subject: %s\r\n"+
|
2023-11-24 12:52:59 +00:00
|
|
|
"Message-ID: %s\r\n"+ // add Message-ID header to avoid being treated as spam, RFC 5322
|
2023-11-24 12:56:53 +00:00
|
|
|
"Date: %s\r\n"+
|
2023-05-13 07:30:09 +00:00
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
|
2024-01-21 15:21:42 +00:00
|
|
|
receiver, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
|
|
|
|
auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
|
2023-05-13 07:30:09 +00:00
|
|
|
to := strings.Split(receiver, ";")
|
2023-11-24 12:52:59 +00:00
|
|
|
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.SMTPPort == 465 {
|
2023-05-13 10:57:27 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
2024-01-21 15:21:42 +00:00
|
|
|
ServerName: config.SMTPServer,
|
2023-05-13 10:57:27 +00:00
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
|
2023-05-13 10:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
client, err := smtp.NewClient(conn, config.SMTPServer)
|
2023-05-13 10:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
if err = client.Auth(auth); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
if err = client.Mail(config.SMTPFrom); err != nil {
|
2023-05-13 10:57:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
receiverEmails := strings.Split(receiver, ";")
|
|
|
|
for _, receiver := range receiverEmails {
|
|
|
|
if err = client.Rcpt(receiver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w, err := client.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(mail)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-21 15:21:42 +00:00
|
|
|
err = smtp.SendMail(addr, auth, config.SMTPAccount, to, mail)
|
2023-05-13 10:57:27 +00:00
|
|
|
}
|
2023-04-22 12:39:27 +00:00
|
|
|
return err
|
|
|
|
}
|