-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmark.go
105 lines (85 loc) · 2.67 KB
/
postmark.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
// Package postmark provides a wrapper for the postmarkapi.com email service
package postmark
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)
const (
// DefaultHost is used to hold the standard config to send messages to
DefaultHost = "api.postmarkapp.com"
TestApiKey = "POSTMARK_API_TEST"
)
// Service contains all information and methods for the postmark api service
// Host will be set to the default host "api.postmarkapp.com" if not specified
type Service struct {
APIKey string
Host string
From string
}
// Response will contain the information returned by the postmark-api
type Response struct {
ErrorCode int
Message string
MessageID string
SubmittedAt string
To string
}
// New will return an email service instance that can be used to send emails
// via the service https://postmarkapp.com
func New(postmarkHost string, postmarkApikey string, defaultFrom string) (*Service, error) {
if postmarkHost == "" || postmarkApikey == "" || defaultFrom == "" {
return nil, errors.New("Postmark host, postmark api key and default sender address must be provided")
}
return &Service{
APIKey: postmarkApikey,
Host: postmarkHost,
From: defaultFrom,
}, nil
}
// Send swill send a single message to the server
func (service *Service) Send(msg *Message) (*Response, error) {
// set default sender if not specified
if msg.From == "" {
msg.From = service.From
}
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return nil, fmt.Errorf("could not encode email message: %w", err)
}
// send the data through postmark
return service.sendMessageThroughPostmark(&buf, "email")
}
// SendWithTemplate will send a message using a pre-specified template
func (service *Service) SendWithTemplate(msg *MessageWithTemplate) (*Response, error) {
// set default sender if not specified
if msg.From == "" {
msg.From = service.From
}
// marshal the message
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return nil, fmt.Errorf("could not encode email message with template: %w", err)
}
// send the data through postmark
return service.sendMessageThroughPostmark(&buf, "email/withTemplate")
}
// SendBatch will send multiple messages using the batch API
func (service *Service) SendBatch(msg []*Message) (*Response, error) {
// set default sender if not
for i := range msg {
if msg[i].From == "" {
msg[i].From = service.From
}
}
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(msg)
if err != nil {
return nil, fmt.Errorf("could not encode batch email messages: %w", err)
}
// send the data through postmark
return service.sendMessageThroughPostmark(&buf, "email/batch")
}