From 61f137d2cc8ae0199c99493701023b4d862a34ad Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 29 Aug 2013 22:37:05 +0200 Subject: Registering accounts is working --- mails.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 mails.go (limited to 'mails.go') diff --git a/mails.go b/mails.go new file mode 100644 index 0000000..5e5a6dc --- /dev/null +++ b/mails.go @@ -0,0 +1,52 @@ +package main + +import ( + "bytes" + "fmt" + "kch42.de/gostuff/mailremind/model" + "log" + "path" + "text/template" + "time" +) + +func loadMailTpl(tplroot, name string) *template.Template { + tpl, err := template.ParseFiles(path.Join(tplroot, name+".tpl")) + if err != nil { + log.Fatalf("Could not load mailtemplate %s: %s", name, err) + } + return tpl +} + +var mailActivationcode *template.Template + +func initMails() { + tplroot, err := conf.GetString("paths", "mailtpls") + if err != nil { + log.Fatalf("Could not get paths.mailtpls from config: %s", err) + } + + mailActivationcode = loadMailTpl(tplroot, "activationcode") +} + +type activationcodeData struct { + URL string +} + +func SendActivationcode(to, acCode string, uid model.DBID) bool { + buf := new(bytes.Buffer) + fmt.Fprintf(buf, "To: %s\n", to) + fmt.Fprintf(buf, "From: %s\n", MailFrom) + fmt.Fprintf(buf, "Subject: Activation code for your mailremind account\n") + fmt.Fprintf(buf, "Date: %s\n", time.Now().Format(time.RFC822)) + + fmt.Fprintln(buf, "") + + url := fmt.Sprintf("%s/activate/U=%s&Code=%s", baseurl, uid, acCode) + if err := mailActivationcode.Execute(buf, activationcodeData{url}); err != nil { + log.Printf("Error while executing mail template (activationcode): %s", err) + return false + } + + return Mail(to, MailFrom, buf.Bytes()) +} -- cgit v1.2.3-54-g00ecf