-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.go
76 lines (66 loc) · 2.32 KB
/
send.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
package smtpd
import (
"encoding/json"
"fmt"
"net/http"
)
// SendBasicMessage is used to send an email
type SendBasicMessage struct {
RecipientEmailAddress string `json:"recipient_email_address"`
RecipientName string `json:"recipient_name"`
FromEmailAddress string `json:"from_email_address"`
FromName string `json:"from_name"`
ReplyTo string `json:"reply_to"`
Subject string `json:"subject"`
ContentHTML string `json:"content_html"`
ContentText string `json:"content_text"`
OpenTracking bool `json:"open_tracking"`
ClickTracking bool `json:"click_tracking"`
Tags []string `json:"tags"`
}
// SendBasicDetailedResponse after a message has been accepted
type SendBasicDetailedResponse struct {
SendID string `json:"send_id"`
SendKey string `json:"send_key"`
SendingProfileID string `json:"sending_profile_id"`
FromEmailAddress string `json:"from_email_address"`
FromName string `json:"from_name"`
RecipientEmailAddress string `json:"recipient_email_address"`
RecipientName string `json:"recipient_name"`
Subject string `json:"subject"`
OpenTracking bool `json:"open_tracking"`
ClickTracking bool `json:"click_tracking"`
Tags []string `json:"tags"`
State string `json:"state"`
StateCategory string `json:"state_category"`
Error string `json:"error,omitempty"`
CreatedAtUtc int64 `json:"created_at_utc"`
ModifiedAtUtc int64 `json:"modified_at_utc"`
}
// SendBasic sends a transactional email
func (c *Client) SendBasic(message SendBasicMessage) (SendBasicDetailedResponse, error) {
var result SendBasicDetailedResponse
if !c.preFlight() {
return result, ErrInvalidCredentials
}
b, err := json.Marshal(&message)
if err != nil {
return result, err
}
body, statusCode, err := c.http.Post(
fmt.Sprintf("%s/api/%s/email/send", baseURL, baseVersion),
b,
c.baseHeaders(),
)
if err != nil {
return result, err
}
if statusCode != http.StatusAccepted {
return result, c.parseAPIError(body)
}
err = json.Unmarshal(body, &result)
if err != nil {
return result, err
}
return result, nil
}