// GetLogLines reads log lines from IMAP e-mail messages, sending them to the specified channel.
// This is adapted from go-imap examples.
// N.B. this currently calls log.Fatal on errors
GetLogLines reads log lines from IMAP e-mail messages, sending them to the specified channel.
This is adapted from go-imap examples.
N.B. this currently calls log.Fatal on errors
// GetLogLines reads log lines from IMAP e - mail messages sending them to the specified channel . // This is adapted from go - imap examples . // N . B . this currently calls log . Fatal on errors
func (conf *ImapConfig) GetLogLines(logLines chan string) {
	log.Println("Connecting to server...")
	c, err := client.DialTLS(conf.Server, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer c.Logout()
	log.Println("Connected")
	if err := c.Login(conf.Username, conf.Password); err != nil {
		log.Fatal(err)
	}
	log.Println("Logged in")
	_, err = c.Select("INBOX", false)
	if err != nil {
		log.Fatal(err)
	}
	criteria := imap.NewSearchCriteria()
	criteria.Header.Add("SUBJECT", r7800subject)
	uids, err := c.Search(criteria)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Search returned %v uids", len(uids))
	if len(uids) > 0 {
		seqSet := new(imap.SeqSet)
		seqSet.AddNum(uids...)
		section := &imap.BodySectionName{}
		items := []imap.FetchItem{section.FetchItem()}
		messages := make(chan *imap.Message, 10)
		done := make(chan error, 1)
		go func() {
			done <- c.Fetch(seqSet, items, messages)
		}()
		log.Println("Read messages")
		for msg := range messages {
			r := msg.GetBody(section)
			if r == nil {
				log.Fatal("Server didn't returned message body")
			}
			mr, err := mail.CreateReader(r)
			if err != nil {
				log.Fatal(err)
			}
			header := mr.Header
			if date, err := header.Date(); err == nil {
				log.Println("Date:", date)
			}
			if from, err := header.AddressList("From"); err == nil {
				log.Println("From:", from)
			}
			if to, err := header.AddressList("To"); err == nil {
				log.Println("To:", to)
			}
			if subject, err := header.Subject(); err == nil {
				log.Println("Subject:", subject)
			}
			for {
				p, err := mr.NextPart()
				if err == io.EOF {
					break
				} else if err != nil {
					log.Fatal(err)
				}
				switch h := p.Header.(type) {
				case mail.TextHeader:
					scanner := bufio.NewScanner(p.Body)
					for scanner.Scan() {
						logLines <- scanner.Text()
					}
					if err := scanner.Err(); err != nil {
						log.Fatal(err)
					}
				case mail.AttachmentHeader:
					filename, _ := h.Filename()
					log.Println("Got attachment: ", filename)
				}
			}
		}
	}
	close(logLines)
}
func ( conf * ImapConfig ) GetLogLines ( logLines chan string ) { log . Println ( " " ) c , err := client . DialTLS ( conf . Server , nil ) if err != nil { log . Fatal ( err ) } defer c . Logout ( ) log . Println ( " " ) if err := c . Login ( conf . Username , conf . Password ) ; err != nil { log . Fatal ( err ) } log . Println ( " " ) _ , err = c . Select ( " " , false ) if err != nil { log . Fatal ( err ) } criteria := imap . NewSearchCriteria ( ) criteria . Header . Add ( " " , r7800subject ) uids , err := c . Search ( criteria ) if err != nil { log . Fatal ( err ) } log . Printf ( " " , len ( uids ) ) if len ( uids ) > 0 { seqSet := new ( imap . SeqSet ) seqSet . AddNum ( uids ... ) section := & imap . BodySectionName { } items := [ ] imap . FetchItem { section . FetchItem ( ) } messages := make ( chan * imap . Message , 10 ) done := make ( chan error , 1 ) go func ( ) { done <- c . Fetch ( seqSet , items , messages ) } ( ) log . Println ( " " ) for msg := range messages { r := msg . GetBody ( section ) if r == nil { log . Fatal ( " " ) } mr , err := mail . CreateReader ( r ) if err != nil { log . Fatal ( err ) } header := mr . Header if date , err := header . Date ( ) ; err == nil { log . Println ( " " , date ) } if from , err := header . AddressList ( " " ) ; err == nil { log . Println ( " " , from ) } if to , err := header . AddressList ( " " ) ; err == nil { log . Println ( " " , to ) } if subject , err := header . Subject ( ) ; err == nil { log . Println ( " " , subject ) } for { p , err := mr . NextPart ( ) if err == io . EOF { break } else if err != nil { log . Fatal ( err ) } switch h := p . Header . ( type ) { case mail . TextHeader : scanner := bufio . NewScanner ( p . Body ) for scanner . Scan ( ) { logLines <- scanner . Text ( ) } if err := scanner . Err ( ) ; err != nil { log . Fatal ( err ) } case mail . AttachmentHeader : filename , _ := h . Filename ( ) log . Println ( " " , filename ) } } } } close ( logLines ) }
GetLogLines reads log lines from IMAP e-mail messages, sending them to the specified channel.
GetLogLines reads log lines from IMAP e - mail messages sending them to the specified channel .
// Get the whole message body // Process each message part // This is the message's text (can be plain-text or HTML) //b, _ := ioutil.ReadAll(p.Body) //log.Println("Got text: ", string(b)) // This is an attachment, which we do not expect! // N.B. the done channel should be read to detect errors, right?