-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransactional.go
124 lines (99 loc) · 2.91 KB
/
transactional.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package plunk
import (
"errors"
"fmt"
"net/http"
"sync"
)
type TransactionalEmailPayload struct {
To string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
From string `json:"from,omitempty"`
Name string `json:"name,omitempty"`
}
type ContactInfo struct {
ID string `json:"id"`
Email string `json:"email"`
}
type EmailRecipient struct {
Contact ContactInfo `json:"contact"`
Email string `json:"email"`
}
type TransactionalEmailResponse struct {
Success bool `json:"success"`
Emails []EmailRecipient `json:"emails"`
Timestamp string `json:"timestamp"`
}
var (
ErrEmptyResponse = errors.New("empty response")
ErrMissingTo = errors.New("missing recipient")
ErrMissingSubject = errors.New("missing subject")
ErrMissingBody = errors.New("missing body or html")
)
// Used to send transactional emails to a single recipient or multiple recipients at once.
// Transactional emails are programmatically sent emails that are considered to be part of your application's workflow.
// This could be a password reset email, a billing email or other non-marketing emails.
//
// # Using Markdown
//
// It is possible to use Markdown when sending a transactional email. Plunk will automatically apply the same styling as the email templates you make in the editor.
// Any email with a body that starts with # will be treated as Markdown.
func (p *Plunk) SendTransactionalEmail(payload TransactionalEmailPayload) (*TransactionalEmailResponse, error) {
res, err := p.sendTransactionalEmails([]TransactionalEmailPayload{payload})
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, ErrEmptyResponse
}
return res[0], nil
}
func (p *Plunk) SendMultipleTransactionalEmails(payload []TransactionalEmailPayload) ([]*TransactionalEmailResponse, error) {
return p.sendTransactionalEmails(payload)
}
func (p *Plunk) sendTransactionalEmails(payload []TransactionalEmailPayload) ([]*TransactionalEmailResponse, error) {
sem := make(chan bool, 10)
var wg sync.WaitGroup
// validate payload
for _, pl := range payload {
if pl.To == "" {
return nil, ErrMissingTo
}
if pl.Subject == "" {
return nil, ErrMissingSubject
}
if pl.Body == "" {
return nil, ErrMissingBody
}
}
result := []*TransactionalEmailResponse{}
url := p.url(transactionalEmailEndpoint)
for _, pl := range payload {
wg.Add(1)
sem <- true
go func(pl TransactionalEmailPayload) {
defer wg.Done()
defer func() { <-sem }()
res := &TransactionalEmailResponse{}
resp, err := p.sendRequest(SendConfig{
Body: pl,
Url: url,
Method: http.MethodPost,
})
if err != nil {
return
}
defer resp.Body.Close()
err = decodeResponse(resp, &res)
if err != nil {
return
}
result = append(result, res)
}(pl)
}
wg.Wait()
close(sem)
p.logInfo(fmt.Sprintf("Sent %d transactional emails", len(result)))
return result, nil
}