51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/smtp"
|
|
|
|
"sale/internal/config"
|
|
)
|
|
|
|
func SendEmail(to, subject, body string) error {
|
|
cfg := config.AppConfig.SMTP
|
|
if cfg.Host == "" {
|
|
return fmt.Errorf("SMTP not configured")
|
|
}
|
|
|
|
auth := smtp.PlainAuth("", cfg.User, cfg.Password, cfg.Host)
|
|
|
|
msg := fmt.Sprintf(
|
|
"From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n%s",
|
|
cfg.From, to, subject, body,
|
|
)
|
|
|
|
return smtp.SendMail(
|
|
fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
|
|
auth,
|
|
cfg.From,
|
|
[]string{to},
|
|
[]byte(msg),
|
|
)
|
|
}
|
|
|
|
func SendVerifyEmail(to, code string) error {
|
|
subject := "邮箱验证码"
|
|
body := fmt.Sprintf(`
|
|
<h2>邮箱验证</h2>
|
|
<p>您的验证码是:<strong style="font-size:24px;color:#1890ff;">%s</strong></p>
|
|
<p>验证码有效期为30分钟,请尽快使用。</p>
|
|
`, code)
|
|
return SendEmail(to, subject, body)
|
|
}
|
|
|
|
func SendResetPasswordEmail(to, code string) error {
|
|
subject := "重置密码验证码"
|
|
body := fmt.Sprintf(`
|
|
<h2>重置密码</h2>
|
|
<p>您的验证码是:<strong style="font-size:24px;color:#1890ff;">%s</strong></p>
|
|
<p>验证码有效期为30分钟,请尽快使用。</p>
|
|
`, code)
|
|
return SendEmail(to, subject, body)
|
|
}
|