mail.go 2.4 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package utils

import (
	"crypto/tls"
黄孟柱 已提交
5
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
6 7 8 9 10
	"net"
	"net/smtp"
	"strings"
)

Y
Your Name 已提交
11
var period = map[string]string{
E
eoLinker API Management 已提交
12 13 14 15 16 17 18
	"0": "1",
	"1": "5",
	"2": "15",
	"3": "30",
	"4": "60",
}

Y
Your Name 已提交
19
//SendToMail 发送邮件
E
eoLinker API Management 已提交
20 21 22
func SendToMail(user, password, host, to, subject, body, mailtype, smtpProtocol string) error {
	hp := strings.Split(host, ":")
	auth := smtp.PlainAuth("", user, password, hp[0])
Y
Your Name 已提交
23 24 25
	sendTo := strings.Split(to, ",")
	if len(sendTo) < 2 {
		if sendTo[0] == "" {
E
eoLinker API Management 已提交
26 27 28 29
			return nil
		}
	}
	log.Debug(user, password, auth)
Y
Your Name 已提交
30
	var contentType string
E
eoLinker API Management 已提交
31
	if mailtype == "html" {
Y
Your Name 已提交
32
		contentType = "Content-Type: text/" + mailtype + "; charset=UTF-8"
E
eoLinker API Management 已提交
33
	} else {
Y
Your Name 已提交
34
		contentType = "Content-Type: text/plain" + "; charset=UTF-8"
E
eoLinker API Management 已提交
35 36
	}
	var err error
Y
Your Name 已提交
37
	msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + subject + "\r\n" + contentType + "\r\n\r\n" + body)
Y
Your Name 已提交
38
	log.Debug("SendToMail", msg)
E
eoLinker API Management 已提交
39 40 41 42 43
	if smtpProtocol != "0" {
		err = SendMailUsingTLS(
			host,
			auth,
			user,
Y
Your Name 已提交
44
			sendTo,
E
eoLinker API Management 已提交
45 46 47 48 49 50 51 52
			msg,
		)
		if err != nil {

			err = SendMailUsingTLS(
				host,
				nil,
				user,
Y
Your Name 已提交
53
				sendTo,
E
eoLinker API Management 已提交
54 55 56 57 58
				msg,
			)

		}
	} else {
Y
Your Name 已提交
59
		err = smtp.SendMail(host, auth, user, sendTo, msg)
E
eoLinker API Management 已提交
60
		if err != nil {
Y
Your Name 已提交
61
			err = smtp.SendMail(host, nil, user, sendTo, msg)
E
eoLinker API Management 已提交
62 63 64 65 66
		}
	}
	return err
}

Y
Your Name 已提交
67
//Dial return a smtp client
E
eoLinker API Management 已提交
68 69 70 71 72 73 74 75 76 77
func Dial(addr string) (*smtp.Client, error) {
	conn, err := tls.Dial("tcp", addr, nil)
	if err != nil {
		return nil, err
	}
	//分解主机端口字符串
	host, _, _ := net.SplitHostPort(addr)
	return smtp.NewClient(conn, host)
}

Y
Your Name 已提交
78
//SendMailUsingTLS 参考net/smtp的func SendMail()
E
eoLinker API Management 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
//使用net.Dial连接tls(ssl)端口时,smtp.NewClient()会卡住且不提示err
//len(to)>1时,to[1]开始提示是密送
func SendMailUsingTLS(addr string, auth smtp.Auth, from string,
	to []string, msg []byte) (err error) {

	//create smtp client
	c, err := Dial(addr)
	if err != nil {
		log.Error("Create smpt client error:", err)
		return err
	}
	defer c.Close()

	if auth != nil {
		if ok, _ := c.Extension("AUTH"); ok {
			if err = c.Auth(auth); err != nil {
				log.Error("Error during AUTH", err)
				return err
			}
		}
	}

	if err = c.Mail(from); err != nil {
		return err
	}

	for _, addr := range to {
		if err = c.Rcpt(addr); err != nil {
			return err
		}
	}

	w, err := c.Data()
	if err != nil {
		return err
	}

	_, err = w.Write(msg)
	if err != nil {
		return err
	}

	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}