依赖
go get -u github.com/jordan-wright/email
文档
文档
示例代码
邮箱的相关配置
# email configuration
email:port: 25 # 端口要配置25 否则可能出现EOF错误from: xxx1@qq.comhost: smtp.qq.comis-ssl: truesecret: xxxxxnickname: 大锦余
发送邮件代码
package utilimport ("ToDoList/global""crypto/tls""fmt""github.com/jordan-wright/email""net/smtp""strconv"
)func SendEmail(to []string, subject string, body string) error {from := global.GVA_CONFIG.Email.Fromnickname := global.GVA_CONFIG.Email.Nicknamesecret := global.GVA_CONFIG.Email.Secrethost := global.GVA_CONFIG.Email.Hostport, _ := strconv.Atoi(global.GVA_CONFIG.Email.Port)isSSL := global.GVA_CONFIG.Email.IsSSLauth := smtp.PlainAuth("", from, secret, host)e := email.NewEmail()if nickname != "" {e.From = fmt.Sprintf("%s <%s>", nickname, from)} else {e.From = from}e.To = toe.Subject = subjecte.HTML = []byte(body)var err errorhostAddr := fmt.Sprintf("%s:%d", host, port)if isSSL {err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})} else {err = e.Send(hostAddr, auth)}return err
}