Skip to content

Commit

Permalink
Simple HTTP API to sh's sendmail
Browse files Browse the repository at this point in the history
  • Loading branch information
danyanya committed Apr 4, 2019
0 parents commit aeedb48
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build:
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/sendmail-http
clean:
rm ./bin/sendmail-http
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simple http to sendmail
65 changes: 65 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"net/http"
"os"
"os/exec"

"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"`
}

func callSendmail(req mailReq) error {
line := fmt.Sprintf("From: %s\\nTo: %s\\nSubject: %s\\n\\n%s",
req.From, req.To, req.Subject, req.Body)
c1 := exec.Command("echo", "-e", line)
c2 := exec.Command("sendmail", req.To)
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")
}

e := echo.New()
e.Use(middleware.Recover())

api_group := e.Group("/api")

api_group.GET("/sendmail", func(c echo.Context) error {

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

err := e.Start(serverAddr)
if err != nil {
panic(err.Error())
}

}

0 comments on commit aeedb48

Please sign in to comment.