예제 #1
0
파일: mail.go 프로젝트: baiyuxiong/gomail
func (m *MailClient) StartSendMail() {
	log.Println("startSendMail....")
	for {
		log.Println("conn to redis... ")

		m.insureMailConn()
		m.insureLogConn()

		log.Println("startSendMail - BRPOP... ")
		data, err := m.MailConn.Do("BRPOP", config.Config().JobKey, 0)

		if err != nil {
			log.Println("startSendMail - redis BRPOP error : ", err.Error())
			m.closeMailConn()
			m.closeLogConn()
			time.Sleep(time.Second * 10)
		} else {
			log.Println("startSendMail - get job, start send...")

			log.Printf("data is %s", data)
			mails := data.([]interface{})
			if len(mails) == 2 {
				go m.Send(string(mails[1].([]byte)))
			} else {
				log.Printf("%s", mails)
			}
		}
	}
}
예제 #2
0
파일: mail.go 프로젝트: baiyuxiong/gomail
func (m *MailClient) newRedisConn() (redis.Conn, error) {
	c, err := redis.Dial(config.Config().Redis.Network, config.Config().Redis.Address)
	if err != nil {
		log.Println("initRedisConn mc - error : ", err.Error())
		return nil, err
	}

	if len(config.Config().Redis.Password) > 0 {
		if _, err := c.Do("AUTH", config.Config().Redis.Password); err != nil {
			c.Close()
			log.Println("initRedisConn mc - Do AUTH error : ", err.Error())
			return nil, err
		}
	}
	return c, nil
}
예제 #3
0
파일: mail.go 프로젝트: baiyuxiong/gomail
func (m *MailClient) PutLog(data model.EmailLog) {
	bs, err := json.Marshal(data)
	if err != nil {
		log.Println("PutLog - err : ", err.Error())
		return
	}
	m.LogConn.Do("LPUSH", config.Config().LogKey, string(bs))
}
예제 #4
0
파일: mail.go 프로젝트: baiyuxiong/gomail
func (m *MailClient) Send(data string) {
	for _, s := range senders {
		if s.Name == config.Config().Sender {
			l := s.Run(data)
			m.PutLog(l)
			break
		}
	}
}
예제 #5
0
파일: mail.go 프로젝트: baiyuxiong/gomail
func (m *MailClient) GetMailLog(start int, stop int) (reply interface{}, err error) {
	log.Println("GetMailLog...")

	m.insureLogConn()

	reply, err = m.LogConn.Do("LRANGE", config.Config().LogKey, start, stop)
	if err != nil {
		log.Println("GetMailLog - reply ", reply, ",error : ", err.Error())
	}

	return
}
예제 #6
0
파일: http.go 프로젝트: baiyuxiong/gomail
func startHttpServer() {

	addr := config.Config().HttpAddress
	if addr == "" {
		return
	}

	configRoutes()

	s := &http.Server{
		Addr:           addr,
		MaxHeaderBytes: 1 << 30,
	}

	log.Println("http.startHttpServer ok, listening", addr)
	log.Fatalln(s.ListenAndServe())
}
예제 #7
0
파일: smtp.go 프로젝트: baiyuxiong/gomail
func SendToMail(to, subject, body string) error {
	hp := strings.Split(config.Config().Smtp.Address, ":")
	auth := smtp.PlainAuth("", config.Config().Smtp.Username, config.Config().Smtp.Password, hp[0])

	send_to := strings.Split(to, ";")
	message := ""
	message += fmt.Sprintf("%s: %s\r\n", "Subject", subject)
	message += fmt.Sprintf("%s: %s\r\n", "Content-Transfer-Encoding", "base64")
	message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

	err := smtp.SendMail(config.Config().Smtp.Address, auth, config.Config().Email.From, send_to, []byte(message))
	if err != nil {
		log.Println("SendToMail", config.Config().Smtp.Address, "with username", config.Config().Smtp.Username, "err : ", err.Error())
	}
	return err
}
예제 #8
0
파일: routes.go 프로젝트: baiyuxiong/gomail
func configRoutes() {
	// GET
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

		if nil != checkLoggedIn(w, r) {
			return
		}

		queries, err := url.ParseQuery(r.URL.RawQuery)

		var perPage int = 20
		var start int = 0
		if err == nil && len(queries["start"]) > 0 {
			start, _ = strconv.Atoi(queries["start"][0])
		}

		var stop int = start + perPage - 1
		if err == nil && len(queries["stop"]) > 0 {
			stop, _ = strconv.Atoi(queries["stop"][0])
		}

		var currentPage int = start/perPage + 1

		var prePageStart int = 0
		var prePageStop int = 19
		var nextPageStart int = 20
		var nextPageStop int = 39

		var showPrePage = false
		if currentPage > 1 {
			showPrePage = true
			prePageStart = (currentPage - 2) * perPage
			prePageStop = prePageStart + perPage - 1
		}
		nextPageStart = currentPage * perPage
		nextPageStop = nextPageStart + perPage - 1

		logs, err := mail.GetMailLog(start, stop)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		var mailLogs []model.EmailLog

		for _, l := range logs.([]interface{}) {
			var m model.EmailLog
			err := json.Unmarshal(l.([]byte), &m)
			if err != nil {
				log.Println("HandleFunc: / Unmarshal error - ", err.Error())
			} else {
				mailLogs = append(mailLogs, m)
			}
		}

		data := map[string]interface{}{
			"mailLogs":      mailLogs,
			"start":         start,
			"stop":          stop,
			"currentPage":   currentPage,
			"showPrePage":   showPrePage,
			"prePageStart":  prePageStart,
			"prePageStop":   prePageStop,
			"nextPageStart": nextPageStart,
			"nextPageStop":  nextPageStop,
		}

		Render(w, "home/index.html", data)
	})

	// GET
	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {

		data := status.Check()

		Render(w, "status/index.html", data)
	})

	// GET
	http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		Render(w, "home/login.html", nil)
	})

	http.HandleFunc("/doLogin", func(w http.ResponseWriter, r *http.Request) {
		if !lastErrorTime.IsZero() && (time.Now().Sub(lastErrorTime) < time.Minute) {
			w.Write([]byte("Pls try after " + time.Now().Add(time.Minute).String()))
			return
		}

		r.ParseForm()
		if len(r.Form["password"]) < 1 {
			w.Write([]byte("Need password."))
			return
		}
		if len(r.Form["username"]) < 1 {
			w.Write([]byte("Need username."))
			return
		}

		password := r.Form["password"][0]
		username := r.Form["username"][0]

		if password != config.Config().Password || username != config.Config().Username {
			lastErrorTime = time.Now()
			w.Write([]byte("I love you, Pls don't hurt me."))
			return
		} else {
			storeUsername(w, r, username)
			http.Redirect(w, r, "/", http.StatusFound)
			return
		}
	})

	http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
		storeUsername(w, r, "")
		http.Redirect(w, r, "/login", http.StatusFound)
		return
	})

	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok\n"))
	})

	http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
		if nil != checkLoggedIn(w, r) {
			return
		}
		w.Write([]byte(fmt.Sprintf("%s\n", config.VERSION)))
	})

	http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
		if nil != checkLoggedIn(w, r) {
			return
		}
		w.Write([]byte(fmt.Sprintf("%s\n", file.SelfDir())))
	})

	http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) {
		if nil != checkLoggedIn(w, r) {
			return
		}
		RenderOKJson(w, config.Config())
	})

	http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) {
		if nil != checkLoggedIn(w, r) {
			return
		}
		config.Parse(config.ConfigFile)
		RenderOKJson(w, config.Config())
	})
	http.HandleFunc("/public/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, r.URL.Path[1:])
	})
}