feat: ticket enhancemnet
This commit is contained in:
@@ -1,19 +1,83 @@
|
||||
package email
|
||||
|
||||
import "net/smtp"
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
type EmailConfig struct {
|
||||
Enabled bool `yaml:"enabled"` // 是否启用邮件发送
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
TLSSkipVerify bool `yaml:"tlsSkipVerify"`
|
||||
From string `yaml:"from"`
|
||||
To []string `yaml:"to"`
|
||||
Subject string `yaml:"subject"`
|
||||
Body string `yaml:"body"`
|
||||
}
|
||||
|
||||
// 简单邮件发送函数
|
||||
func SendEmail(to, subject, body string) error {
|
||||
from := "your@email.com"
|
||||
password := "your_password"
|
||||
smtpHost := "smtp.yourserver.com"
|
||||
smtpPort := "587"
|
||||
// 该函数使用标准库的 smtp 包发送邮件
|
||||
// 注意:此函数不支持 TLS 加密,建议使用 gomail 包发送邮件以支持 TLS 和其他高级功能
|
||||
// gomail 包的使用示例见 SendEmailWithGomail 函数
|
||||
func SendEmail(email EmailConfig) error {
|
||||
username := email.Username
|
||||
from := email.From
|
||||
password := email.Password
|
||||
to := strings.Join(email.To, ",") // 将多个收件人用逗号连接
|
||||
subject := email.Subject
|
||||
body := email.Body
|
||||
smtpHost := email.Host
|
||||
smtpPort := email.Port
|
||||
|
||||
msg := "From: " + from + "\n" +
|
||||
"To: " + to + "\n" +
|
||||
"Subject: " + subject + "\n\n" +
|
||||
body
|
||||
|
||||
auth := smtp.PlainAuth("", from, password, smtpHost)
|
||||
return smtp.SendMail(smtpHost+":"+smtpPort, auth, from, []string{to}, []byte(msg))
|
||||
auth := smtp.PlainAuth(from, username, password, smtpHost)
|
||||
return smtp.SendMail(smtpHost+":"+strconv.Itoa(smtpPort), auth, from, []string{to}, []byte(msg))
|
||||
}
|
||||
|
||||
func SendEmailWithGomail(email EmailConfig) error {
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", email.From)
|
||||
m.SetHeader("To", email.To...)
|
||||
m.SetHeader("Subject", email.Subject)
|
||||
m.SetBody("text/plain", email.Body)
|
||||
|
||||
d := gomail.NewDialer(email.Host, email.Port, email.Username, email.Password)
|
||||
|
||||
// 配置 TLS
|
||||
d.TLSConfig = &tls.Config{
|
||||
InsecureSkipVerify: email.TLSSkipVerify,
|
||||
}
|
||||
|
||||
// gomail 会自动处理 STARTTLS
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
fmt.Printf("Failed to DialAndSend:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveDuplicateEmails 去除重复的邮箱地址
|
||||
func RemoveDuplicateEmails(emails []string) []string {
|
||||
emailMap := make(map[string]struct{})
|
||||
var uniqueEmails []string
|
||||
|
||||
for _, email := range emails {
|
||||
if _, exists := emailMap[email]; !exists {
|
||||
emailMap[email] = struct{}{}
|
||||
uniqueEmails = append(uniqueEmails, email)
|
||||
}
|
||||
}
|
||||
|
||||
return uniqueEmails
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user