Skip to content

Commit

Permalink
Add html template for emails (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoutsilis authored Dec 31, 2023
1 parent 17f5a26 commit b7ca680
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 + ",<br><br>You are the secret Santa for " + match.To.Name + "!<br><br>Best regards,<br>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)
}
Expand Down
55 changes: 55 additions & 0 deletions templates/email_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}

.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
padding: 30px;
border-radius: 10px;
text-align: center;
}

h1 {
font-size: 28px;
margin-bottom: 20px;
}

p {
font-size: 18px;
line-height: 1;
margin-bottom: 10px;
}

.emphasis {
font-weight: bold;
color: #e44d26; /* A shade of red for emphasis */
}

.signature {
font-style: italic;
margin-top: 20px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello {{.From.Name}},</h1>
<p>Your Secret Santa match is <span class="emphasis">{{.To.Name}}</span>!</p>
<p>Get ready to spread some holiday cheer!</p>
<p>Best regards,</p>
<p class="signature">Secret Santa Match Generator</p>
</div>
</body>
</html>

0 comments on commit b7ca680

Please sign in to comment.