diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-09-01 12:37:57 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-09-01 12:37:57 +0200 |
commit | ef002c79acbade33272cf9b524ce561d990df2bf (patch) | |
tree | 80874f4671e5f6cf2c9e4a1c98fdacd7b04cc2f2 | |
parent | e48fbb7d9d4c659890b8f49210bdbcd4b8fbacb2 (diff) | |
download | mailremind-ef002c79acbade33272cf9b524ce561d990df2bf.tar.gz mailremind-ef002c79acbade33272cf9b524ce561d990df2bf.tar.bz2 mailremind-ef002c79acbade33272cf9b524ce561d990df2bf.zip |
Mails are now sent.
(Also fixed date format for mails)
-rw-r--r-- | checkmails.go | 52 | ||||
-rw-r--r-- | mails.go | 4 | ||||
-rw-r--r-- | main.go | 2 |
3 files changed, 56 insertions, 2 deletions
diff --git a/checkmails.go b/checkmails.go new file mode 100644 index 0000000..c30507d --- /dev/null +++ b/checkmails.go @@ -0,0 +1,52 @@ +package main + +import ( + "bytes" + "fmt" + "kch42.de/gostuff/mailremind/model" + "log" + "time" +) + +const checkInterval = 30 // TODO: Make this configurable + +func checkmails() { + ticker := time.NewTicker(checkInterval * time.Second) + + for { + t := <-ticker.C + + jobs := dbcon.JobsBefore(t) + + for _, job := range jobs { + if sendjob(job, t) { + next := job.Chronos().NextAfter(t) + if next.IsZero() { + if err := job.Delete(); err != nil { + log.Printf("Failed deleting job %s after job was done: %s", job.ID(), err) + } + } else { + if err := job.SetNext(next); err != nil { + log.Printf("Filed setting next for job %s: %s", job.ID(), err) + } + } + } + } + } +} + +func sendjob(job model.Job, t time.Time) bool { + user := job.User() + buf := new(bytes.Buffer) + + fmt.Fprintf(buf, "From: %s\n", MailFrom) + fmt.Fprintf(buf, "To: %s\n", user.Email()) + fmt.Fprintf(buf, "Subject: %s\n", job.Subject()) + fmt.Fprintf(buf, "Date: %s\n", t.In(user.Location()).Format(time.RFC1123Z)) + + fmt.Fprintln(buf, "") + + buf.Write(job.Content()) + + return Mail(user.Email(), MailFrom, buf.Bytes()) +} @@ -42,7 +42,7 @@ func SendActivationcode(to, acCode string, uid model.DBID) bool { 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.Fprintf(buf, "Date: %s\n", time.Now().Format(time.RFC1123Z)) fmt.Fprintln(buf, "") @@ -60,7 +60,7 @@ func SendPwresetLink(to, code string, uid model.DBID) bool { fmt.Fprintf(buf, "To: %s\n", to) fmt.Fprintf(buf, "From: %s\n", MailFrom) fmt.Fprintf(buf, "Subject: Password reset request for your mailremind account\n") - fmt.Fprintf(buf, "Date: %s\n", time.Now().Format(time.RFC822)) + fmt.Fprintf(buf, "Date: %s\n", time.Now().Format(time.RFC1123Z)) fmt.Fprintln(buf, "") @@ -78,6 +78,8 @@ func main() { log.Fatalf("Could not get net.laddr config: %s", err) } + go checkmails() + router := mux.NewRouter() router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticpath)))) router.HandleFunc("/register", mkHttpHandler(register, tplRegister)) |