125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package email
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"github.com/civet148/log"
|
|
"gopkg.in/gomail.v2"
|
|
)
|
|
|
|
const (
|
|
SubjectVerificationCodeForRegister = "Complete your registration with your Jelly AI verification Code"
|
|
SubjectVerificationCodeForResetPassword = "Complete your reset with your Jelly AI verification Code"
|
|
)
|
|
|
|
type EmailConfig struct {
|
|
SmtpServer string `json:"smtp_server" db:"smtp_server"`
|
|
SmtpPort uint32 `json:"smtp_port" db:"smtp_port"`
|
|
SmtpName string `json:"smtp_name" db:"smtp_name"`
|
|
AuthCode string `json:"auth_code" db:"auth_code"`
|
|
SendName string `json:"send_name" db:"send_name"`
|
|
}
|
|
|
|
func SendVerificationCode(cfg *EmailConfig, subject, to, body string) (err error) {
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
//发送人
|
|
m.SetAddressHeader("From", cfg.SmtpName, cfg.SendName)
|
|
//接收人
|
|
m.SetHeader("To", to)
|
|
//主题
|
|
m.SetHeader("Subject", subject)
|
|
//内容
|
|
m.SetBody("text/html", body)
|
|
//附件
|
|
//m.Attach("./myIpPic.png")
|
|
d := gomail.NewDialer(cfg.SmtpServer, int(cfg.SmtpPort), cfg.SmtpName, cfg.AuthCode)
|
|
if d.TLSConfig == nil {
|
|
d.TLSConfig = &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
}
|
|
} else {
|
|
d.TLSConfig.InsecureSkipVerify = true
|
|
}
|
|
|
|
// 发送邮件
|
|
if err = d.DialAndSend(m); err != nil {
|
|
log.Errorf("Send mail failed,err: %s", err.Error())
|
|
return err
|
|
}
|
|
log.Debugf("send to mail [%s] body [%s]", to, body)
|
|
return nil
|
|
}
|
|
|
|
func SendEmail(cfg *EmailConfig, subject string, msg string, to string) (err error) {
|
|
log.Debugf("[SMTP] server [%s] port [%v] name [%s] to [%s]", cfg.SmtpServer, cfg.SmtpPort, cfg.SmtpName, to)
|
|
if to == "" {
|
|
return log.Errorf("no recipient to send")
|
|
}
|
|
log.Debugf("send email subject [%s] to %+v ", subject, to)
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
//发送人
|
|
m.SetAddressHeader("From", cfg.SmtpName, cfg.SendName)
|
|
//接收人
|
|
m.SetHeader("To", to)
|
|
//抄送
|
|
//m.SetHeader("Cc", to...)
|
|
//密送
|
|
//m.SetHeader("Bcc", to...)
|
|
//主题
|
|
m.SetHeader("Subject", subject)
|
|
//内容
|
|
m.SetBody("text/html", msg)
|
|
//附件
|
|
//m.Attach("./myIpPic.png")
|
|
d := gomail.NewDialer(cfg.SmtpServer, int(cfg.SmtpPort), cfg.SmtpName, cfg.AuthCode)
|
|
if d.TLSConfig == nil {
|
|
d.TLSConfig = &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
}
|
|
} else {
|
|
d.TLSConfig.InsecureSkipVerify = true
|
|
}
|
|
// 发送邮件
|
|
if err = d.DialAndSend(m); err != nil {
|
|
log.Errorf("send email error [%s]", err.Error())
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func SendEmailBcc(cfg *EmailConfig, subject string, msg string, to ...string) (err error) {
|
|
log.Debugf("[SMTP] server [%s] port [%v] name [%s] to %v", cfg.SmtpServer, cfg.SmtpPort, cfg.SmtpName, to)
|
|
if len(to) == 0 {
|
|
return log.Errorf("no bcc recipients to send")
|
|
}
|
|
log.Debugf("send email subject [%s] to %+v ", subject, to)
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
//发送人
|
|
m.SetAddressHeader("From", cfg.SmtpName, cfg.SendName)
|
|
//接收人
|
|
m.SetHeader("To", cfg.SmtpName)
|
|
//抄送
|
|
//m.SetHeader("Cc", to...)
|
|
//密送
|
|
m.SetHeader("Bcc", to...)
|
|
//主题
|
|
m.SetHeader("Subject", subject)
|
|
//内容
|
|
m.SetBody("text/html", msg)
|
|
//附件
|
|
//m.Attach("./myIpPic.png")
|
|
d := gomail.NewDialer(cfg.SmtpServer, int(cfg.SmtpPort), cfg.SmtpName, cfg.AuthCode)
|
|
|
|
// 发送邮件
|
|
if err = d.DialAndSend(m); err != nil {
|
|
log.Errorf("send email error [%s]", err.Error())
|
|
return err
|
|
}
|
|
return nil
|
|
}
|