/
sender.go
56 lines (45 loc) · 1.03 KB
/
sender.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
package sender
import (
"errors"
"github.com/tidwall/gjson"
"github.com/zcong1993/mailer/common"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const API_ADDRESS = "http://api.sendcloud.net/apiv2/mail/send"
type SendCloud struct {
ApiUser string
ApiKey string
Client http.Client
ApiAddress string
}
func (sc *SendCloud) Send(msg common.MailMsg) (error, bool) {
form := sc.toForm(msg)
resp, err := sc.Client.PostForm(sc.ApiAddress, form)
if err != nil {
return err, true
}
defer resp.Body.Close()
bt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err, true
}
code := gjson.GetBytes(bt, "statusCode").Int()
if code == 200 {
return nil, false
}
message := gjson.GetBytes(bt, "message").String()
return errors.New(message), true
}
func (sc *SendCloud) toForm(msg common.MailMsg) url.Values {
u := url.Values{}
u.Add("apiUser", sc.ApiUser)
u.Add("apiKey", sc.ApiKey)
u.Add("from", msg.From)
u.Add("to", strings.Join(msg.To, ";"))
u.Add("html", msg.Body)
u.Add("subject", msg.Subject)
return u
}