github.com/niubaoshu/goutils@v0.0.0-20180828035119-e8e576f66c2b/sendmail.go (about)

     1  package goutils
     2  
     3  import (
     4  	"net/smtp"
     5  	"strings"
     6  )
     7  
     8  func sendToMail(user, password, host, to, subject, body, mailtype string) error {
     9  	hp := strings.Split(host, ":")
    10  	auth := smtp.PlainAuth("", user, password, hp[0])
    11  	var content_type string
    12  	if mailtype == "html" {
    13  		content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
    14  	} else {
    15  		content_type = "Content-Type: text/plain" + "; charset=UTF-8"
    16  	}
    17  
    18  	msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
    19  	send_to := strings.Split(to, ";")
    20  	err := smtp.SendMail(host, auth, user, send_to, msg)
    21  	return err
    22  }
    23  
    24  func SendEmail(to_email []string, subject, body string) error {
    25  	user := "1460586781@qq.com"
    26  	password := "xmlpdvspdkxeiefh"
    27  	host := "smtp.qq.com:587"
    28  
    29  	to_stirng := strings.Join(to_email, ";")
    30  	return sendToMail(user, password, host, to_stirng, subject, body, "text")
    31  }
    32  
    33  func Notify(subject, message string, emails []string) {
    34  	SendEmail(emails, subject, message)
    35  }