🐛 fix: Outlook email authentication error (#212)

This commit is contained in:
MartialBE 2024-05-24 20:02:45 +08:00
parent dfc79097c8
commit 671aa51f42
No known key found for this signature in database
GPG Key ID: 27C0267EC84B0A5C
3 changed files with 69 additions and 1 deletions

3
.gitignore vendored
View File

@ -12,4 +12,5 @@ tmp/
.env
common/balancer/
config.yaml
dist
dist
test.yaml

View File

@ -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 {

66
common/stmp/email_test.go Normal file
View File

@ -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 := `
<p>
您正在进行邮箱验证您的验证码为:
</p>
<p style="text-align: center; font-size: 30px; color: #58a6ff;">
<strong>%s</strong>
</p>
<p style="color: #858585; padding-top: 15px;">
验证码 %d 分钟内有效如果不是本人操作请忽略
</p>`
subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
content := fmt.Sprintf(contentTemp, code, common.VerificationValidMinutes)
err := stmpClient.Render(email, subject, content)
assert.NoError(t, err)
})
}
}