1.获取Go邮件库(自带发送不了)
go get gopkg.in/gomail.v1
2.包含库
import ( "gopkg.in/gomail.v1" )
3.例子
//info.From:发件地址或账号 //to: 收件地址 //body:邮件内容 //port:邮件服务器端口 //info.Host:邮件服务器地址 //info.Password:密码 func GoMail(to, body string) error { info, err := MailInfoGet() if err != nil { beego.Error("Get Mail Config Err:", err) return err } if info.Host == "" || info.Port == "" || info.From == "" || info.Password == "" || info.Header == "" || info.Footer == "" { beego.Error("Pleaase Finish Mail Config!") return nil } port, _ := strconv.Atoi(info.Port) msg := gomail.NewMessage() msg.SetHeader("From", info.From) msg.SetHeader("To", to) // msg.SetAddressHeader("Cc", "dan@example.com", "Dan") msg.SetHeader("Subject", info.Header) msg.SetBody("text/html", body) // mailer := gomail.NewMailer(info.Host, info.From, info.Password, port) if err := mailer.Send(msg); err != nil { beego.Error("Has err:", err) return err } return nil }
有疑问加站长微信联系(非本文作者)