1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package warning
- import (
- "strconv"
- "strings"
- "gopkg.in/gomail.v2"
- )
- // SendToMail send mails to reciever
- // user is sender's mail address
- // password is sender's mail password
- // host is sender's mail servier address
- // to is reciver's mail address
- // subject mail title
- // body is mail content
- // mailtype is mail type
- func SendToMail(user, password, host, to, subject, body, mailtype string) error {
- hostIp := ""
- hostPort := ""
- array := strings.Split(host, ":")
- if len(array) > 1 {
- hostIp = array[0]
- hostPort = array[1]
- } else {
- hostIp = host
- hostPort = "465"
- }
- mailConn := map[string]string{
- "user": user,
- "pass": password,
- "host": hostIp,
- "port": hostPort,
- }
- port, _ := strconv.Atoi(mailConn["port"])
- mailTo := strings.Split(to, ";")
- m := gomail.NewMessage()
- m.SetHeader("From", "sy"+"<"+mailConn["user"]+">")
- m.SetHeader("To", mailTo...)
- m.SetHeader("Subject", subject)
- m.SetBody("text/html", body)
- d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
- err := d.DialAndSend(m)
- return err
- }
|