-
Notifications
You must be signed in to change notification settings - Fork 5
/
email.go
103 lines (87 loc) · 3.04 KB
/
email.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
package postmark
import (
"errors"
"time"
)
const (
emailAPIPath = "email"
emailBatchAPIPath = "email/batch"
emailWithTemplateAPIPath = "email/withTemplate"
)
// EmailService handles communication with the email related methods of the
// Postmark API (http://developer.postmarkapp.com/developer-api-email.html)
type EmailService service
// Email is the set of parameters that can be used when sending an email.
type Email struct {
From string `json:",omitempty"`
To string `json:",omitempty"`
Cc string `json:",omitempty"`
Bcc string `json:",omitempty"`
Subject string `json:",omitempty"`
Tag string `json:",omitempty"`
TemplateID int `json:",omitempty"`
TemplateModel map[string]interface{} `json:",omitempty"`
HTMLBody string `json:",omitempty"`
TextBody string `json:",omitempty"`
ReplyTo string `json:",omitempty"`
Headers []EmailHeader `json:",omitempty"`
Attachments []EmailAttachment `json:",omitempty"`
TrackOpens bool `json:",omitempty"`
MessageStream string `json:",omitempty"`
Metadata map[string]string `json:",omitempty"`
}
// EmailHeader represents the values for an email header.
type EmailHeader struct {
Name string
Value string
}
// EmailAttachment represents the values for an email attachment.
type EmailAttachment struct {
Name string
Content []byte
ContentType *string
}
// EmailResponse is the set of parameters that is used in response to a send
// request
type EmailResponse struct {
To string
SubmittedAt time.Time
MessageID string
}
// Send will build and execute request to send an email via the API.
func (s *EmailService) Send(emailRequest *Email) (*EmailResponse, *Response, error) {
if emailRequest == nil {
return nil, nil, errors.New("The email request cannot be nil")
}
// If we have a template ID, use the Postmark template API endpoint.
requestPath := emailAPIPath
if emailRequest.TemplateID != 0 {
requestPath = emailWithTemplateAPIPath
}
request, err := s.client.NewRequest("POST", requestPath, emailRequest)
if err != nil {
return nil, nil, err
}
email := &EmailResponse{}
response, err := s.client.Do(request, email)
if err != nil {
return nil, response, err
}
return email, response, nil
}
// SendBatch will build and execute request to send batch emails via the API.
func (s *EmailService) SendBatch(emailRequests []*Email) ([]*EmailResponse, *Response, error) {
if len(emailRequests) < 1 {
return nil, nil, errors.New("You must pass a minimum of one email to SendBatch()")
}
request, err := s.client.NewRequest("POST", emailBatchAPIPath, emailRequests)
if err != nil {
return nil, nil, err
}
email := []*EmailResponse{}
response, err := s.client.Do(request, email)
if err != nil {
return nil, response, err
}
return email, response, nil
}