-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmandrill_inbound.go
169 lines (150 loc) · 6.37 KB
/
mandrill_inbound.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gochimp
import (
"errors"
)
// see https://mandrillapp.com/api/docs/inbound.JSON.html
const inbound_domains_endpoint string = "/inbound/domains.json" //List the domains that have been configured for inbound delivery
const add_domain_endpoint string = "/inbound/add-domain.json" //Add an inbound domain to your account
const check_domain_endpoint string = "/inbound/check-domain.json" //Check the MX settings for an inbound domain.
const delete_domain_endpoint string = "/inbound/delete-domain.json" //Delete an inbound domain from the account.
const routes_endopoint string = "/inbound/routes.json" //List the mailbox routes defined for an inbound domain
const add_route_endpoint string = "/inbound/add-route.json" //Add a new mailbox route to an inbound domain
const update_route_endpoint string = "/inbound/update-route.json" //Update the pattern or webhook of an existing inbound mailbox route.
const delete_route_endpoint string = "/inbound/delete-route.json" //Delete an existing inbound mailbox route
const send_raw_endpoint string = "/inbound/send-raw.json" //Send a raw MIME message to an inbound hook as if it had been sent over SMTP.
// can error with one of the following: Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) InboundDomainList() ([]InboundDomain, error) {
var response []InboundDomain
var params map[string]interface{} = make(map[string]interface{})
err := parseMandrillJson(a, inbound_domains_endpoint, params, &response)
return response, err
}
// can error with one of the following: Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) InboundDomainAdd(domain string) (InboundDomain, error) {
return getInboundDomain(a, domain, add_domain_endpoint)
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) InboundDomainCheck(domain string) (InboundDomain, error) {
return getInboundDomain(a, domain, check_domain_endpoint)
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) InboundDomainDelete(domain string) (InboundDomain, error) {
return getInboundDomain(a, domain, delete_domain_endpoint)
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) RouteList(domain string) ([]Route, error) {
var response []Route
if domain == "" {
return response, errors.New("domain cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["domain"] = domain
err := parseMandrillJson(a, routes_endopoint, params, &response)
return response, err
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) RouteAdd(domain string, pattern string, url string) (Route, error) {
var response Route
if domain == "" {
return response, errors.New("domain cannot be blank")
}
if pattern == "" {
return response, errors.New("pattern cannot be blank")
}
if url == "" {
return response, errors.New("url cannot be blank")
}
return getRoute(a, "", domain, pattern, url, add_route_endpoint)
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) RouteUpdate(id string, domain string, pattern string, url string) (Route, error) {
var response Route
if id == "" {
return response, errors.New("id cannot be blank")
}
return getRoute(a, id, domain, pattern, url, update_route_endpoint)
}
// can error with one of the following: Invalid_Key, Unknown_InboundDomain, ValidationError, GeneralError
func (a *MandrillAPI) RouteDelete(id string) (Route, error) {
var response Route
if id == "" {
return response, errors.New("id cannot be blank")
}
return getRoute(a, id, "", "", "", delete_route_endpoint)
}
// can error with one of the following: Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) SendRawMIME(raw_message string, to []string, mail_from string, helo string, client_address string) ([]InboundRecipient, error) {
var response []InboundRecipient
if raw_message == "" {
return response, errors.New("raw_message cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["raw_message"] = raw_message
if len(to) > 0 {
params["to"] = to
}
if mail_from != "" {
params["mail_from"] = mail_from
}
if helo != "" {
params["helo"] = helo
}
if client_address != "" {
params["client_address"] = client_address
}
err := parseMandrillJson(a, send_raw_endpoint, params, &response)
return response, err
}
func getRoute(a *MandrillAPI, id string, domain string, pattern string, url string, endpoint string) (Route, error) {
var params map[string]interface{} = make(map[string]interface{})
var response Route
if id != "" {
params["id"] = id
}
if domain != "" {
params["domain"] = domain
}
if pattern != "" {
params["pattern"] = pattern
}
if url != "" {
params["url"] = url
}
err := parseMandrillJson(a, endpoint, params, &response)
return response, err
}
func getInboundDomain(a *MandrillAPI, domain string, endpoint string) (InboundDomain, error) {
if domain == "" {
return InboundDomain{}, errors.New("domain cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["domain"] = domain
var response InboundDomain
err := parseMandrillJson(a, endpoint, params, &response)
return response, err
}
type InboundDomain struct {
Domain string `json:"domain"`
CreatedAt APITime `json:"created_at"`
ValidMx bool `json:"valid_mx"`
}
type Route struct {
Id string `json:"id"`
Pattern string `json:"pattern"`
Url string `json:"url"`
}
type InboundRecipient struct {
Email string `json:"email"`
Pattern string `json:"pattern"`
Url string `json:"url"`
}