golang(beego) 发送邮件

前端之家收集整理的这篇文章主要介绍了golang(beego) 发送邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import (
	"net/smtp"
	"strings"
)


//发送邮件帮助类
func SendMail(user,password,host,to,subject,body,mailtype string) error {
	hp := strings.Split(host,":")
	auth := smtp.PlainAuth("",user,hp[0])
	var content_type string
	if mailtype == "html" {
		content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
	} else {
		content_type = "Content-Type: text/plain" + "; charset=UTF-8"
	}
	msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
	send_to := strings.Split(to,";")
	err := smtp.SendMail(host,auth,send_to,msg)
	return err
}


用法:


err := SendMail("发送的邮箱","发送的邮箱密码","smtp.qq.com:25","目标邮箱","邮件标题","邮件内容","html")

err := SendMail("1442919817@qq.com","zfyxxxxxx252078",username+"@qq.com","找回密码","点击这里修改密码:http://www.xxx.com/updatepass?username="+username+"&time="+time.Now().Format("2006-01-0215:04:05"),"html")
原文链接:https://www.f2er.com/go/190711.html

猜你在找的Go相关文章