Added resend, fixed CSRF
This commit is contained in:
@@ -5,10 +5,9 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"gopkg.in/gomail.v2"
|
||||
"github.com/resend/resend-go/v2"
|
||||
)
|
||||
|
||||
type ContactForm struct {
|
||||
@@ -31,29 +30,29 @@ func HandleContact(w http.ResponseWriter, r *http.Request) {
|
||||
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
|
||||
apiKey := os.Getenv("RESEND_APIKEY")
|
||||
sendTo := os.Getenv("SMTP_EMAIL")
|
||||
client := resend.NewClient(apiKey)
|
||||
params := &resend.SendEmailRequest{
|
||||
From: "onboarding@resend.dev",
|
||||
To: []string{sendTo},
|
||||
Subject: form.Subject,
|
||||
Html: fmt.Sprintf("<h1><strong>%s %s</strong> (%s) vous a envoyé un mail.</h1></br></br>%s",
|
||||
form.Firstname, form.Lastname, form.EMail, form.Message),
|
||||
ReplyTo: form.EMail,
|
||||
}
|
||||
d := gomail.NewDialer(os.Getenv("SMTP_DOMAIN"), port, os.Getenv("SMTP_EMAIL"), os.Getenv("SMTP_APP_PASSWORD"))
|
||||
|
||||
if err = d.DialAndSend(); err != nil {
|
||||
sent, err := client.Emails.Send(params)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(*sent)
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Email sent.",
|
||||
|
||||
Reference in New Issue
Block a user