Browse Source

Initial commit.

main
Atridad Lahiji 2 months ago
parent
commit
d1c9e0349f
  1. 54
      .drone.yml
  2. 6
      .env.example
  3. 2
      .gitignore
  4. 12
      README.md
  5. 23
      build.sh
  6. 10
      go.mod
  7. 36
      go.sum
  8. BIN
      mailpoint
  9. 89
      main.go

54
.drone.yml

@ -0,0 +1,54 @@
kind: pipeline
type: docker
name: dev
trigger:
branch:
- master
event:
- push
steps:
- image: golang
name: test-build
commands:
- go build
---
kind: pipeline
type: docker
name: prod
trigger:
event:
- tag
steps:
- image: golang
name: build-backend
commands:
- chmod +x ./build.sh
- ./build.sh
- image: node
name: pre-upload
commands:
- curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 > jq
- chmod +x ./jq
- LATEST_RELEASE_ID=$(curl -s https://git.atrilahiji.dev/api/v1/repos/atrilahiji/mailpoint/releases | ./jq -r '.[0].name')
- rm -rf bin/darwin-amd64
- rm -rf bin/linux-amd64
- mv bin/darwin-amd64.tar.gz bin/$LATEST_RELEASE_ID-darwin-amd64.tar.gz
- mv bin/linux-amd64.tar.gz bin/$LATEST_RELEASE_ID-linux-amd64.tar.gz
- image: plugins/s3
name: upload
settings:
access_key:
from_secret: S3_ACCESS
secret_key:
from_secret: S3_SECRET
bucket: mailpoint
source: bin/**/*
target: /
path_style: true
endpoint: https://s3.atrilahiji.dev

6
.env.example

@ -0,0 +1,6 @@
SMTP_FROM_EMAIL=person@email.com
SMTP_USERNAME=cooluserperson
SMTP_PASSWORD=changeme123
SMTP_HOST=smtp.email.com
SMTP_PORT=587
SECURE_TOKEN=changeme123

2
.gitignore

@ -0,0 +1,2 @@
bin/
.env

12
README.md

@ -1,2 +1,12 @@
# mailpoint
# Mailpoint
Secure sendmail endpoint in golang
## Default config
```
SMTP_FROM_EMAIL=person@email.com
SMTP_USERNAME=cooluserperson
SMTP_PASSWORD=changeme123
SMTP_HOST=smtp.email.com
SMTP_PORT=587
SECURE_TOKEN=changeme123
```

23
build.sh

@ -0,0 +1,23 @@
#!/bin/bash
export OLD_GOOS=$(go env GOOS)
export OLD_GOARCH=$(go env GOARCH)
mkdir bin
# buid for macos x86 64-bit
export GOOS=darwin
export GOARCH=amd64
mkdir bin/$(go env GOOS)-$(go env GOARCH)
cp .env.example ./bin/$(go env GOOS)-$(go env GOARCH)/.env.example
go build -o ./bin/$(go env GOOS)-$(go env GOARCH)/mailpoint-$(go env GOOS)-$(go env GOARCH)
tar -czvf ./bin/$(go env GOOS)-$(go env GOARCH).tar.gz ./bin/$(go env GOOS)-$(go env GOARCH)
# buid for linux x86 64-bit
export GOOS=linux
export GOARCH=amd64
mkdir bin/$(go env GOOS)-$(go env GOARCH)
cp .env.example ./bin/$(go env GOOS)-$(go env GOARCH)/.env.example
go build -o ./bin/$(go env GOOS)-$(go env GOARCH)/mailpoint-$(go env GOOS)-$(go env GOARCH)
tar -czvf ./bin/$(go env GOOS)-$(go env GOARCH).tar.gz ./bin/$(go env GOOS)-$(go env GOARCH)

10
go.mod

@ -0,0 +1,10 @@
module git.atrilahiji.dev/lahijiappsconsulting/mailpoint
go 1.16
require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/joho/godotenv v1.3.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
)

36
go.sum

@ -0,0 +1,36 @@
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb h1:EVl3FJLQCzSbgBezKo/1A4ADnJ4mtJZ0RvnNzDJ44nY=
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/campoy/svg-badge v0.0.0-20180116034456-40c38fcad9f6 h1:tqkfnr9GEZzMyWtQKvgPwWvWxH8RCMA+GeNylbYYEb0=
github.com/campoy/svg-badge v0.0.0-20180116034456-40c38fcad9f6/go.mod h1:kDWVB5nIXF847uNfomMkNoV4QEdH0q0JwMPQvgsvsLI=
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
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/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
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/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stripe/stripe-go/v72 v72.37.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
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/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

BIN
mailpoint

Binary file not shown.

89
main.go

@ -0,0 +1,89 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"encoding/json"
"strconv"
"github.com/joho/godotenv"
"gopkg.in/gomail.v2"
)
func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func frontEndHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, "<h1>M A I L?!?!?!?!?!</h1>")
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Health Check"))
}
func mailHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Max-Age", "3600")
w.WriteHeader(http.StatusNoContent)
return
}
token := r.URL.Query().Get("token")
if (token == os.Getenv("SECURE_TOKEN") || os.Getenv("SECURE_TOKEN") == "") && r.Method == http.MethodPost {
type Request struct {
Sendto string
Subject string
Body string
}
var reqItems Request
_ = json.NewDecoder(r.Body).Decode(&reqItems)
m := gomail.NewMessage()
m.SetHeader("From", os.Getenv("SMTP_FROM_EMAIL"))
m.SetHeader("To", reqItems.Sendto)
m.SetHeader("Subject", reqItems.Subject)
m.SetBody("text/html", reqItems.Body)
port, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
d := gomail.NewDialer(os.Getenv("SMTP_HOST"), port, os.Getenv("SMTP_USERNAME"), os.Getenv("SMTP_PASSWORD"))
if err := d.DialAndSend(m); err != nil {
panic(err)
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST")
} else {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("401 - Unauthorized!"))
}
}
func main() {
loadEnv()
http.HandleFunc("/", frontEndHandler)
http.HandleFunc("/api/healthcheck", healthCheckHandler)
http.HandleFunc("/api/sendmail", mailHandler)
log.Println("Listening on :3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}
Loading…
Cancel
Save