Exemplo n.º 1
0
func CheckAndDownloadMailContent(client *pop3.Client, index int) (err error) {
	filePath := filepath.Join(destination, strconv.Itoa(index))
	if _, errFileStat := os.Stat(filePath); errFileStat != nil { // mail file not found

		log.Printf("Downloading Mail:%d", index)
		// THINK: check content integrity
		var mailContent string
		mailContent, err = client.Retr(index)
		if err != nil {
			return
		}

		//find newline at head of file
		var lastNewline = 0
		for i, b := range mailContent {
			if b != '\n' {
				lastNewline = i
				break
			}
		}
		rawMailContent := []byte(mailContent)[lastNewline:]

		// it's safe to ignore parse error
		fromAddress, _ := getFromAddress(rawMailContent)

		if fromAddress == mailFrom {
			log.Printf("Store mail content to file:%s", filePath)
			err = ioutil.WriteFile(filePath, rawMailContent, 0644)
			if err != nil {
				return
			}
		}

	}
	return
}