package main import ( "bytes" "fmt" "github.com/Masterminds/sprig" "os" "text/template" "gopkg.in/gomail.v2" ) type templateBody string func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer, err error) { t, err := template.New(templateName).Funcs( sprig.GenericFuncMap()).ParseFiles(fmt.Sprintf("templates/%s", templateName)) if err != nil { return } mailText = bytes.NewBufferString("") t.Execute(mailText, context) return } func CreateMotion(decision *Decision, voter *Voter) (err error) { decision.ProponentId = voter.Id err = decision.Create() if err != nil { logger.Println("Error saving motion:", err) return } type mailContext struct { Decision Name string VoteURL string UnvotedURL string } voteURL := fmt.Sprintf("%s/vote", config.BaseURL) unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL) context := mailContext{*decision, voter.Name, voteURL, unvotedURL} mailText, err := buildMail("create_motion_mail.txt", context) if err != nil { logger.Println("Error", err) return } m := gomail.NewMessage() m.SetHeader("From", config.NoticeSenderAddress) m.SetHeader("To", config.BoardMailAddress) m.SetHeader("Subject", fmt.Sprintf("%s - %s", decision.Tag, decision.Title)) m.SetHeader("Message-ID", fmt.Sprintf("<%s>", decision.Tag)) m.SetBody("text/plain", mailText.String()) d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "") if err := d.DialAndSend(m); err != nil { logger.Println("Error sending mail", err) } return } func UpdateMotion(decision *Decision, voter *Voter) (err error) { err = decision.Update() if err != nil { logger.Println("Error updating motion:", err) return } type mailContext struct { Decision Name string VoteURL string UnvotedURL string } voteURL := fmt.Sprintf("%s/vote", config.BaseURL) unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL) context := mailContext{*decision, voter.Name, voteURL, unvotedURL} mailText, err := buildMail("update_motion_mail.txt", context) if err != nil { logger.Println("Error", err) return } m := gomail.NewMessage() m.SetHeader("From", config.NoticeSenderAddress) m.SetHeader("To", config.BoardMailAddress) m.SetHeader("Subject", fmt.Sprintf("%s - %s", decision.Tag, decision.Title)) m.SetHeader("References", fmt.Sprintf("<%s>", decision.Tag)) m.SetBody("text/plain", mailText.String()) d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "") if err := d.DialAndSend(m); err != nil { logger.Println("Error sending mail", err) } return } func WithdrawMotion(decision *Decision, voter *Voter) (err error) { // load template, fill name, tag, title, content type mailContext struct { *Decision Name string Sender string Recipient string } context := mailContext{decision, voter.Name, config.NoticeSenderAddress, config.BoardMailAddress} // fill withdraw_mail.txt t, err := template.New("withdraw_mail.txt").Funcs( sprig.GenericFuncMap()).ParseFiles("templates/withdraw_mail.txt") if err != nil { logger.Fatal(err) } // TODO: send mail t.Execute(os.Stdout, context) // TODO: implement call decision.Close() return }