small pixel drawing of a pufferfish nshc-form

main.go

package main

import (
	"html/template"
	"log"
	"net/http"
	"net/smtp"
	"os"
)

func main() {
	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/confirmation", confirmationHandler)

	log.Fatal(http.ListenAndServe(":8080", nil))
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		render(w, "templates/form.html", nil)
	case "POST":
		// message := r.info or whatever
		sendMessage()
		// re-route to /confirmation
	}
}

func confirmationHandler(w http.ResponseWriter, r *http.Request) {
	render(w, "templates/confirmation.html", nil)
}

func render(w http.ResponseWriter, filename string, data interface{}) {
	tmpl, err := template.ParseFiles(filename)

	if err != nil {
		log.Println(err)
		http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
	}

	if err := tmpl.Execute(w, data); err != nil {
		log.Println(err)
		http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
	}
}

func sendMessage() {
	auth := smtp.PlainAuth("", os.Getenv("SMTP_FROM"), os.Getenv("SMTP_PASS"), os.Getenv("SMTP_SERVER"))

	to := []string{os.Getenv("SMTP_TO")}
	msg := []byte("From: " + os.Getenv("SMTP_FROM") + "\r\n" +
        "To: " + os.Getenv("SMTP_TO") + "\r\n" +
		"Subject: Form completion\r\n" +
		"\r\n" +
		"Body.\r\n")
	err := smtp.SendMail(os.Getenv("SMTP_SERVER") + ":" + os.Getenv("SMTP_PORT"), auth, os.Getenv("SMTP_FROM"), to, msg)
	if err != nil {
		log.Fatal(err)
	}
}