Email is working!
j3s j3s@c3f.net
Tue, 09 Jun 2020 23:30:19 -0500
M
main.go
→
main.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) + } +}