Пример #1
0
func (s *MailStore) Insert(m *M.Mail) error {
	m.Created = time.Now()
	m.Active = true
	m.Status = true

	if _, err := s.db.Insert(m); err != nil {
		return err
	}

	return nil
}
Пример #2
0
//Calls SendMail function and respondes to errors if they appear
func (n *NotifyMail) send(errChan ErrorChannel, mail M.Mail) {

	err := n.SendMail(mail)

	if err != nil {
		//If no internet connection sleep untill waitTime
		if strings.Contains(fmt.Sprintf("%s", err), "no such host") {
			time.Sleep(time.Duration(n.waitTime) * time.Second)
			errChan.In() <- errors.New("No internet connection mail")
		}

		//Try to resend mail n times
		if mail.Try >= n.retry {
			n.notifChan.In() <- fmt.Sprintf("Message sending failed after %d times %d", n.retry, mail.Id)
			mail.Status = false

			if err := n.store.Update(&mail); err != nil {
				n.errChan.In() <- err
			}

			return
		}

		//send to send mail channel again
		n.sendChan <- mail
		errChan.In() <- errors.New(fmt.Sprint("Resend: ", mail.Id))
		return
	}

	mail.Status = true
	if err := n.store.Update(&mail); err != nil {
		n.errChan.In() <- err
	}

	//If no errors send success notification
	n.notifChan.In() <- fmt.Sprintf("Mail ID: %d Succesfully sended at: %s", mail.Id)
}