func sendMail(mo *MailObject) { mcfg := g.GetConfig().Mail msg := gomail.NewMessage() // from msg.SetAddressHeader("From", mcfg.MailServerAccount, mo.FromUser) // receivers for i, to := range mo.Receivers { if i == 0 { msg.SetHeader("To", to) } else { msg.AddHeader("To", to) } } // subject msg.SetHeader("Subject", mo.Subject) // content msg.SetBody("text/plain", mo.Content) // statistics proc.MailSendCnt.Incr() m := gomail.NewMailer(mcfg.MailServerHost, mcfg.MailServerAccount, mcfg.MailServerPasswd, mcfg.MailServerPort) if err := m.Send(msg); err != nil { // statistics proc.MailSendErrCnt.Incr() log.Println(err, ", mailObject:", mo) } else { // statistics proc.MailSendOkCnt.Incr() } }
// try pushing one mail into sender queue, maybe failed func AddMail(r []string, subject string, content string, from ...string) bool { mcfg := g.GetConfig().Mail fromUserName := mcfg.FromUser if len(from) == 1 { fromUserName = from[0] } nm := NewMailObject(r, subject, content, fromUserName) return mailqueue.PushFront(nm) }
func startHttpServer() { if !g.GetConfig().Http.Enable { return } addr := g.GetConfig().Http.Listen if addr == "" { return } addRoutes() s := &http.Server{ Addr: addr, MaxHeaderBytes: 1 << 30, } log.Println("http.startHttpServer ok, listening", addr) log.Fatalln(s.ListenAndServe()) }
func Start() { sendsema = nsema.NewSemaphore(g.GetConfig().Mail.SendConcurrent) mailqueue = nlist.NewSafeListLimited(g.GetConfig().Mail.MaxQueueSize) go startSender() }