* 修复自建邮箱发送错误: INVALID HEADER Missing required header field: "Date" * chore: fix style --------- Co-authored-by: liyujie <29959257@qq.com> Co-authored-by: JustSong <39998050+songquanpeng@users.noreply.github.com> Co-authored-by: JustSong <songquanpeng@foxmail.com>
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/smtp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func SendEmail(subject string, receiver string, content string) error {
|
|
if SMTPFrom == "" { // for compatibility
|
|
SMTPFrom = SMTPAccount
|
|
}
|
|
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
|
|
|
|
// Extract domain from SMTPFrom
|
|
parts := strings.Split(SMTPFrom, "@")
|
|
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)
|
|
|
|
mail := []byte(fmt.Sprintf("To: %s\r\n"+
|
|
"From: %s<%s>\r\n"+
|
|
"Subject: %s\r\n"+
|
|
"Message-ID: %s\r\n"+ // add Message-ID header to avoid being treated as spam, RFC 5322
|
|
"Date: %s\r\n"+
|
|
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
|
|
receiver, SystemName, SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
|
|
auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer)
|
|
addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort)
|
|
to := strings.Split(receiver, ";")
|
|
|
|
if SMTPPort == 465 {
|
|
tlsConfig := &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
ServerName: SMTPServer,
|
|
}
|
|
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", SMTPServer, SMTPPort), tlsConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
client, err := smtp.NewClient(conn, SMTPServer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer client.Close()
|
|
if err = client.Auth(auth); err != nil {
|
|
return err
|
|
}
|
|
if err = client.Mail(SMTPFrom); err != nil {
|
|
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 {
|
|
err = smtp.SendMail(addr, auth, SMTPAccount, to, mail)
|
|
}
|
|
return err
|
|
}
|