wip: added contact form handling

This commit is contained in:
$(pass /github/name)
2024-08-13 21:47:04 +02:00
parent 9cbe3d005b
commit 6441ad3ef9
5 changed files with 159 additions and 3 deletions

View File

@@ -12,4 +12,5 @@ const (
MailVerificationSubject = "Nur noch ein kleiner Schritt!"
MailRegistrationSubject = "Neues Mitglied hat sich registriert"
MailWelcomeSubject = "Willkommen beim Dörpsmobil Hasloh e.V."
MailContactSubject = "Jemand hat das Kontaktformular gefunden"
)

View File

@@ -0,0 +1,44 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"GoMembership/internal/services"
"GoMembership/pkg/logger"
)
type ContactController struct {
EmailService *services.EmailService
}
type contactData struct {
email string `validate:"required,email"`
name string
message string `validate:"required"`
}
func (cc *ContactController) RelayContactRequest(c *gin.Context) {
var msgData contactData
if c.Query("username") != "" {
// A bot is talking to us
return
}
msgData.name = c.Query("name")
msgData.email = c.Query("email")
msgData.message = c.Query("message")
validate := validator.New()
if err := validate.Struct(msgData); err != nil {
logger.Error.Printf("Couldn't validate contact form data: %v", err)
c.JSON(http.StatusNotAcceptable, gin.H{"error": "Couldn't validate contact form data"})
}
if err := cc.EmailService.RelayContactFormMessage(&msgData.email, &msgData.name, &msgData.message); err != nil {
logger.Error.Printf("Couldn't send contact message mail: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't send mail"})
}
c.JSON(http.StatusOK, "Your message has been sent")
}

View File

@@ -10,6 +10,7 @@ func RegisterRoutes(router *gin.Engine, userController *controllers.UserControll
router.GET("/backend/verify", userController.VerifyMailHandler)
router.POST("/backend/api/register", userController.RegisterUser)
router.POST("/backend/api/register/subscription", membershipcontroller.RegisterSubscription)
router.POST("/backend/api/contact", contactController.RelayContactRequest)
// router.HandleFunc("/login", userController.LoginUser).Methods("POST")
}

View File

@@ -1,13 +1,15 @@
package services
import (
"bytes"
"html/template"
"gopkg.in/gomail.v2"
"GoMembership/internal/config"
"GoMembership/internal/constants"
"GoMembership/internal/models"
"GoMembership/pkg/logger"
"bytes"
"gopkg.in/gomail.v2"
"html/template"
)
type EmailService struct {
@@ -152,3 +154,22 @@ func (s *EmailService) NotifyAdminOfNewUser(user *models.User) error {
}
return s.SendEmail(config.SMTP.AdminEmail, subject, body)
}
func (s *EmailService) RelayContactFormMessage(sender string, name string, message string) error {
data := struct {
Message string
Name string
BASEURL string
}{
Message: message,
Name: name,
BASEURL: config.BaseURL,
}
subject := constants.MailContactSubject
body, err := ParseTemplate("mail_contact_form.html", data)
if err != nil {
logger.Error.Print("Couldn't send contact form message mail")
return err
}
return s.SendEmail(config.SMTP.AdminEmail, subject, body)
}