Files
terminal/server/service/mail.go
T

62 lines
1.7 KiB
Go

package service
import (
"context"
"fmt"
"net/smtp"
"next-terminal/server/common/nt"
"next-terminal/server/branding"
"next-terminal/server/log"
"next-terminal/server/repository"
"github.com/jordan-wright/email"
)
var MailService = new(mailService)
type mailService struct {
}
func (r mailService) SendMail(to, subject, text string) {
propertiesMap := repository.PropertyRepository.FindAllMap(context.TODO())
host := propertiesMap[nt.MailHost]
port := propertiesMap[nt.MailPort]
username := propertiesMap[nt.MailUsername]
password := propertiesMap[nt.MailPassword]
if host == "" || port == "" || username == "" || password == "" {
log.Warn("邮箱信息不完整,跳过发送邮件。")
return
}
e := email.NewEmail()
e.From = fmt.Sprintf("%s <%s>", branding.Name, username)
e.To = []string{to}
e.Subject = subject
e.Text = []byte(text)
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
if err != nil {
log.Error("邮件发送失败", log.String("err", err.Error()))
}
}
func (r mailService) SendTestMail(req map[string]interface{}) error {
host, _ := req["mail-host"].(string)
port, _ := req["mail-port"].(string)
username, _ := req["mail-username"].(string)
password, _ := req["mail-password"].(string)
to, _ := req["mail-to"].(string)
if host == "" || port == "" || username == "" || to == "" {
return fmt.Errorf("邮箱信息不完整")
}
e := email.NewEmail()
e.From = fmt.Sprintf("%s <%s>", branding.Name, username)
e.To = []string{to}
e.Subject = "Next Terminal Test Mail"
e.Text = []byte("This is a test email from Next Terminal.")
return e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
}