forked from koltyakov/gosip
/
utility.go
68 lines (60 loc) · 2 KB
/
utility.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pnocera/gosip"
)
// Utility represents SharePoint Utilities namespace API object struct
// Always use NewUtility constructor instead of &Utility{}
type Utility struct {
client *gosip.SPClient
config *RequestConfig
endpoint string
}
// EmailProps struct for SendEmail method parameters
type EmailProps struct {
Subject string // Email subject
Body string // Email text or HTML body
To []string // Slice of To email addresses to whom Email is intended to be sent
CC []string // Slice of CC email addresses (optional)
BCC []string // Slice of BCC email addresses (optional)
From string // Sender email addresses (optional)
}
// NewUtility - Utility struct constructor function
func NewUtility(client *gosip.SPClient, endpoint string, config *RequestConfig) *Utility {
return &Utility{
client: client,
endpoint: endpoint,
config: config,
}
}
// SendEmail sends an email via REST API due to the provided EmailProps options
func (utility *Utility) SendEmail(options *EmailProps) error {
endpoint := fmt.Sprintf(
"%s/_api/SP.Utilities.Utility.SendEmail",
getPriorEndpoint(utility.endpoint, "/_api"),
)
client := NewHTTPClient(utility.client)
properties := map[string]interface{}{}
properties["__metadata"] = map[string]string{"type": "SP.Utilities.EmailProperties"}
properties["Subject"] = options.Subject
properties["Body"] = options.Body
if options.From != "" {
properties["From"] = options.From
}
if len(options.To) > 0 {
properties["To"] = map[string][]string{"results": options.To}
}
if len(options.CC) > 0 {
properties["CC"] = map[string][]string{"results": options.CC}
}
if len(options.BCC) > 0 {
properties["BCC"] = map[string][]string{"results": options.BCC}
}
props, _ := json.Marshal(properties)
JSONProps := fmt.Sprintf("%s", props)
body := []byte(TrimMultiline(`{ "properties": ` + JSONProps + `}`))
_, err := client.Post(endpoint, bytes.NewBuffer(body), utility.config)
return err
}