This repository was archived by the owner on Apr 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage.go
131 lines (110 loc) · 2.38 KB
/
message.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
package postmark
import (
"os"
"fmt"
"io/ioutil"
"encoding/json"
"encoding/base64"
"path"
"mime"
)
type Header struct {
Name string
Value string
}
type Attachment struct {
Name string
Content string // Base 64 encoded string
ContentType string
}
type Response struct {
ErrorCode int
Message string
MessageID string
SubmittedAt string //Date
To string
}
type BatchResponse []Response
type Message struct {
From string
To string
Cc string
Bcc string
Subject string
Tag string
HtmlBody string
TextBody string
ReplyTo string
Headers []Header
Attachments []Attachment
}
type BatchMessage []Message
func (p *Message) String() string{
js, e := json.MarshalIndent(p, "", "")
if e != nil {
return ""
}
return string(js)
}
// Attach file to message (base64 encoded)
func (p *Message) Attach(file string)(error){
finfo, e := os.Stat(file)
if e != nil {
return e
}
if finfo.Size() > int64(10e6){
return fmt.Errorf("File size %d exceeds 10MB limit.", finfo.Size())
}
fh, e := os.Open(file)
if e != nil {
return e
}
// Even though we only have 10MB limit..
// I probably shouldn't do this..
cnt, e := ioutil.ReadAll(fh)
if e != nil {
return e
}
fh.Close()
mimeType := mime.TypeByExtension(path.Ext(file))
if len(mimeType) == 0 {
return fmt.Errorf("Unknown mime type for attachment: %s", file)
}
attachment := Attachment{
Name: finfo.Name(),
Content: base64.StdEncoding.EncodeToString(cnt),
ContentType: mimeType,
}
p.Attachments = append(p.Attachments, attachment)
return nil
}
func unmarshal (msg []byte, i interface{})(error){
e := json.Unmarshal(msg, i)
if e != nil {
return e
}
return nil
}
func (m *Message) Marshal()([]byte, error){
return json.Marshal(*m)
}
func UnmarshalMessage(msg []byte)(*Message, error){
var m Message
e := unmarshal(msg, &m)
return &m, e
}
func (r *Response) Marshal()([]byte, error){
return json.Marshal(*r)
}
func UnmarshalResponse(rsp []byte)(*Response, error){
var r Response
e := unmarshal(rsp, &r)
return &r, e
}
func (r *Response) String() string{
js, e := json.MarshalIndent(r, "", "")
if e != nil {
return ""
}
return string(js)
}