small pixel drawing of a pufferfish nshc-form

Email is working!
j3s j3s@c3f.net
Tue, 09 Jun 2020 23:30:19 -0500
commit

9f69955a21fd4e9827cb157fdc99313b7f8fe796

parent

0e8a4e487c7689747f54822240655ff10d4c3f5f

2 files changed, 39 insertions(+), 1 deletions(-)

jump to
M READMEREADME

@@ -1,2 +1,11 @@

nothing to see here -------------------------- + + +Set the following environment variables: + + SMTP_FROM=forms@example.com + SMTP_PASS=garbageday + SMTP_SERVER=mail.example.com + SMTP_PORT=587 + SMTP_TO=inbox@example.com
M main.gomain.go

@@ -4,16 +4,30 @@ 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) { - render(w, "templates/form.html", nil) + 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{}) {

@@ -29,3 +43,18 @@ 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) + } +}