package jobhandler import ( "bytes" "github.com/jordan-wright/email" "html/template" "log" "menuserver/dbconnection" "net/smtp" "os" "strings" "time" ) var ( from = os.Getenv("EMAIL_FROM") login = os.Getenv("EMAIL_LOGIN") recipients = strings.Split(os.Getenv("EMAIL_RECIPIENTS"), ",") // recipients = []string{"Martin "} hostname = os.Getenv("EMAIL_HOSTNAME") ) func DoJob(r *dbconnection.SQLiteRepository) { var data PugData var store []MenuEntry var menuItems []dbconnection.Menu srcURL := os.Getenv("SITE_URL") var types = [7]int{0, 0, 0, 0, 0, 0, 0} limit := 2 log.Printf("Doing Job....") now := time.Now() daystart := (now.Unix() / 86400) * 86400 data.Ts = now.Format("Monday, January 2, 2006") items, err := r.GetRandom(now.Unix()) if err != nil { log.Fatal(err) } for _, item := range items { if types[item.Meat] < limit && len(store) < 5 { entry := MenuEntry{ Url: srcURL + "/view/" + item.Short, Meat: item.Meat, Name: item.Name, } store = append(store, entry) types[item.Meat] = types[item.Meat] + 1 menuItems = append(menuItems, item) } } data.Menu = store soupItem, err := r.GetRandomSoup(now.Unix()) if err != nil { log.Fatal(err) } soupEntry := MenuEntry{ Url: srcURL + "/view/" + soupItem.Short, Meat: soupItem.Meat, Name: soupItem.Name, } data.Soup = soupEntry menuItems = append(menuItems, soupItem) outputMsg := generateHTML(data) subject := "Suggestions 🍽️ - " + data.Ts sendSMTP(outputMsg, subject) updateTimestamps(r, menuItems, daystart) } func generateHTML(data PugData) string { var buf bytes.Buffer t := template.New("mail") t, err := t.Parse(` Suggestions

Suggestions for {{ .Ts }}

Mains

    {{ range .Menu }}
  1. {{ .Name }}
  2. {{ end }}

Soup

V3.00 -- 😊 -- GO! `) if err != nil { log.Fatal(err) } err = t.Execute(&buf, data) if err != nil { log.Fatal(err) } // log.Printf("%+v\n") return buf.String() } func getEmojis() []string { return []string{"🍽️", "🍴", "🍳", "👨🏻‍🍳", "👩🏻‍🍳", "🥑", "🥕", "🫑", "🍔"} } func updateTimestamps(r *dbconnection.SQLiteRepository, menuItems []dbconnection.Menu, timestamp int64) { err := r.UpdateTimeStamps(menuItems, timestamp) if err != nil { log.Fatal(err) } } func sendSMTP(inmsg string, subject string) { log.Println("Sending Email") e := email.NewEmail() auth := smtp.PlainAuth("", login, os.Getenv("EMAIL_PASSWORD"), hostname) e.From = from e.To = []string{from} e.Bcc = recipients e.Subject = subject e.Text = []byte(inmsg) e.HTML = []byte(inmsg) err := e.Send(hostname+":587", auth) if err != nil { log.Fatal(err) } else { log.Println("Mail sent!") } }