-
Notifications
You must be signed in to change notification settings - Fork 0
/
brevo.go
55 lines (46 loc) · 1.24 KB
/
brevo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package emailer
import (
"bytes"
"context"
"html/template"
brevo "github.com/sendinblue/APIv3-go-library/v2/lib"
"github.com/aliml92/anor/config"
)
type brevoEmailer struct {
client *brevo.APIClient
templateCache *template.Template
config *config.EmailConfig
}
func NewBrevoEmailer(cfg *config.EmailConfig, t *template.Template) Emailer {
// create brevo APIClient
brevoCfg := brevo.NewConfiguration()
brevoCfg.AddDefaultHeader("api-key", cfg.APIKey)
brevoAPIClient := brevo.NewAPIClient(brevoCfg)
return &brevoEmailer{
client: brevoAPIClient,
templateCache: t,
config: cfg,
}
}
func (e *brevoEmailer) SendVerificationMessageWithOTP(ctx context.Context, otp string, email string) error {
var tplBuffer bytes.Buffer
tmpl := e.templateCache.Lookup(e.config.SignupVerificationTemplateName)
err := tmpl.Execute(&tplBuffer, otp)
if err != nil {
return err
}
_, _, err = e.client.TransactionalEmailsApi.SendTransacEmail(ctx, brevo.SendSmtpEmail{
Sender: &brevo.SendSmtpEmailSender{
Name: "Anor",
Email: e.config.FromEmail,
},
To: []brevo.SendSmtpEmailTo{
{
Email: email,
},
},
Subject: "Anor account details confirmation",
HtmlContent: tplBuffer.String(),
})
return err
}