-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_form.go
71 lines (57 loc) · 1.39 KB
/
contact_form.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
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/json"
"github.com/gorilla/schema"
"net/http"
)
var decoder = schema.NewDecoder()
type contactForm struct {
Firstname string
Lastname string
Email string
Subject string
Message string
ReCaptcha string
Body string
}
type response struct {
Code int64
Message string
}
func (h *Handlers) contactFormHandler(w http.ResponseWriter, r *http.Request) {
response := response{
Code: 200,
Message: "The email has been sent",
}
w.Header().Set("Content-Type", "application/json")
if r.URL.Path != "/contact" {
http.Error(w, "404 Not Found", http.StatusNotFound)
return
}
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
err := r.ParseForm()
if err != nil {
http.Error(w, "An error occurred. Please contact administrator ([email protected])", http.StatusInternalServerError)
return
}
if r.ContentLength == 0 {
http.Error(w, "Information cannot be empty", http.StatusBadRequest)
return
}
var contactForm contactForm
err = decoder.Decode(&contactForm, r.Form)
if err != nil {
response.Code = 400
response.Message = "Invalid schema"
json.NewEncoder(w).Encode(response)
return
}
err = contactForm.ParseTemplate("mail.html", contactForm, h)
checkErr(err)
_ = mailer(contactForm)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}