/
main.go
77 lines (65 loc) · 1.41 KB
/
main.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
package main
import (
"encoding/json"
"github.com/go-gomail/gomail"
"net/http"
"os"
"strings"
"sync"
)
var (
attach string = "./attach.txt"
attachMutex sync.Mutex
)
func response(status bool, msg string) []byte {
result := map[string]string{
"status": "ok",
"msg": msg,
}
if !status {
result["status"] = "error"
}
strresult, _ := json.Marshal(result)
return strresult
}
func main() {
http.HandleFunc("/sync", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
title := r.Form.Get("title")
body := r.Form.Get("body")
if strings.EqualFold(title, "") {
w.Write(response(false, "please pass the title"))
return
}
if strings.EqualFold(body, "") {
w.Write(response(false, "please pass the body"))
return
}
m := gomail.NewMessage()
m.SetHeader("From", "kyf456@163.com")
m.SetHeader("To", "kyf456@163.com", "cora@example.com")
m.SetHeader("Subject", title)
m.SetBody("text/plain", body)
attachMutex.Lock()
defer attachMutex.Unlock()
fp, err := os.Create(attach)
if err != nil {
panic(err)
}
defer func() {
fp.Close()
os.Remove(attach)
}()
fp.Write([]byte(body))
m.Attach(attach)
d := gomail.NewPlainDialer("smtp.163.com", 25, "user", "password")
if err := d.DialAndSend(m); err != nil {
panic(err)
}
w.Write(response(true, "data sync success"))
})
err := http.ListenAndServe(":2225", nil)
if err != nil {
panic(err)
}
}