Skip to content

Commit

Permalink
Add: send email with mutt (better file attach sup)
Browse files Browse the repository at this point in the history
  • Loading branch information
danyanya committed Aug 13, 2019
1 parent 10cdb29 commit ed00817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM interlegis/alpine-postfix

LABEL maintainer="Danya Sliusar <[email protected]>"

RUN apk add -U --no-cache sharutils
RUN apk add -U --no-cache sharutils mutt

ADD ./bin/sendmail-http /
ADD start.sh /
Expand Down
43 changes: 43 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ func callSendmail(req mailReq) error {
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")
Expand Down Expand Up @@ -77,6 +101,25 @@ func main() {
)
})

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",
},
)
})

var err = e.Start(serverAddr)
if err != nil {
panic(err.Error())
Expand Down

0 comments on commit ed00817

Please sign in to comment.