mirror of
https://github.com/Dvorinka/SendMail.git
synced 2026-06-04 00:52:56 +00:00
first commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
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
|
||||
}
|
||||
|
||||
var domainMap = map[string]EmailConfig{
|
||||
"tdvorak.dev": {"info@tdvorak.dev", "%8s3Yad*!b3*t"},
|
||||
"vbly.org": {"info@vbly.org", "!N0^e0Q9k*mV"},
|
||||
"sportcreative.eu": {"info@sportcreative.eu", "@dN$TZPgG5!"},
|
||||
}
|
||||
|
||||
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("smtp.purelymail.com", 465, config.Email, config.Password)
|
||||
d.SSL = true // Purelymail recommends SSL on port 465
|
||||
|
||||
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 main() {
|
||||
http.HandleFunc("/send", sendHandler)
|
||||
log.Println("Server running on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
Reference in New Issue
Block a user