diff --git a/.gitignore b/.gitignore index 9a275649..4f989ea5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ tmp/ .env common/balancer/ config.yaml -dist \ No newline at end of file +dist +test.yaml \ No newline at end of file diff --git a/common/stmp/email.go b/common/stmp/email.go index 8d513452..fd1014ee 100644 --- a/common/stmp/email.go +++ b/common/stmp/email.go @@ -55,6 +55,7 @@ func (s *StmpConfig) Send(to, subject, body string) error { client.SetSSL(true) case 587: client.SetTLSPolicy(mail.TLSMandatory) + client.SetSMTPAuth(mail.SMTPAuthLogin) } if err := client.DialAndSend(message); err != nil { diff --git a/common/stmp/email_test.go b/common/stmp/email_test.go new file mode 100644 index 00000000..778c6ce9 --- /dev/null +++ b/common/stmp/email_test.go @@ -0,0 +1,66 @@ +package stmp_test + +import ( + "fmt" + "testing" + + "one-api/common" + "one-api/common/stmp" + + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func InitConfig() { + viper.AddConfigPath("/one-api") + viper.SetConfigName("test") + viper.ReadInConfig() +} + +type SMTPConfig struct { + Name string `mapstructure:"name"` + SMTPServer string `mapstructure:"SMTPServer"` + SMTPPort int `mapstructure:"SMTPPort"` + SMTPAccount string `mapstructure:"SMTPAccount"` + SMTPToken string `mapstructure:"SMTPToken"` + SMTPFrom string `mapstructure:"SMTPFrom"` +} + +func TestSend(t *testing.T) { + InitConfig() + + var configs []SMTPConfig + err := viper.UnmarshalKey("stmp.provider", &configs) + if err != nil { + t.Fatal(err) + } + + fmt.Println(configs) + email := viper.GetString("stmp.to") + fmt.Println(email) + + for _, tt := range configs { + t.Run(tt.Name, func(t *testing.T) { + stmpClient := stmp.NewStmp(tt.SMTPServer, tt.SMTPPort, tt.SMTPAccount, tt.SMTPToken, tt.SMTPFrom) + code := "123456" + contentTemp := ` +

+ 您正在进行邮箱验证。您的验证码为: +

+ +

+ %s +

+ +

+ 验证码 %d 分钟内有效,如果不是本人操作,请忽略。 +

` + + subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName) + content := fmt.Sprintf(contentTemp, code, common.VerificationValidMinutes) + + err := stmpClient.Render(email, subject, content) + assert.NoError(t, err) + }) + } +}