引包
import "github.com/jordan-wright/email"
需要的配置
email:
addr: smtp.qq.com:25
host: smtp.qq.com
from: "xxxxxxxxxx <xxxxxxxxxx@qq.com>"
email: xxxxxxxxxx@qq.com
auth: xxxxxxxxxxxx
发送邮件
这里用发送验证码为例
func SendAuthCode(to string) {
subject := "【study_system】邮箱验证"
html := fmt.Sprintf(`<div style="text-align: center;">
<h2 style="color: #333;">欢迎使用,你的验证码为:</h2>
<h1 style="margin: 1.2em 0;">%s</h1>
<p style="font-size: 12px; color: #666;">请在5分钟内完成验证,过期失效,请勿告知他人,以防个人信息泄露</p>
</div>`, CreateAuthCode(to))
em := e.NewEmail()
em.From = config.EmailFrom
em.To = []string{to}
em.Subject = subject
em.HTML = []byte(html)
err := em.Send(config.EmailAddr, smtp.PlainAuth("", config.Email, config.EmailAuth, config.EmailHost))
}
func CreateAuthCode(em string) string {
code := fmt.Sprintf("%d", tools.Randnum(900000)+100000)
repository.SetAuthCode(em, code)
return code
}
本人项目参考:
[](Ambition6666/study_system: go,gin (github.com))