From 2e72ea822757f525d340cd300468c68ab6234b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dvo=C5=99=C3=A1k?= <150935816+Dvorinka@users.noreply.github.com> Date: Mon, 19 May 2025 09:52:43 +0200 Subject: [PATCH] Add files via upload --- main.go | 234 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 117 insertions(+), 117 deletions(-) diff --git a/main.go b/main.go index 44bd756..8502f5b 100644 --- a/main.go +++ b/main.go @@ -1,117 +1,117 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "net/smtp" - - "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" -) - -type TripEntry struct { - Name string `json:"name" binding:"required"` - Destination string `json:"destination" binding:"required"` - Date string `json:"date" binding:"required"` - Purpose string `json:"purpose" binding:"required"` - KmStart int `json:"km_start" binding:"required"` - KmEnd int `json:"km_end" binding:"required"` -} - -func main() { - r := gin.Default() - - // Enable CORS for all origins - r.Use(cors.Default()) - - r.POST("/submit", func(c *gin.Context) { - var entry TripEntry - if err := c.ShouldBindJSON(&entry); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - // Send email with trip details - err := sendEmail(entry) - if err != nil { - log.Println("Failed to send email:", err) - c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to send email"}) - return - } - - c.JSON(http.StatusOK, gin.H{"message": "Entry submitted and email sent successfully"}) - }) - - r.Run(":8080") -} - -func sendEmail(entry TripEntry) error { - smtpHost := "smtp.example.com" - smtpPort := "587" - sender := "your@email.com" - password := "yourpassword" - recipient := "fleet@company.com" - - auth := smtp.PlainAuth("", sender, password, smtpHost) - - subject := "Nový záznam o jízdě služebním autem" - - body := fmt.Sprintf(` - - - - - -
-

Záznam o jízdě služebním autem

-

Řidič: %s

-

Kam: %s

-

Datum: %s

-

Účel jízdy: %s

-

Kilometry na začátku: %d km

-

Kilometry na konci: %d km

-

Ujeté kilometry: %d km

-
- - - `, entry.Name, entry.Destination, entry.Date, entry.Purpose, entry.KmStart, entry.KmEnd, entry.KmEnd-entry.KmStart) - - msg := []byte( - "MIME-Version: 1.0\r\n" + - "Content-Type: text/html; charset=\"UTF-8\"\r\n" + - "To: " + recipient + "\r\n" + - "Subject: " + subject + "\r\n" + - "\r\n" + body + "\r\n", - ) - - return smtp.SendMail(smtpHost+":"+smtpPort, auth, sender, []string{recipient}, msg) -} +package main + +import ( + "fmt" + "log" + "net/http" + "net/smtp" + + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" +) + +type TripEntry struct { + Name string `json:"name" binding:"required"` + Destination string `json:"destination" binding:"required"` + Date string `json:"date" binding:"required"` + Purpose string `json:"purpose" binding:"required"` + KmStart int `json:"km_start" binding:"required"` + KmEnd int `json:"km_end" binding:"required"` +} + +func main() { + r := gin.Default() + + // Enable CORS for all origins + r.Use(cors.Default()) + + r.POST("/submit", func(c *gin.Context) { + var entry TripEntry + if err := c.ShouldBindJSON(&entry); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + // Send email with trip details + err := sendEmail(entry) + if err != nil { + log.Println("Failed to send email:", err) + c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to send email"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Entry submitted and email sent successfully"}) + }) + + r.Run(":8080") +} + +func sendEmail(entry TripEntry) error { + smtpHost := "smtp.gmail.com" + smtpPort := "465" + sender := "contact.dvorak@gmail.com" + password := "pnhkcsahbwsbpyqj" + recipient := "contact.dvorak@gmail.com" + + auth := smtp.PlainAuth("", sender, password, smtpHost) + + subject := "Nový záznam o jízdě služebním autem" + + body := fmt.Sprintf(` + + + + + +
+

Záznam o jízdě služebním autem

+

Řidič: %s

+

Kam: %s

+

Datum: %s

+

Účel jízdy: %s

+

Kilometry na začátku: %d km

+

Kilometry na konci: %d km

+

Ujeté kilometry: %d km

+
+ + + `, entry.Name, entry.Destination, entry.Date, entry.Purpose, entry.KmStart, entry.KmEnd, entry.KmEnd-entry.KmStart) + + msg := []byte( + "MIME-Version: 1.0\r\n" + + "Content-Type: text/html; charset=\"UTF-8\"\r\n" + + "To: " + recipient + "\r\n" + + "Subject: " + subject + "\r\n" + + "\r\n" + body + "\r\n", + ) + + return smtp.SendMail(smtpHost+":"+smtpPort, auth, sender, []string{recipient}, msg) +}