mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
feat: smtp mail provider implementation
This commit is contained in:
@@ -5,21 +5,13 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/markbates/pkger"
|
||||
conf "github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/models"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
tplPath = "/views/mail"
|
||||
tplNamePasswordReset = "reset_password"
|
||||
)
|
||||
|
||||
type MailWhaleService struct {
|
||||
type MailWhaleMailService struct {
|
||||
config *conf.MailwhaleMailConfig
|
||||
httpClient *http.Client
|
||||
}
|
||||
@@ -33,8 +25,8 @@ type MailWhaleSendRequest struct {
|
||||
TemplateVars map[string]string `json:"template_vars"`
|
||||
}
|
||||
|
||||
func NewMailWhaleService(config *conf.MailwhaleMailConfig) *MailWhaleService {
|
||||
return &MailWhaleService{
|
||||
func NewMailWhaleService(config *conf.MailwhaleMailConfig) *MailWhaleMailService {
|
||||
return &MailWhaleMailService{
|
||||
config: config,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
@@ -42,25 +34,15 @@ func NewMailWhaleService(config *conf.MailwhaleMailConfig) *MailWhaleService {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MailWhaleService) SendPasswordResetMail(recipient *models.User, resetLink string) error {
|
||||
tpl, err := m.loadTemplate(tplNamePasswordReset)
|
||||
func (s *MailWhaleMailService) SendPasswordResetMail(recipient *models.User, resetLink string) error {
|
||||
template, err := getPasswordResetTemplate(passwordResetLinkTplData{ResetLink: resetLink})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type data struct {
|
||||
ResetLink string
|
||||
}
|
||||
|
||||
var rendered bytes.Buffer
|
||||
if err := tpl.Execute(&rendered, data{ResetLink: resetLink}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.send(recipient.Email, "Wakapi – Password Reset", rendered.String(), true)
|
||||
return s.send(recipient.Email, subjectPasswordReset, template.String(), true)
|
||||
}
|
||||
|
||||
func (m *MailWhaleService) send(to, subject, body string, isHtml bool) error {
|
||||
func (s *MailWhaleMailService) send(to, subject, body string, isHtml bool) error {
|
||||
if to == "" {
|
||||
return errors.New("no recipient mail address set, cannot send password reset link")
|
||||
}
|
||||
@@ -76,36 +58,21 @@ func (m *MailWhaleService) send(to, subject, body string, isHtml bool) error {
|
||||
}
|
||||
payload, _ := json.Marshal(sendRequest)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/mail", m.config.Url), bytes.NewBuffer(payload))
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/mail", s.config.Url), bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(m.config.ClientId, m.config.ClientSecret)
|
||||
req.SetBasicAuth(s.config.ClientId, s.config.ClientSecret)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
res, err := m.httpClient.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode >= 400 {
|
||||
return errors.New(fmt.Sprintf("failed to send password reset mail to %v, got status %d from mailwhale", to, res.StatusCode))
|
||||
return errors.New(fmt.Sprintf("got status %d from mailwhale", res.StatusCode))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MailWhaleService) loadTemplate(tplName string) (*template.Template, error) {
|
||||
tplFile, err := pkger.Open(fmt.Sprintf("%s/%s.tpl.html", tplPath, tplName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tplFile.Close()
|
||||
|
||||
tplData, err := ioutil.ReadAll(tplFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return template.New(tplName).Parse(string(tplData))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user