forked from keighl/postmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
email.go
124 lines (108 loc) · 3.91 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package postmark
import (
"fmt"
"time"
)
// TrackLinksOption are possible options for TrackLinks
type TrackLinksOption string
const (
// NoneTrackLinks means no tracking for any links in html or text bodies.
NoneTrackLinks = TrackLinksOption("None")
// HTMLAndTextTrackLinks means tracking for any links in html and text bodies.
HTMLAndTextTrackLinks = TrackLinksOption("HtmlAndText")
// HTMLOnlyTrackLinks means tracking for any links only in html bodies.
HTMLOnlyTrackLinks = TrackLinksOption("HtmlOnly")
// TextOnlyrackLinks means tracking for only links only in text bodies.
TextOnlyrackLinks = TrackLinksOption("TextOnly")
)
// Email is exactly what it sounds like
type Email struct {
// From: REQUIRED The sender email address. Must have a registered and confirmed Sender Signature.
From string `json:",omitempty"`
// To: REQUIRED Recipient email address. Multiple addresses are comma separated. Max 50.
To string `json:",omitempty"`
// Cc recipient email address. Multiple addresses are comma separated. Max 50.
Cc string `json:",omitempty"`
// Bcc recipient email address. Multiple addresses are comma separated. Max 50.
Bcc string `json:",omitempty"`
// Subject: Email subject
Subject string `json:",omitempty"`
// Tag: Email tag that allows you to categorize outgoing emails and get detailed statistics.
Tag string `json:",omitempty"`
// HtmlBody: HTML email message. REQUIRED, If no TextBody specified
HtmlBody string `json:",omitempty"`
// TextBody: Plain text email message. REQUIRED, If no HtmlBody specified
TextBody string `json:",omitempty"`
// ReplyTo: Reply To override email address. Defaults to the Reply To set in the sender signature.
ReplyTo string `json:",omitempty"`
// Headers: List of custom headers to include.
Headers []Header `json:",omitempty"`
// TrackLinks: Activate link tracking for links in the HTML or Text bodies of this email.
// Possible options: None HtmlAndText HtmlOnly TextOnly
TrackLinks TrackLinksOption `json:",omitempty"`
// TrackOpens: Activate open tracking for this email.
TrackOpens bool `json:",omitempty"`
// Attachments: List of attachments
Attachments []Attachment `json:",omitempty"`
// Metadata: metadata
Metadata map[string]string `json:",omitempty"`
}
// Header - an email header
type Header struct {
// Name: header name
Name string
// Value: header value
Value string
}
// Attachment is an optional encoded file to send along with an email
type Attachment struct {
// Name: attachment name
Name string
// Content: Base64 encoded attachment data
Content string
// ContentType: attachment MIME type
ContentType string
// ContentId: populate for inlining images with the images cid
ContentID string `json:",omitempty"`
}
// EmailResponse holds info in response to a send/send-batch request
// Even if API request comes back successful, check the ErrorCode to see if there might be a delivery problem
type EmailResponse struct {
// To: Recipient email address
To string
// SubmittedAt: Timestamp
SubmittedAt time.Time
// MessageID: ID of message
MessageID string
// ErrorCode: API Error Codes
ErrorCode int64
// Message: Response message
Message string
}
// SendEmail sends, well, an email.
func (client *Client) SendEmail(email Email) (EmailResponse, error) {
res := EmailResponse{}
err := client.doRequest(parameters{
Method: "POST",
Path: "email",
Payload: email,
TokenType: server_token,
}, &res)
if res.ErrorCode != 0 {
return res, fmt.Errorf(`%v %s`, res.ErrorCode, res.Message)
}
return res, err
}
// SendEmailBatch sends multiple emails together
// Note, individual emails in the batch can error, so it would be wise to
// range over the responses and sniff for errors
func (client *Client) SendEmailBatch(emails []Email) ([]EmailResponse, error) {
res := []EmailResponse{}
err := client.doRequest(parameters{
Method: "POST",
Path: "email/batch",
Payload: emails,
TokenType: server_token,
}, &res)
return res, err
}