changed contact form reply_to and response html

This commit is contained in:
$(pass /github/name)
2024-08-21 12:29:13 +02:00
parent 3224536e56
commit ee66835649
5 changed files with 34 additions and 14 deletions

View File

@@ -52,7 +52,6 @@ func gracefulShutdown() {
if err := server.Shutdown(ctx); err != nil { if err := server.Shutdown(ctx); err != nil {
logger.Error.Fatalf("Error during Server shutdown: %#v", err) logger.Error.Fatalf("Error during Server shutdown: %#v", err)
} else { } else {
logger.Info.Println("Server gracefully stopped") logger.Info.Println("Server gracefully stopped")
} }
} }

View File

@@ -15,7 +15,7 @@ type ContactController struct {
EmailService *services.EmailService EmailService *services.EmailService
} }
type contactData struct { type contactData struct {
Email string `form:"email" validate:"required,email"` Email string `form:"REPLY_TO" validate:"required,email"`
Name string `form:"name"` Name string `form:"name"`
Message string `form:"message" validate:"required"` Message string `form:"message" validate:"required"`
Honeypot string `form:"username" validate:"eq="` Honeypot string `form:"username" validate:"eq="`
@@ -32,16 +32,19 @@ func (cc *ContactController) RelayContactRequest(c *gin.Context) {
validate := validator.New() validate := validator.New()
if err := validate.Struct(msgData); err != nil { if err := validate.Struct(msgData); err != nil {
logger.Error.Printf("Couldn't validate contact form data: %v", err) logger.Error.Printf("Couldn't validate contact form data: %#v: %v", msgData, err)
c.JSON(http.StatusNotAcceptable, gin.H{"error": "Couldn't validate contact form data"}) 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 return
} }
if err := cc.EmailService.RelayContactFormMessage(msgData.Email, msgData.Name, msgData.Message); err != nil { if err := cc.EmailService.RelayContactFormMessage(msgData.Email, msgData.Name, msgData.Message); err != nil {
logger.Error.Printf("Couldn't send contact message mail: %v", err) 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 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})
} }

View File

@@ -95,7 +95,7 @@ func getBaseRequest() *url.Values {
return &url.Values{ return &url.Values{
"username": {""}, "username": {""},
"name": {"My-First and-Last-Name"}, "name": {"My-First and-Last-Name"},
"email": {"name@domain.de"}, "REPLY_TO": {"name@domain.de"},
"message": {"My message to the world"}, "message": {"My message to the world"},
} }
} }
@@ -116,7 +116,7 @@ func getContactData() []RelayContactRequestTest {
Assert: false, Assert: false,
Input: *customizeRequest( Input: *customizeRequest(
map[string]string{ map[string]string{
"email": "", "REPLY_TO": "",
}), }),
}, },
{ {
@@ -125,7 +125,7 @@ func getContactData() []RelayContactRequestTest {
Assert: false, Assert: false,
Input: *customizeRequest( Input: *customizeRequest(
map[string]string{ map[string]string{
"email": "novalid#email.de", "REPLY_TO": "novalid#email.de",
}), }),
}, },
{ {

View File

@@ -21,11 +21,14 @@ func NewEmailService(host string, port int, username string, password string) *E
return &EmailService{dialer: dialer} 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 := gomail.NewMessage()
msg.SetHeader("From", s.dialer.Username) msg.SetHeader("From", s.dialer.Username)
msg.SetHeader("To", to) msg.SetHeader("To", to)
msg.SetHeader("Subject", subject) msg.SetHeader("Subject", subject)
if replyTo != "" {
msg.SetHeader("REPLY_TO", replyTo)
}
msg.SetBody("text/html", body) msg.SetBody("text/html", body)
if err := s.dialer.DialAndSend(msg); err != nil { 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") logger.Error.Print("Couldn't send verification mail")
return err 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") logger.Error.Print("Couldn't send welcome mail")
return err return err
} }
return s.SendEmail(user.Email, subject, body) return s.SendEmail(user.Email, subject, body, "")
} }
func (s *EmailService) SendRegistrationNotification(user *models.User) error { 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") logger.Error.Print("Couldn't send admin notification mail")
return err 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 { 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") logger.Error.Print("Couldn't send contact form message mail")
return err return err
} }
return s.SendEmail(config.Recipients.ContactForm, subject, body) return s.SendEmail(config.Recipients.ContactForm, subject, body, sender)
} }

View File

@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Form Submission</title>
</head>
<body>
{{ if .Success }}
<p>Nachricht wurde gesendet!</p>
{{ end }} {{ if .Error }}
<p>Fehler: {{ .Error }}</p>
{{ end }}
</body>
</html>