From ed008173a778b08a0987a5cfa0a783991eb1735a Mon Sep 17 00:00:00 2001 From: Daniil Sliusar Date: Tue, 13 Aug 2019 20:49:08 +0300 Subject: [PATCH] Add: send email with mutt (better file attach sup) --- Dockerfile | 2 +- main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 872d998..de664ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM interlegis/alpine-postfix LABEL maintainer="Danya Sliusar " -RUN apk add -U --no-cache sharutils +RUN apk add -U --no-cache sharutils mutt ADD ./bin/sendmail-http / ADD start.sh / diff --git a/main.go b/main.go index 1dbac5e..e5c3d4a 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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())