2021-04-05 17:25:13 +03:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
type MailWhaleMailService struct {
|
2021-04-10 11:48:06 +03:00
|
|
|
publicUrl string
|
2021-04-05 17:25:13 +03:00
|
|
|
config *conf.MailwhaleMailConfig
|
|
|
|
httpClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailWhaleSendRequest struct {
|
|
|
|
To []string `json:"to"`
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Html string `json:"html"`
|
|
|
|
TemplateId string `json:"template_id"`
|
|
|
|
TemplateVars map[string]string `json:"template_vars"`
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
func NewMailWhaleService(config *conf.MailwhaleMailConfig, publicUrl string) *MailWhaleMailService {
|
2021-04-10 01:07:13 +03:00
|
|
|
return &MailWhaleMailService{
|
2021-04-10 11:48:06 +03:00
|
|
|
publicUrl: publicUrl,
|
|
|
|
config: config,
|
2021-04-05 17:25:13 +03:00
|
|
|
httpClient: &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
func (s *MailWhaleMailService) SendPasswordReset(recipient *models.User, resetLink string) error {
|
|
|
|
template, err := getPasswordResetTemplate(PasswordResetTplData{ResetLink: resetLink})
|
2021-04-05 17:25:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-10 01:07:13 +03:00
|
|
|
return s.send(recipient.Email, subjectPasswordReset, template.String(), true)
|
2021-04-05 17:25:13 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
func (s *MailWhaleMailService) SendImportNotification(recipient *models.User, duration time.Duration, numHeartbeats int) error {
|
|
|
|
template, err := getImportNotificationTemplate(ImportNotificationTplData{
|
|
|
|
PublicUrl: s.publicUrl,
|
|
|
|
Duration: fmt.Sprintf("%.0f seconds", duration.Seconds()),
|
|
|
|
NumHeartbeats: numHeartbeats,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.send(recipient.Email, subjectImportNotification, template.String(), true)
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
func (s *MailWhaleMailService) send(to, subject, body string, isHtml bool) error {
|
2021-04-05 17:25:13 +03:00
|
|
|
if to == "" {
|
2021-04-10 11:48:06 +03:00
|
|
|
return errors.New("not sending mail as recipient mail address seems to be invalid")
|
2021-04-05 17:25:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sendRequest := &MailWhaleSendRequest{
|
|
|
|
To: []string{to},
|
|
|
|
Subject: subject,
|
|
|
|
}
|
|
|
|
if isHtml {
|
|
|
|
sendRequest.Html = body
|
|
|
|
} else {
|
|
|
|
sendRequest.Text = body
|
|
|
|
}
|
|
|
|
payload, _ := json.Marshal(sendRequest)
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/mail", s.config.Url), bytes.NewBuffer(payload))
|
2021-04-05 17:25:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
req.SetBasicAuth(s.config.ClientId, s.config.ClientSecret)
|
2021-04-05 17:25:13 +03:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
res, err := s.httpClient.Do(req)
|
2021-04-05 17:25:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.StatusCode >= 400 {
|
2021-04-10 01:07:13 +03:00
|
|
|
return errors.New(fmt.Sprintf("got status %d from mailwhale", res.StatusCode))
|
2021-04-05 17:25:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|