diff --git a/config/dev-example.toml b/config/dev-example.toml index 58c638c..a02d7c0 100644 --- a/config/dev-example.toml +++ b/config/dev-example.toml @@ -23,6 +23,10 @@ level = "debug" sender = "xxxxx" secret = "xxxxx" +[cos] +secret_id="xxx" +secret_key="xxx" + [oauth.server] # urlExample := "postgres://username:password@localhost:5432/database_name" db_uri = "xxxx" @@ -37,7 +41,3 @@ redirect_url = "xxx" id = "xxx" secret = "xxx" redirect_url = "xxx" - -[cos] -secret_id="xxx" -secret_key="xxx" \ No newline at end of file diff --git a/model/user_test.go b/model/user_test.go index 89dc52b..274eb8e 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -11,10 +11,11 @@ func TestGenerateCode(t *testing.T) { } func TestSendEmail(t *testing.T) { - testEmail := "xunop@qq.com" + testEmail := "user@example.org" code := GenerateVerifyCode() - title := "发送邮件测试" - SendEmail(testEmail, code, title) + title := "发送邮件测试,请勿回复" + content := InsertCode(code) + SendEmail(testEmail, content, title) } func TestVerifyCode(t *testing.T) { diff --git a/util/common.go b/util/common.go index b5796dd..2a86f4b 100644 --- a/util/common.go +++ b/util/common.go @@ -7,8 +7,6 @@ import ( "encoding/base64" "encoding/hex" "fmt" - "github.com/NJUPT-SAST/sast-link-backend/config" - "github.com/tencentyun/cos-go-sdk-v5" mr "math/rand" "net" "net/http" @@ -18,6 +16,9 @@ import ( "os" "time" + "github.com/NJUPT-SAST/sast-link-backend/config" + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/google/uuid" ) @@ -25,6 +26,10 @@ var ( T_cos *cos.Client = connectToTencentCOS() ) +const ( + larkSmtpServer = "smtp.feishu.cn:465" +) + func connectToTencentCOS() *cos.Client { // 将 examplebucket-1250000000 和 COS_REGION 修改为用户真实的信息 // 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。https://console.cloud.tencent.com/cos5/bucket @@ -103,7 +108,7 @@ func GenerateCode() string { } // sendEmail send email to user -func SendEmail(sender string, secret string, recipient string, content, title string) error { +func SendEmail(sender, secret, recipient, content, title string) error { // https://gist.github.com/chrisgillis/10888032 from := mail.Address{"", sender} to := mail.Address{"", recipient} @@ -121,10 +126,11 @@ func SendEmail(sender string, secret string, recipient string, content, title st for k, v := range headers { message += fmt.Sprintf("%s: %s\r\n", k, v) } - message += "\r\n" + body + header := "Content-Type: text/html; charset=\"UTF-8\";\n\n" + message += header + "\r\n" + body // Connect to the SMTP server - servername := "smtp.feishu.cn:465" + servername := larkSmtpServer host, _, _ := net.SplitHostPort(servername)