From b7ca6801d5d4834cbfeea496ed7e478ca6957007 Mon Sep 17 00:00:00 2001 From: Konstantinos Koutsilis <50744802+kkoutsilis@users.noreply.github.com> Date: Sun, 31 Dec 2023 15:07:57 +0200 Subject: [PATCH] Add html template for emails (#7) --- cmd/root.go | 18 +++++++++++- templates/email_template.html | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 templates/email_template.html diff --git a/cmd/root.go b/cmd/root.go index 7d04eab..05d34dd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,11 +3,13 @@ package cmd import ( "encoding/json" "errors" + "html/template" "log" "math/rand" "os" "path/filepath" "strconv" + "strings" "github.com/spf13/cobra" "gopkg.in/gomail.v2" @@ -84,8 +86,22 @@ func sendEmail(to, subject, body string) error { func sendEmails(matches []MatchPair) { subject := "Your Secret Santa Match!" + + templateFile := "./templates/email_template.html" + tmpl, err := template.ParseFiles(templateFile) + if err != nil { + log.Fatalf("Error parsing email template: %v", err) + } + for _, match := range matches { - emailBody := "Hello " + match.From.Name + ",

You are the secret Santa for " + match.To.Name + "!

Best regards,
Secret Santa Match Generator" + var emailBodyBuffer = &strings.Builder{} + err := tmpl.Execute(emailBodyBuffer, match) + if err != nil { + log.Printf("Error executing template for %s: %v", match.From.Email, err) + continue + } + emailBody := emailBodyBuffer.String() + if err := sendEmail(match.From.Email, subject, emailBody); err != nil { log.Printf("Error sending email to %s: %v", match.From.Email, err) } diff --git a/templates/email_template.html b/templates/email_template.html new file mode 100644 index 0000000..6f555f2 --- /dev/null +++ b/templates/email_template.html @@ -0,0 +1,55 @@ + + + + + + +
+

Hello {{.From.Name}},

+

Your Secret Santa match is {{.To.Name}}!

+

Get ready to spread some holiday cheer!

+

Best regards,

+

Secret Santa Match Generator

+
+ +