diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 2e98def..0fca8ac 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -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" ) diff --git a/internal/controllers/contactController.go b/internal/controllers/contactController.go new file mode 100644 index 0000000..128fa79 --- /dev/null +++ b/internal/controllers/contactController.go @@ -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") +} diff --git a/internal/routes/routes.go b/internal/routes/routes.go index c3ceada..a30c73d 100644 --- a/internal/routes/routes.go +++ b/internal/routes/routes.go @@ -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") } diff --git a/internal/services/email_service.go b/internal/services/email_service.go index 244c3a2..18c6ea0 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -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) +} diff --git a/templates/email/mail_contact_form.html b/templates/email/mail_contact_form.html new file mode 100644 index 0000000..7b12d21 --- /dev/null +++ b/templates/email/mail_contact_form.html @@ -0,0 +1,89 @@ + + +
+ + +