-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
73 lines (55 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"log"
"os"
"github.com/go-gomail/gomail"
)
func main() {
userCsvFile := flag.String("file", "testdata/users_001.csv", "Path to the user CSV file")
numberOfWinners := flag.Int("winners", 6, "Number of winners to select")
flag.Parse()
records, err := ParseCsvFile(*userCsvFile)
if err != nil {
log.Fatalf("Error parsing CSV file: %v", err)
}
if len(records) <= 1 {
log.Fatalf("Not enough records to select winners")
}
winners, err := SelectRandomEntries(records, *numberOfWinners)
if err != nil {
log.Fatalf("Error selecting winners: %v", err)
}
if err := SendEmail(winners); err != nil {
log.Fatalf("Error sending email to winners")
}
if err := SaveWinners("winners.csv", winners); err != nil {
log.Fatalf("Error saving winners to csv")
}
}
func SendEmail(winners [][]string) error {
email := os.Getenv("EMAIL")
password := os.Getenv("EMAIL_PASSWORD")
dialer := gomail.NewDialer("smtp.gmail.com", 587, email, password)
emails := extractEmails(winners)
message := gomail.NewMessage()
message.SetHeader("Subject", "You are a winner!")
message.SetHeader("From", email)
message.SetHeader("To", emails...)
htmlString, err := os.ReadFile("email.html")
if err != nil {
log.Fatalf("Error reading email template: %v", err)
}
message.SetBody("text/html", string(htmlString))
if err := dialer.DialAndSend(message); err != nil {
log.Fatalf("Error sending email: %v", err)
}
return nil
}
func extractEmails(winners [][]string) []string {
emails := make([]string, 0, len(winners))
for _, winner := range winners {
emails = append(emails, winner[0])
}
return emails
}