-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparkpost.go
49 lines (43 loc) · 1.3 KB
/
sparkpost.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
package sg
import "encoding/json"
// SparkPostService serializes a mail for SparkPost API.
type SparkPostService struct{}
// Authorize implements the Service interface.
func (*SparkPostService) Authorize(key string) string { return key }
// Serialize implements the Service interface.
func (*SparkPostService) Serialize(m *Mail) ([]byte, error) {
content := o{}
if m.TemplateID != "" {
content["template_id"] = m.TemplateID
}
if m.TemplateInline != "" {
content["html"] = m.TemplateInline
}
if m.Subject != "" {
content["subject"] = m.Subject
}
if m.From != "" {
content["from"] = o{"email": m.From, "name": m.FromName}
}
if m.Attachments != nil {
content["attachments"] = m.Attachments
}
return json.Marshal(&struct {
Recipients []H `json:"recipients"`
SubstitutionData H `json:"substitution_data,omitempty"`
Content o `json:"content"`
}{
Recipients: []H{{"address": m.To}},
Content: content,
SubstitutionData: m.Substitutions,
})
}
// NewSparkPostClient creates a new client with a SparkPost API key.
func NewSparkPostClient(apiKey string, tracers ...Tracer) Sender {
return &Client{
APIKey: apiKey,
APIURL: "https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3",
Service: new(SparkPostService),
Tracer: composedTracer{tracers},
}
}