Fix: email send timeout

This commit is contained in:
cyp0633 2022-09-18 21:59:57 +08:00
parent a6d76f2cfc
commit d0d53e5cb2
Signed by: cyp0633
GPG Key ID: CF90D09FB1FDCE45
4 changed files with 23 additions and 20 deletions

2
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/glebarez/sqlite v1.4.6
github.com/go-ini/ini v1.67.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gorm.io/gorm v1.23.8
)
@ -39,6 +40,7 @@ require (
golang.org/x/sys v0.0.0-20220913120320-3275c407cedc // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.18.2 // indirect

4
go.sum
View File

@ -154,11 +154,15 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View File

@ -8,21 +8,21 @@ import (
// MailConf 是用于邮件发送的配置。
var MailConf = struct {
Identity string
Username string
Password string
Host string
Identity string // 发件人身份,默认留空即可
Username string // 发件人邮箱
Password string // 发件人邮箱密码
Host string // SMTP 服务器地址
}{}
// SMSConf 使用 Access Key 和 Access ID 配置阿里云短信 SDK。
var SMSConf = struct {
AccessKeyID string
AccessKeySecret string
AccessKeyID string // 阿里云短信服务的 Access Key ID
AccessKeySecret string // 阿里云短信服务的 Access Key Secret
}{}
// ServerConf 配置服务器的端口号等信息。
var ServerConf = struct {
Port string
Port string // 服务器端口号
}{}
// InitConf 使用文件夹内的 conf.ini 文件初始化配置。CLI 无需此操作。

View File

@ -2,23 +2,20 @@ package models
import (
"fmt"
"net/smtp"
"strings"
gomail "gopkg.in/gomail.v2"
)
// SendMail 使用 SMTP + TLS 协议向用户发送邮件。
func SendMail(dest string, code int) bool {
auth := smtp.PlainAuth(MailConf.Identity, MailConf.Username, MailConf.Password, MailConf.Host)
to := []string{dest}
nickname := "数学学习软件"
user := MailConf.Username
subject := "验证码"
contentType := "Content-Type: text/plain; charset=UTF-8"
body := fmt.Sprintf("您的数学学习软件注册验证码是%v请妥善保管", code)
msg := []byte("To: " + strings.Join(to, ",") + "\r\nFrom: " + nickname + "<" + user + ">\r\nSubject: " + subject + "\r\n" + contentType + "\r\n\r\n" + body)
err := smtp.SendMail(MailConf.Host+":465", auth, user, to, msg)
if err != nil {
fmt.Println("send mail error: ", err)
m := gomail.NewMessage()
m.SetHeader("From", MailConf.Username)
m.SetHeader("To", dest)
m.SetHeader("Subject", "验证码")
m.SetBody("text/html", fmt.Sprintf("您的数学学习软件注册验证码是<br><h1>%v</h1><br>请妥善保管", code))
d := gomail.NewDialer(MailConf.Host, 465, MailConf.Username, MailConf.Password)
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
return false
}
return true