예제 #1
0
파일: fetch.go 프로젝트: yanue/go-webmail
func GetMailDetail(seq_id string) (data map[string]string, err error) {
	// Fetch the headers of the 10 most recent messages
	set, _ := imap.NewSeqSet("")
	set.Add(seq_id)
	cmd, _ = c.Fetch(set, "RFC822.HEADER", "UID", "BODY[TEXT]", "FLAGS", "ENVELOPE")

	// Process responses while the command is running
	fmt.Println("\nMost recent messages:")

	for cmd.InProgress() {
		// Wait for the next response (no timeout)
		c.Recv(-1)
		// Process command data
		for _, rsp := range cmd.Data {
			fmt.Println(rsp.MessageInfo().Attrs)
			header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])

			// header link: https://tools.ietf.org/html/rfc4021
			if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
				data = fetchHeader(msg)
			}

			fmt.Println("ENVELOPE", rsp.MessageInfo().Attrs["ENVELOPE"])
			fmt.Println("UID", rsp.MessageInfo().Attrs["UID"])
			fmt.Println("FLAGS", rsp.MessageInfo().Attrs["FLAGS"])
			fmt.Println("FULL", rsp.MessageInfo().Attrs["FULL"])

			data["body"] = imap.AsString(rsp.MessageInfo().Attrs["BODY[TEXT]"])
			data["flags"] = imap.AsString(rsp.MessageInfo().Attrs["FLAGS"])
			data["ENVELOPE"] = imap.AsString(rsp.MessageInfo().Attrs["ENVELOPE"])

			//if msg, _ := mail.ReadMessage(bytes.NewReader(body)); msg != nil {
			//	data = fetchBody(msg)
			//	break
			//}

			fmt.Println("body", data["body"])

			// 第一次循环结束
			break
		}

		cmd.Data = nil

		// Process unilateral server data
		for _, rsp = range c.Data {
			fmt.Println("Server data:", rsp)
		}
		c.Data = nil
	}
	fmt.Println("res", data)
	// Check command completion status
	if rsp, err := cmd.Result(imap.OK); err != nil {
		if err == imap.ErrAborted {
			fmt.Println("Fetch command aborted")
		} else {
			fmt.Println("Fetch error:", rsp.Info)
		}
	}

	return data, nil
}
예제 #2
0
파일: fetch.go 프로젝트: yanue/go-webmail
func GetMailList() (list []map[string]string, err error) {
	// Fetch the headers of the 10 most recent messages
	set, _ := imap.NewSeqSet("")
	fmt.Println("c.Mailbox.Messages", c.Mailbox.Messages)
	if c.Mailbox.Messages >= 3 {
		set.AddRange(c.Mailbox.Messages, c.Mailbox.Messages)
	} else {
		set.Add("1:*")
	}
	cmd, _ = c.Fetch(set, "RFC822.HEADER", "FLAGS")

	// Process responses while the command is running
	fmt.Println("\nMost recent messages:")
	list = make([]map[string]string, len(cmd.Data))

	for cmd.InProgress() {
		// Wait for the next response (no timeout)
		c.Recv(-1)

		// Process command data
		for i, rsp := range cmd.Data {
			fmt.Println("i", i)
			//fmt.Println("rsp.MessageInfo().Attrs", rsp.MessageInfo().Attrs)
			header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
			a := enmime.DecodeToUTF8Base64Header(imap.AsString(rsp.MessageInfo().Attrs["RFC822.HEADER"]))

			fmt.Println("a:", a)
			r := bytes.NewReader(header)

			if msg, _ := mail.ReadMessage(r); msg != nil {
				// header link: https://tools.ietf.org/html/rfc4021
				fmt.Println("msg", msg)
				fmt.Println()
				fmt.Println()

				item := map[string]string{}

				item["Mime-Version"] = msg.Header.Get("Mime-Version")
				item["X-Original-To"] = msg.Header.Get("X-Original-To")
				item["Return-Path"] = msg.Header.Get("Return-Path")
				item["To"] = enmime.DecodeHeader(msg.Header.Get("To"))
				item["FROM"] = enmime.DecodeHeader(msg.Header.Get("FROM"))
				item["Reply-To"] = enmime.DecodeHeader(msg.Header.Get("Reply-To"))
				item["Subject"] = enmime.DecodeHeader(msg.Header.Get("Subject"))
				item["Received"] = msg.Header.Get("Received")
				item["Date"] = msg.Header.Get("Date")
				item["X-Priority"] = msg.Header.Get("X-Priority")
				item["Message-Id"] = msg.Header.Get("Message-Id")
				item["Content-Type"] = msg.Header.Get("Content-Type")
				item["Delivered-To"] = msg.Header.Get("Delivered-To")
				item["X-Mailer"] = msg.Header.Get("X-Mailer")
				list = append(list, item)

				fmt.Println("|--Mime-Version", msg.Header.Get("Mime-Version"))
				fmt.Println("|--X-Original-To", msg.Header.Get("X-Original-To"))
				fmt.Println("|--Return-Path", msg.Header.Get("Return-Path"))
				fmt.Println("|--To", msg.Header.Get("To"))
				fmt.Println("|--Reply-To", msg.Header.Get("Reply-To"))
				fmt.Println("|--Subject", msg.Header.Get("Subject"))
				fmt.Println("|--Received", msg.Header.Get("Received"))
				fmt.Println("|--Date", msg.Header.Get("Date"))
				fmt.Println("|--X-Priority", msg.Header.Get("X-Priority"))
				fmt.Println("|--Message-Id", msg.Header.Get("Message-Id"))
				fmt.Println("|--Content-Type", msg.Header.Get("Content-Type"))
				fmt.Println("|--Delivered-To", msg.Header.Get("Delivered-To"))
				fmt.Println("|--X-Mailer", msg.Header.Get("X-Mailer"))

			}
			//flags := imap.AsBytes(rsp.MessageInfo().Attrs["FLAGS"])
			//if flags, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
			//	fmt.Println("|--", msg.Header.Get("Subject"))
			//}
		}

		cmd.Data = nil

		// Process unilateral server data
		for _, rsp = range c.Data {
			fmt.Println("Server data:", rsp)
		}
		c.Data = nil
	}

	// Check command completion status
	if rsp, err := cmd.Result(imap.OK); err != nil {
		if err == imap.ErrAborted {
			fmt.Println("Fetch command aborted")
		} else {
			fmt.Println("Fetch error:", rsp.Info)
		}
	}
	return list, nil
}