-
Notifications
You must be signed in to change notification settings - Fork 0
/
emails.go
119 lines (101 loc) · 3.03 KB
/
emails.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
package postmark
import (
"path"
"time"
"golang.org/x/net/context"
)
// Emails defines the functionality of the emails resource
type Emails interface {
// Email sends a single email with custom content.
// http://developer.postmarkape.com/developer-api-email.html#send-email
Email(ctx context.Context, email *Email) (*EmailResponse, error)
// EmailWithTemplate sends an email with templated content.
// http://developer.postmarkape.com/developer-api-templates.html#email-with-template
EmailWithTemplate(ctx context.Context, email *EmailWithTemplate) (*EmailResponse, error)
}
type emails struct {
pm *postmark
}
var _ Emails = (*emails)(nil)
// LinkTrackType defines types of tracked links.
type LinkTrackType string
// LinkTrackType constant definitions.
const (
LinkTrackTypeNone LinkTrackType = "None"
LinkTrackTypeHTMLAndText LinkTrackType = "HtmlAndText"
LinkTrackTypeHTMLOnly LinkTrackType = "HtmlOnly"
LinkTrackTypeTextOnly LinkTrackType = "TextOnly"
)
// BaseEmail defines the fields common to all Postmark emails
type BaseEmail struct {
From string `json:",omitempty"`
To string `json:",omitempty"`
Cc string `json:",omitempty"`
Bcc string `json:",omitempty"`
Tag string `json:",omitempty"`
ReplyTo string `json:",omitempty"`
Headers []Header `json:",omitempty"`
TrackOpens *bool `json:",omitempty"`
TrackLinks LinkTrackType `json:",omitempty"`
Attachments []Attachment `json:",omitempty"`
}
// Header defines an email header within the Postmark API
type Header struct {
Name string
Value string
}
// Attachment defines an email attachment within the Postmark API
type Attachment struct {
Name string
Content string
ContentType string
}
// EmailResponse is the response from the postmark API after an email is sent.
// This can also be an error type for unsuccesful calls.
type EmailResponse struct {
To string
SubmittedAt time.Time
MessageID string
ErrorCode int
Message string
}
// Email defines an email object within the Postmark API
type Email struct {
BaseEmail
Subject string
HTMLBody string `json:"HtmlBody"`
TextBody string
}
func (e *emails) Email(ctx context.Context, email *Email) (*EmailResponse, error) {
er := new(EmailResponse)
_, err := e.pm.Exec(ctx, &Request{
Method: "POST",
Path: "email",
Payload: email,
Target: er,
})
if err != nil {
return nil, err
}
return er, nil
}
// EmailWithTemplate defines a templated email to the postmark API
type EmailWithTemplate struct {
BaseEmail
TemplateID string `json:"TemplateId"`
TemplateModel map[string]interface{}
InlineCSS bool `json:"InlineCss,omitempty"`
}
func (e *emails) EmailWithTemplate(ctx context.Context, email *EmailWithTemplate) (*EmailResponse, error) {
er := new(EmailResponse)
_, err := e.pm.Exec(ctx, &Request{
Method: "POST",
Path: path.Join("email", "withTemplate"),
Payload: email,
Target: er,
})
if err != nil {
return nil, err
}
return er, nil
}