-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (100 loc) · 3.15 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/smtp"
"os"
"regexp"
)
// Gmail SMTP configuration from environment variables
const (
smtpServer = "smtp.gmail.com"
smtpPort = "587"
)
// Email struct to handle JSON data
type Email struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
}
func main() {
http.HandleFunc("/submit", corsMiddleware(submitContactForm))
log.Println("Server starting on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// CORS middleware to add the necessary headers
func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// Handle preflight requests (OPTIONS)
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
next(w, r)
}
}
// Handle form submission and send email
func submitContactForm(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
if r.Header.Get("Content-Type") != "application/json" {
http.Error(w, "Content-Type must be application/json", http.StatusUnsupportedMediaType)
return
}
var email Email
err := json.NewDecoder(r.Body).Decode(&email)
if err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
// Validate email format
if !isValidEmail(email.Email) {
http.Error(w, "Invalid email address", http.StatusBadRequest)
return
}
// Validate that message is not empty
if email.Message == "" {
http.Error(w, "Message cannot be empty", http.StatusBadRequest)
return
}
// Get Gmail credentials from environment variables
username := os.Getenv("GMAIL_USERNAME")
password := os.Getenv("GMAIL_PASSWORD")
if username == "" || password == "" {
http.Error(w, "SMTP credentials not configured", http.StatusInternalServerError)
return
}
subject := "New Contact Form Submission"
body := fmt.Sprintf("Name: %s\nEmail: %s\nMessage:\n%s", email.Name, email.Email, email.Message)
message := []byte("To: " + username + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n\r\n" + body)
auth := smtp.PlainAuth("", username, password, smtpServer)
err = smtp.SendMail(smtpServer+":"+smtpPort, auth, username, []string{username}, message)
if err != nil {
http.Error(w, "Failed to send email: "+err.Error(), http.StatusInternalServerError)
return
}
// Prepare a success response
response := map[string]string{
"status": "success",
"message": "Your message has been sent successfully. Thank you for contacting Adioz!",
}
// Write JSON response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
// Helper function to validate email format
func isValidEmail(email string) bool {
const emailRegex = `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
re := regexp.MustCompile(emailRegex)
return re.MatchString(email)
}