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) }