mail.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package warning
  2. import (
  3. "strconv"
  4. "strings"
  5. "gopkg.in/gomail.v2"
  6. )
  7. // SendToMail send mails to reciever
  8. // user is sender's mail address
  9. // password is sender's mail password
  10. // host is sender's mail servier address
  11. // to is reciver's mail address
  12. // subject mail title
  13. // body is mail content
  14. // mailtype is mail type
  15. func SendToMail(user, password, host, to, subject, body, mailtype string) error {
  16. hostIp := ""
  17. hostPort := ""
  18. array := strings.Split(host, ":")
  19. if len(array) > 1 {
  20. hostIp = array[0]
  21. hostPort = array[1]
  22. } else {
  23. hostIp = host
  24. hostPort = "465"
  25. }
  26. mailConn := map[string]string{
  27. "user": user,
  28. "pass": password,
  29. "host": hostIp,
  30. "port": hostPort,
  31. }
  32. port, _ := strconv.Atoi(mailConn["port"])
  33. mailTo := strings.Split(to, ";")
  34. m := gomail.NewMessage()
  35. m.SetHeader("From", "sy"+"<"+mailConn["user"]+">")
  36. m.SetHeader("To", mailTo...)
  37. m.SetHeader("Subject", subject)
  38. m.SetBody("text/html", body)
  39. d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
  40. err := d.DialAndSend(m)
  41. return err
  42. }