mirror of
https://github.com/Dvorinka/SendMail.git
synced 2026-07-29 01:53:47 +00:00
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"gopkg.in/gomail.v2"
|
|
)
|
|
|
|
type FormData struct {
|
|
Domain string `json:"domain"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
Subject string `json:"subject"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type EmailConfig struct {
|
|
Email string
|
|
Password string
|
|
SMTPHost string
|
|
SMTPPort int
|
|
}
|
|
|
|
var domainMap = map[string]EmailConfig{
|
|
"tdvorak.dev": {"[email protected]", "%8s3Yad*!b3*t", "smtp.purelymail.com", 465},
|
|
"vbly.org": {"[email protected]", "!N0^e0Q9k*mV", "smtp.purelymail.com", 465},
|
|
"sportcreative.eu": {"[email protected]", "@dN$TZPgG5!", "smtp.purelymail.com", 465},
|
|
"reklik.net": {"[email protected]", "FK8eAwJbSkP2%H", "smtp.purelymail.com", 465},
|
|
"zeusport.eu": {"[email protected]", "r-:#sSQ?9D%C4t8", "smtp.forpsi.com", 465},
|
|
}
|
|
|
|
func sendHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var data FormData
|
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
data.Domain = strings.ToLower(strings.TrimSpace(data.Domain))
|
|
config, ok := domainMap[data.Domain]
|
|
if !ok {
|
|
http.Error(w, "Unknown domain", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
m := gomail.NewMessage()
|
|
m.SetHeader("From", config.Email)
|
|
m.SetHeader("To", config.Email) // you can change this to forward to another inbox
|
|
m.SetHeader("Subject", data.Subject)
|
|
m.SetBody("text/plain", "Name: "+data.Name+"\nEmail: "+data.Email+"\n\n"+data.Message)
|
|
|
|
d := gomail.NewDialer(config.SMTPHost, config.SMTPPort, config.Email, config.Password)
|
|
d.SSL = true // Using SSL for secure connection
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
|
log.Println("Email send error:", err)
|
|
http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Write([]byte("Email sent successfully"))
|
|
}
|
|
|
|
func enableCors(handler http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Allow all origins
|
|
origin := r.Header.Get("Origin")
|
|
if origin != "" {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
|
|
}
|
|
|
|
// Handle preflight requests
|
|
if r.Method == http.MethodOptions {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
handler(w, r)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/send", enableCors(sendHandler))
|
|
log.Println("Server running on :660")
|
|
log.Fatal(http.ListenAndServe(":660", nil))
|
|
}
|