From ee668356498dd0b836d6bc6c9424d8adceaa0f89 Mon Sep 17 00:00:00 2001 From: "$(pass /github/name)" <$(pass /github/email)> Date: Wed, 21 Aug 2024 12:29:13 +0200 Subject: [PATCH] changed contact form reply_to and response html --- cmd/membership/main.go | 1 - internal/controllers/contactController.go | 13 ++++++++----- internal/controllers/contactController_test.go | 6 +++--- internal/services/email_service.go | 13 ++++++++----- templates/html/contactForm_reply.html | 15 +++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 templates/html/contactForm_reply.html diff --git a/cmd/membership/main.go b/cmd/membership/main.go index 6af1591..08f2244 100644 --- a/cmd/membership/main.go +++ b/cmd/membership/main.go @@ -52,7 +52,6 @@ func gracefulShutdown() { if err := server.Shutdown(ctx); err != nil { logger.Error.Fatalf("Error during Server shutdown: %#v", err) } else { - logger.Info.Println("Server gracefully stopped") } } diff --git a/internal/controllers/contactController.go b/internal/controllers/contactController.go index 28ca706..629925e 100644 --- a/internal/controllers/contactController.go +++ b/internal/controllers/contactController.go @@ -15,7 +15,7 @@ type ContactController struct { EmailService *services.EmailService } type contactData struct { - Email string `form:"email" validate:"required,email"` + Email string `form:"REPLY_TO" validate:"required,email"` Name string `form:"name"` Message string `form:"message" validate:"required"` Honeypot string `form:"username" validate:"eq="` @@ -32,16 +32,19 @@ func (cc *ContactController) RelayContactRequest(c *gin.Context) { 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"}) + logger.Error.Printf("Couldn't validate contact form data: %#v: %v", msgData, err) + c.HTML(http.StatusOK, "contactForm_reply.html", gin.H{"Error": "Form submission failed. Please try again."}) + // c.JSON(http.StatusNotAcceptable, gin.H{"error": "Couldn't validate contact form data"}) return } 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.HTML(http.StatusOK, "contactForm_reply.html", gin.H{"Error": "Form submission failed. Please try again."}) + // c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't send mail"}) return } - c.JSON(http.StatusAccepted, "Your message has been sent") + // c.JSON(http.StatusAccepted, "Your message has been sent") + c.HTML(http.StatusOK, "contactForm_reply.html", gin.H{"Success": true}) } diff --git a/internal/controllers/contactController_test.go b/internal/controllers/contactController_test.go index 5beb71f..a08b1bd 100644 --- a/internal/controllers/contactController_test.go +++ b/internal/controllers/contactController_test.go @@ -95,7 +95,7 @@ func getBaseRequest() *url.Values { return &url.Values{ "username": {""}, "name": {"My-First and-Last-Name"}, - "email": {"name@domain.de"}, + "REPLY_TO": {"name@domain.de"}, "message": {"My message to the world"}, } } @@ -116,7 +116,7 @@ func getContactData() []RelayContactRequestTest { Assert: false, Input: *customizeRequest( map[string]string{ - "email": "", + "REPLY_TO": "", }), }, { @@ -125,7 +125,7 @@ func getContactData() []RelayContactRequestTest { Assert: false, Input: *customizeRequest( map[string]string{ - "email": "novalid#email.de", + "REPLY_TO": "novalid#email.de", }), }, { diff --git a/internal/services/email_service.go b/internal/services/email_service.go index 7334b5e..1b7797a 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -21,11 +21,14 @@ func NewEmailService(host string, port int, username string, password string) *E return &EmailService{dialer: dialer} } -func (s *EmailService) SendEmail(to string, subject string, body string) error { +func (s *EmailService) SendEmail(to string, subject string, body string, replyTo string) error { msg := gomail.NewMessage() msg.SetHeader("From", s.dialer.Username) msg.SetHeader("To", to) msg.SetHeader("Subject", subject) + if replyTo != "" { + msg.SetHeader("REPLY_TO", replyTo) + } msg.SetBody("text/html", body) if err := s.dialer.DialAndSend(msg); err != nil { @@ -76,7 +79,7 @@ func (s *EmailService) SendVerificationEmail(user *models.User, token *string) e logger.Error.Print("Couldn't send verification mail") return err } - return s.SendEmail(user.Email, subject, body) + return s.SendEmail(user.Email, subject, body, "") } @@ -106,7 +109,7 @@ func (s *EmailService) SendWelcomeEmail(user *models.User) error { logger.Error.Print("Couldn't send welcome mail") return err } - return s.SendEmail(user.Email, subject, body) + return s.SendEmail(user.Email, subject, body, "") } func (s *EmailService) SendRegistrationNotification(user *models.User) error { @@ -151,7 +154,7 @@ func (s *EmailService) SendRegistrationNotification(user *models.User) error { logger.Error.Print("Couldn't send admin notification mail") return err } - return s.SendEmail(config.Recipients.UserRegistration, subject, body) + return s.SendEmail(config.Recipients.UserRegistration, subject, body, "") } func (s *EmailService) RelayContactFormMessage(sender string, name string, message string) error { @@ -170,5 +173,5 @@ func (s *EmailService) RelayContactFormMessage(sender string, name string, messa logger.Error.Print("Couldn't send contact form message mail") return err } - return s.SendEmail(config.Recipients.ContactForm, subject, body) + return s.SendEmail(config.Recipients.ContactForm, subject, body, sender) } diff --git a/templates/html/contactForm_reply.html b/templates/html/contactForm_reply.html new file mode 100644 index 0000000..f933aed --- /dev/null +++ b/templates/html/contactForm_reply.html @@ -0,0 +1,15 @@ + + +
+ + +Nachricht wurde gesendet!
+ {{ end }} {{ if .Error }} +Fehler: {{ .Error }}
+ {{ end }} + +