Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add html template for emails #7

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>