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!" MailVerificationSubject = "Nur noch ein kleiner Schritt!"
MailRegistrationSubject = "Neues Mitglied hat sich registriert" MailRegistrationSubject = "Neues Mitglied hat sich registriert"
MailWelcomeSubject = "Willkommen beim Dörpsmobil Hasloh e.V." 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.GET("/backend/verify", userController.VerifyMailHandler)
router.POST("/backend/api/register", userController.RegisterUser) router.POST("/backend/api/register", userController.RegisterUser)
router.POST("/backend/api/register/subscription", membershipcontroller.RegisterSubscription) router.POST("/backend/api/register/subscription", membershipcontroller.RegisterSubscription)
router.POST("/backend/api/contact", contactController.RelayContactRequest)
// router.HandleFunc("/login", userController.LoginUser).Methods("POST") // router.HandleFunc("/login", userController.LoginUser).Methods("POST")
} }

View File

@@ -1,13 +1,15 @@
package services package services
import ( import (
"bytes"
"html/template"
"gopkg.in/gomail.v2"
"GoMembership/internal/config" "GoMembership/internal/config"
"GoMembership/internal/constants" "GoMembership/internal/constants"
"GoMembership/internal/models" "GoMembership/internal/models"
"GoMembership/pkg/logger" "GoMembership/pkg/logger"
"bytes"
"gopkg.in/gomail.v2"
"html/template"
) )
type EmailService struct { type EmailService struct {
@@ -152,3 +154,22 @@ func (s *EmailService) NotifyAdminOfNewUser(user *models.User) error {
} }
return s.SendEmail(config.SMTP.AdminEmail, subject, body) 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)
}

View File

@@ -0,0 +1,89 @@
<!doctype html>
<html>
<body>
<div
style="
background-color: #f2f5f7;
color: #242424;
font-family: &quot;Helvetica Neue&quot;, &quot;Arial Nova&quot;,
&quot;Nimbus Sans&quot;, Arial, sans-serif;
font-size: 16px;
font-weight: 400;
letter-spacing: 0.15008px;
line-height: 1.5;
margin: 0;
padding: 32px 0;
min-height: 100%;
width: 100%;
"
>
<table
align="center"
width="100%"
style="margin: 0 auto; max-width: 600px; background-color: #ffffff"
role="presentation"
cellspacing="0"
cellpadding="0"
border="0"
>
<tbody>
<tr style="width: 100%">
<td>
<div style="padding: 0px 24px 0px 24px">
<a
href="https://carsharing-hasloh.de"
style="text-decoration: none"
target="_blank"
><img
alt="Carsharing-Hasloh"
src="https://carsharing-hasloh.de/images/CarsharingSH-Hasloh-LOGO.jpeg"
style="
outline: none;
border: none;
text-decoration: none;
vertical-align: middle;
display: inline-block;
max-width: 100%;
"
/></a>
</div>
<div style="font-weight: normal; padding: 0px 24px 16px 24px">
Moin Du Vorstand 👋,
</div>
<div style="font-weight: normal; padding: 0px 24px 16px 24px">
<p>Eine neue Kontaktanfrage<br />{{.Name}} hat geschrieben</p>
</div>
<div
style="
font-size: 22px;
font-weight: normal;
text-align: center;
padding: 0px 24px 0px 24px;
"
>
Hier ist die Nachricht:
</div>
<div style="font-weight: normal; padding: 16px 24px 0px 24px">
<p>{{.Message}}</p>
</div>
<div style="padding: 16px 0px 16px 0px">
<hr
style="
width: 100%;
border: none;
border-top: 1px solid #cccccc;
margin: 0;
"
/>
</div>
<div style="font-weight: normal; padding: 16px 24px 16px 24px">
<p>Mit freundlichen Grüßen,</p>
<p>Dein untertänigster Wolkenrechner</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>