mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-17 16:43:54 +00:00
refactor: use built in smtp library (close #34)
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
package common
|
||||
|
||||
import "gopkg.in/gomail.v2"
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SendEmail(subject string, receiver string, content string) error {
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", SMTPAccount)
|
||||
m.SetHeader("To", receiver)
|
||||
m.SetHeader("Subject", subject)
|
||||
m.SetBody("text/html", content)
|
||||
d := gomail.NewDialer(SMTPServer, SMTPPort, SMTPAccount, SMTPToken)
|
||||
err := d.DialAndSend(m)
|
||||
mail := []byte(fmt.Sprintf("To: %s\r\n"+
|
||||
"From: %s<%s>\r\n"+
|
||||
"Subject: %s\r\n"+
|
||||
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
|
||||
receiver, SystemName, SMTPAccount, subject, content))
|
||||
auth := smtp.PlainAuth("", SMTPAccount, SMTPToken, SMTPServer)
|
||||
addr := fmt.Sprintf("%s:%d", SMTPServer, SMTPPort)
|
||||
to := strings.Split(receiver, ";")
|
||||
err := smtp.SendMail(addr, auth, SMTPAccount, to, mail)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user