-
Notifications
You must be signed in to change notification settings - Fork 0
/
maelstromMailGun.go
81 lines (65 loc) · 1.67 KB
/
maelstromMailGun.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
// MailGun Mail Server specific implementation
package main
import (
"bytes"
"net/http"
"net/url"
"strconv"
)
var mailGunKey string
type MailGunServer struct {
Server MailServer
}
func (s *MailGunServer) Send(message Message) int {
if Debug {
InfoLog.Printf("sending email from %s to %s with subject %s via MailGun.\n", message.From, message.To, message.Subject)
}
data := url.Values{}
data.Set("from", message.From)
for _, to := range message.To {
data.Add("to", to)
}
data.Set("subject", message.Subject)
data.Set("text", message.Text)
r, err := http.NewRequest("POST", s.Server.Url+"messages", bytes.NewBufferString(data.Encode()))
check(err)
r.SetBasicAuth("api", mailGunKey)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
if Debug {
InfoLog.Println("Sending Request " + r.URL.String())
}
res, err := http.DefaultClient.Do(r)
if err != nil {
ErrorLog.Println("Error sending mail via MailGun", err)
return 500
}
if Debug {
InfoLog.Println("Received: " + res.Status)
}
return res.StatusCode
}
func (s *MailGunServer) Ping() bool {
r, err := http.NewRequest("GET", s.Server.Url+"stats", nil)
check(err)
r.SetBasicAuth("api", mailGunKey)
if Debug {
InfoLog.Println("Sending Request " + r.URL.String())
}
res, err := http.DefaultClient.Do(r)
if err != nil {
ErrorLog.Println("Error reaching MailGun server ", err)
return false
}
if Debug {
ErrorLog.Println("Received: " + res.Status)
}
return res.StatusCode == 200
}
func (s *MailGunServer) GetName() string {
return "MailGun"
}
func (s *MailGunServer) SetKey(key string) {
mailGunKey = key
return
}