-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutbound.go
More file actions
89 lines (74 loc) · 2.78 KB
/
outbound.go
File metadata and controls
89 lines (74 loc) · 2.78 KB
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
package cloudmailin
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
)
// OutboundMail represents an email message ready to be sent.
// The ID will be populated by the API call once the message has been sent.
type OutboundMail struct {
From string `json:"from"`
To []string `json:"to,omitempty"`
CC []string `json:"cc,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
Subject string `json:"subject,omitempty"`
Plain string `json:"plain,omitempty"`
HTML string `json:"html,omitempty"`
Markdown string `json:"markdown,omitempty"`
Priority string `json:"priority,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []OutboundMailAttachment `json:"attachments,omitempty"`
TestMode bool `json:"test_mode,omitempty"`
ID string `json:"id,omitempty"`
}
// OutboundMailAttachment represents the format of attachments to be sent
// in an OutboundMail. Content must be a Base64 encoded string.
type OutboundMailAttachment struct {
// The Base64 encoded representation of the content.
Content string `json:"content"`
// An optional content id for the embedded attachment
ContentID string `json:"content_id,omitempty"`
// The mime content type of the file such as `image/jpeg`
ContentType string `json:"content_type"`
// The file name of the attachment
FileName string `json:"file_name"`
}
// SendMail will make a POST to send the OutboundMail email via the HTTP API.
func (client Client) SendMail(message *OutboundMail) (res *http.Response, err error) {
res, err = client.Do("POST", "/messages", message, RequestTypeSMTP)
if err != nil {
return
}
if res.StatusCode != 202 {
body, _ := ioutil.ReadAll(res.Body)
err = fmt.Errorf("could not send message (%d): %s", res.StatusCode, body)
} else {
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(message)
}
return
}
// AttachmentFromFile is a convenience function to prepare an OutboundMailAttachment
// from a local file (given as the filepath argument).
// The content will be Base64 encoded automatically and the filename included.
// This uses http.DetectContentType to guess the content type of the file.
func AttachmentFromFile(filepath string) (att OutboundMailAttachment,
err error) {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return
}
contentType := http.DetectContentType(bytes)
encoded := base64.StdEncoding.EncodeToString(bytes)
filename := path.Base(filepath)
att = OutboundMailAttachment{
Content: encoded,
ContentID: "",
ContentType: contentType,
FileName: filename,
}
return
}