Files
mengpost/mengpost-backend/services/smtp.go

30 lines
791 B
Go

package services
import (
"gopkg.in/gomail.v2"
"mengpost-backend/models"
)
// SendMail 通过指定账户发送邮件. body 支持纯文本或 HTML.
func SendMail(acc *models.Account, to, subject, body string, html bool) error {
if acc.AuthType == models.AuthTypeOAuthMicrosoft {
return sendMailMicrosoftOAuth(acc, to, subject, body, html)
}
m := gomail.NewMessage()
m.SetHeader("From", acc.Email)
m.SetHeader("To", to)
m.SetHeader("Subject", subject)
if html {
m.SetBody("text/html", body)
} else {
m.SetBody("text/plain", body)
}
d := gomail.NewDialer(acc.SMTPHost, acc.SMTPPort, acc.Email, acc.Password)
d.SSL = acc.UseTLS && acc.SMTPPort == 465
if tc := SMTPTLSConfig(acc); tc != nil {
d.TLSConfig = tc
}
return d.DialAndSend(m)
}