-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_forwarder.go
80 lines (59 loc) · 2.05 KB
/
email_forwarder.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
package directadmin
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/spf13/cast"
)
func (c *UserContext) CreateEmailForwarder(domain string, user string, emails ...string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("domain", domain)
body.Set("email", strings.Join(emails, ","))
body.Set("user", user)
if _, err := c.makeRequestOld(http.MethodPost, "API_EMAIL_FORWARDERS?action=create", body, &response); err != nil {
return err
}
if response.Success != "Forwarder created" {
return fmt.Errorf("failed to create email account: %v", response.Result)
}
return nil
}
// GetEmailForwarders (user) returns an array of email forwarders belonging to the provided domain
func (c *UserContext) GetEmailForwarders(domain string) (map[string][]string, error) {
var emailForwarders = make(map[string][]string)
if _, err := c.makeRequestOld(http.MethodGet, "API_EMAIL_FORWARDERS?domain="+domain, nil, &emailForwarders); err != nil {
return nil, err
}
return emailForwarders, nil
}
func (c *UserContext) DeleteEmailForwarders(domain string, names ...string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("domain", domain)
for index, name := range names {
body.Set("select"+cast.ToString(index), name)
}
if _, err := c.makeRequestOld(http.MethodPost, "API_EMAIL_FORWARDERS?action=delete", body, &response); err != nil {
return err
}
if response.Success != "Forwarders deleted" {
return fmt.Errorf("failed to delete email forwarder: %v", response.Result)
}
return nil
}
func (c *UserContext) UpdateEmailForwarder(domain string, user string, emails ...string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("domain", domain)
body.Set("email", strings.Join(emails, ","))
body.Set("user", user)
if _, err := c.makeRequestOld(http.MethodPost, "API_EMAIL_FORWARDERS?action=modify", body, &response); err != nil {
return err
}
if response.Success != "Forwarder updated" {
return fmt.Errorf("failed to update email forwarder: %v", response.Result)
}
return nil
}