mail.go 738 B

123456789101112131415161718192021222324252627282930313233343536
  1. package utils
  2. import (
  3. "net/smtp"
  4. )
  5. // Mail define mail struct
  6. type Mail struct {
  7. ToName string
  8. ToAddr string
  9. FromName string
  10. FromAddr string
  11. Subject string
  12. Body string
  13. }
  14. //Send send mail to recipients
  15. func (m *Mail) Send() error {
  16. config := GetConfig()
  17. mime := "\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
  18. msg := []byte("From: " + m.FromName + " <" + m.FromAddr + ">" + "\r\n" +
  19. "To: " + m.ToAddr + "\r\n" +
  20. "Subject: " + m.Subject + mime + "\r\n\r\n" +
  21. m.Body + "\r\n")
  22. auth := smtp.PlainAuth("", config.SMTP.User, config.SMTP.Password, config.SMTP.Host)
  23. err := smtp.SendMail(config.SMTP.Host+":"+config.SMTP.Port, auth, m.FromAddr, []string{m.ToAddr}, msg)
  24. return err
  25. }