1 Star 4 Fork 0

jingshanccc/course

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
email.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
micahchen 提交于 2021-04-16 15:52 +08:00 . [feature] #89
package public
import (
"bytes"
"context"
"gitee.com/jingshanccc/course/public/middleware/redis"
"gitee.com/jingshanccc/course/public/util"
"github.com/jordan-wright/email"
"html/template"
"log"
"net/smtp"
"strings"
"time"
)
type EmailConfig struct {
Id int
Host string
Port string
User string
Pass string
}
func SendEmail(receiver string) {
var emailConfig EmailConfig
DB.Raw("select * from email where id = 1 limit 1").Find(&emailConfig)
auth := smtp.PlainAuth("", emailConfig.User, emailConfig.Pass, emailConfig.Host)
msg := []byte("Subject: 这里是标题内容\r\n\r\n" + "这里是正文内容\r\n")
err := smtp.SendMail(emailConfig.Host+emailConfig.Port, auth, emailConfig.User, []string{receiver}, msg)
if err != nil {
log.Fatal("failed to send EmailConfig:", err)
}
}
//SendHTMLEmail: 发送HTML格式邮件
func SendHTMLEmail(receiver string, path string, variables interface{}) error {
var emailConfig EmailConfig
DB.Raw("select * from email where id = 1 limit 1").Find(&emailConfig)
e := email.NewEmail()
e.Subject = "Micah在线视频后台管理"
e.From = emailConfig.User
e.To = strings.Split(receiver, ",")
t := template.Must(template.ParseFiles(path))
body := new(bytes.Buffer)
//作为变量传递给html模板
t.Execute(body, variables)
e.HTML = body.Bytes()
auth := smtp.PlainAuth("", emailConfig.User, emailConfig.Pass, emailConfig.Host)
err := e.Send(emailConfig.Host+emailConfig.Port, auth)
if err != nil {
log.Println("failed to send email:", err)
}
return err
}
//SendEmailCode: 发送邮箱验证码
func SendEmailCode(duration time.Duration, email, redisKey, templatePath string) *BusinessException {
var code string
if c, err := redis.RedisClient.Get(context.Background(), redisKey).Result(); err == nil {
code = c
} else {
code = util.GetVerifyCode()
redis.RedisClient.Set(context.Background(), redisKey, code, duration)
}
log.Printf("code is : %v", code)
if err := SendHTMLEmail(email, templatePath, code); err != nil {
return NewBusinessException(SEND_EMAIL_CODE_ERROR)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jingshanccc/course.git
git@gitee.com:jingshanccc/course.git
jingshanccc
course
course
23f538baa694

搜索帮助