-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (110 loc) · 2.61 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
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
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"path"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type mailReq struct {
From string `query:"from"`
To string `query:"to"`
Subject string `query:"subject"`
Body string `query:"body"`
File string `query:"file"`
}
func callSendmail(req mailReq) error {
var line = fmt.Sprintf("From: %s\\nTo: %s\\nSubject: %s\\n\\n%s",
req.From, req.To, req.Subject, req.Body)
var script = fmt.Sprintf("echo -e '%s'", line)
fmt.Println(script)
if len(req.File) > 0 {
if _, err := os.Stat(req.File); err == nil {
script = fmt.Sprintf("%s ; uuencode %s %s", script,
req.File, path.Base(req.File))
} else {
fmt.Println(err.Error())
}
}
fmt.Println(script)
var c1 = exec.Command("/bin/sh", "-c", script)
var c2 = exec.Command("sendmail", req.To)
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
_ = c1.Run()
_ = c2.Wait()
return nil
}
func callSendmailMutt(req mailReq) error {
var args = []string{"-s", req.Subject}
if len(req.File) > 0 {
if _, err := os.Stat(req.File); err == nil {
args = append(args, []string{"-a", req.File}...)
} else {
fmt.Println(err.Error())
}
}
fmt.Println(args)
args = append(args, []string{"--", req.To}...)
var c1 = exec.Command("echo", "-e", req.Body)
var c2 = exec.Command("mutt", args...)
c2.Env = os.Environ()
c2.Env = append(c2.Env, fmt.Sprintf("EMAIL=%s", req.From))
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
_ = c1.Run()
_ = c2.Wait()
return nil
}
func main() {
var serverAddr = os.Getenv("SERVER_ADDR")
if len(serverAddr) == 0 {
panic("Server address must not be empty")
}
var e = echo.New()
e.Use(middleware.Recover())
var apiGroup = e.Group("/api")
// GET HTTP endpoint to send emails via sendmail
apiGroup.GET("/sendmail", func(c echo.Context) error {
var req = mailReq{}
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest,
map[string]string{
"status": "error",
"desc": err.Error(),
},
)
}
callSendmail(req)
return c.JSON(http.StatusOK,
map[string]string{
"status": "ok",
},
)
})
// GET HTTP endpoint to send emails via mutt
apiGroup.GET("/sendmutt", func(c echo.Context) error {
var req = mailReq{}
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest,
map[string]string{
"status": "error",
"desc": err.Error(),
},
)
}
callSendmailMutt(req)
return c.JSON(http.StatusOK,
map[string]string{
"status": "ok",
},
)
})
if err := e.Start(serverAddr); err != nil {
panic(err.Error())
}
}