package api import ( "encoding/json" "fmt" "net/http" "os" "strconv" "fr.latosa-escrima/core" "gopkg.in/gomail.v2" ) type ContactForm struct { Firstname string `json:"firstname"` Lastname string `json:"lastname"` EMail string `json:"email"` Subject string `json:"subject"` Message string `json:"message"` } func HandleContact(w http.ResponseWriter, r *http.Request) { // TODO: Warning email not being sent ? var form ContactForm err := json.NewDecoder(r.Body).Decode(&form) if err != nil { core.JSONError{ Status: core.Error, Message: err.Error(), }.Respond(w, http.StatusBadRequest) return } fmt.Println("Received form", form) fmt.Println("ENV:", os.Environ()) m := gomail.NewMessage() m.SetHeader("From", os.Getenv("SMTP_EMAIL")) // m.SetHeader("Reply-To", form.EMail) m.SetHeader("To", os.Getenv("SMTP_EMAIL")) m.SetHeader("Subject", form.Subject) m.SetBody("text/plain", fmt.Sprintf("%s %s vous a envoyé un email:\n\n%s", form.Firstname, form.Lastname, form.Message)) port, err := strconv.Atoi(os.Getenv("SMTP_PORT")) if err != nil { port = 587 } d := gomail.NewDialer(os.Getenv("SMTP_DOMAIN"), port, os.Getenv("SMTP_EMAIL"), os.Getenv("SMTP_APP_PASSWORD")) if err = d.DialAndSend(); err != nil { core.JSONError{ Status: core.Error, Message: err.Error(), }.Respond(w, http.StatusInternalServerError) return } core.JSONSuccess{ Status: core.Success, Message: "Email sent.", }.Respond(w, http.StatusAccepted) }