2023-12-29 11:30:20 +00:00
|
|
|
package client
|
|
|
|
|
2023-12-31 12:22:03 +00:00
|
|
|
import (
|
2024-01-03 13:50:11 +00:00
|
|
|
"bytes"
|
2023-12-31 12:22:03 +00:00
|
|
|
"fmt"
|
2024-01-03 13:50:11 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"mime/multipart"
|
2023-12-31 12:22:03 +00:00
|
|
|
)
|
2023-12-29 11:30:20 +00:00
|
|
|
|
2024-01-03 13:50:11 +00:00
|
|
|
type RecoveryEmailSenderDeps struct {
|
2024-01-18 22:01:15 +00:00
|
|
|
SmtpApiUrl string
|
|
|
|
SmtpHost string
|
|
|
|
SmtpPort string
|
|
|
|
SmtpSender string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
ApiKey string
|
|
|
|
FiberClient *fiber.Client
|
|
|
|
Logger *zap.Logger
|
|
|
|
RecoveryUrl string
|
2024-01-03 13:50:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 12:22:03 +00:00
|
|
|
type RecoveryEmailSender struct {
|
2024-01-03 13:50:11 +00:00
|
|
|
deps RecoveryEmailSenderDeps
|
2023-12-31 12:22:03 +00:00
|
|
|
}
|
2023-12-29 11:30:20 +00:00
|
|
|
|
2024-01-03 13:50:11 +00:00
|
|
|
func NewRecoveryEmailSender(deps RecoveryEmailSenderDeps) *RecoveryEmailSender {
|
|
|
|
if deps.FiberClient == nil {
|
|
|
|
deps.FiberClient = fiber.AcquireClient()
|
|
|
|
}
|
|
|
|
return &RecoveryEmailSender{
|
|
|
|
deps: deps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:13:07 +00:00
|
|
|
func (r *RecoveryEmailSender) SendRecoveryEmail(email string, signature string) error {
|
2024-01-03 13:50:11 +00:00
|
|
|
url := r.deps.SmtpApiUrl
|
|
|
|
|
2024-01-07 11:13:07 +00:00
|
|
|
fmt.Println(email, signature)
|
2024-01-03 13:50:11 +00:00
|
|
|
|
2024-01-18 22:01:15 +00:00
|
|
|
message := fmt.Sprintf(`Здравствуйте, ваша <a href="%s">ссылка для восстановление пароля</a>(доступна всего 15 минут)
|
|
|
|
|
|
|
|
Если это были не вы, напишите пожалуйста в техническую поддержку.`, r.deps.RecoveryUrl + signature)
|
2023-12-31 12:22:03 +00:00
|
|
|
|
2024-01-03 13:50:11 +00:00
|
|
|
form := new(bytes.Buffer)
|
|
|
|
writer := multipart.NewWriter(form)
|
|
|
|
defer writer.Close()
|
|
|
|
|
|
|
|
fields := map[string]string{
|
|
|
|
"from": r.deps.SmtpSender,
|
2024-01-18 13:44:55 +00:00
|
|
|
"to": email,
|
2024-01-03 13:50:11 +00:00
|
|
|
"subject": "Восстановление доступа",
|
|
|
|
"html": message,
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range fields {
|
|
|
|
if err := writer.WriteField(key, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-12-31 12:22:03 +00:00
|
|
|
|
2024-01-03 13:50:11 +00:00
|
|
|
if err := writer.Close(); err != nil {
|
2023-12-31 12:22:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-12-29 11:30:20 +00:00
|
|
|
|
2024-01-03 13:50:11 +00:00
|
|
|
req := r.deps.FiberClient.Post(url).Body(form.Bytes()).ContentType(writer.FormDataContentType())
|
|
|
|
if r.deps.ApiKey != "" {
|
|
|
|
req.Set("Authorization", r.deps.ApiKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
statusCode, body, errs := req.Bytes()
|
|
|
|
if errs != nil {
|
2024-01-04 11:27:50 +00:00
|
|
|
r.deps.Logger.Error("Error sending request", zap.Error(errs[0]))
|
2024-01-03 13:50:11 +00:00
|
|
|
return errs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode != fiber.StatusOK {
|
2024-01-04 11:27:50 +00:00
|
|
|
err := fmt.Errorf("the SMTP service returned an error: %s Response body: %s", statusCode, body)
|
2024-01-04 11:32:27 +00:00
|
|
|
r.deps.Logger.Error("Error sending email", zap.Error(err))
|
2024-01-03 13:50:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-12-29 11:30:20 +00:00
|
|
|
|
2024-01-04 11:27:50 +00:00
|
|
|
//r.deps.Logger.Info("Recovery email sent", zap.String("email", email))
|
2023-12-29 11:30:20 +00:00
|
|
|
return nil
|
|
|
|
}
|