You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

  1. package utils
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net/smtp"
  6. "strings"
  7. "gin-vue-admin/global"
  8. "github.com/jordan-wright/email"
  9. )
  10. func Email(subject string, body string) error {
  11. to := strings.Split(global.GVA_CONFIG.Email.EmailTo, ",")
  12. return send(to, subject, body)
  13. }
  14. func EmailTest(subject string, body string) error {
  15. to := []string{global.GVA_CONFIG.Email.EmailFrom}
  16. return send(to, subject, body)
  17. }
  18. func send(to []string, subject string, body string) error {
  19. from := global.GVA_CONFIG.Email.EmailFrom
  20. nickName := global.GVA_CONFIG.Email.EmailNickName
  21. secret := global.GVA_CONFIG.Email.EmailSecret
  22. host := global.GVA_CONFIG.Email.EmailHost
  23. port := global.GVA_CONFIG.Email.EmailPort
  24. isSSL := global.GVA_CONFIG.Email.EmailIsSSL
  25. auth := smtp.PlainAuth("", from, secret, host)
  26. e := email.NewEmail()
  27. if nickName != "" {
  28. e.From = fmt.Sprintf("%s <%s>", nickName, from)
  29. } else {
  30. e.From = from
  31. }
  32. e.To = to
  33. e.Subject = subject
  34. e.HTML = []byte(body)
  35. var err error
  36. hostAddr := fmt.Sprintf("%s:%d", host, port)
  37. if isSSL {
  38. err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
  39. } else {
  40. err = e.Send(hostAddr, auth)
  41. }
  42. return err
  43. }