Skip to content

Commit

Permalink
Add file attach to email send
Browse files Browse the repository at this point in the history
  • Loading branch information
danyanya committed Jul 31, 2019
1 parent c68f816 commit 10cdb29
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM interlegis/alpine-postfix

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

RUN apk add -U --no-cache sharutils

ADD ./bin/sendmail-http /
ADD start.sh /

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.6'
services:
postfix-http:
image: danyanya/postfix-http
image: danyanya/sendmail-http:latest
entrypoint: /start.sh
environment:
- SERVER_ADDR=:1001
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module bitbucket.org/wimarksystems/sendmail-http

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.2.9 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.2.9 h1:heVeuAYtevIQVYkGj6A41dtfT91LrvFG220lavpWhrU=
github.com/labstack/gommon v0.2.9/go.mod h1:E8ZTmW9vw5az5/ZyHWCp0Lw4OH2ecsaBP1C/NKavGG4=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
50 changes: 35 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"os/exec"
"path"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
Expand All @@ -15,13 +16,27 @@ type mailReq struct {
To string `query:"to"`
Subject string `query:"subject"`
Body string `query:"body"`
File string `query:"file"`
}

func callSendmail(req mailReq) error {
line := fmt.Sprintf("From: %s\\nTo: %s\\nSubject: %s\\n\\n%s",
var 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)
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()
Expand All @@ -37,27 +52,32 @@ func main() {
panic("Server address must not be empty")
}

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

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

api_group.GET("/sendmail", func(c echo.Context) error {
// GET HTTP endpoint to send Emails with sendmail
apiGroup.GET("/sendmail", func(c echo.Context) error {

req := mailReq{}
var req = mailReq{}
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"status": "error",
"desc": err.Error(),
})
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",
})
return c.JSON(http.StatusOK,
map[string]string{
"status": "ok",
},
)
})

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

0 comments on commit 10cdb29

Please sign in to comment.