main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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) } }