How to get body from imap server in Go

I have successfully selected the list of email headers using the sample code from this URL: https://godoc.org/code.google.com/p/go-imap/go1/imap#example-Client . However, I still have not been able to receive the email. Can someone show some working example code that could receive the body of emails from the imap server in Golang?

+5
source share
2 answers

I figured out how to get body text now.

cmd, _ = c.UIDFetch(set, "RFC822.HEADER", "RFC822.TEXT") // 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 { header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"]) uid := imap.AsNumber((rsp.MessageInfo().Attrs["UID"])) body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"]) if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil { fmt.Println("|--", msg.Header.Get("Subject")) fmt.Println("UID: ", uid) fmt.Println(string(body)) } } cmd.Data = nil c.Data = nil } 
+3
source

The sample code you linked demonstrates using the IMAP FETCH command to retrieve the RFC822.HEADER message RFC822.HEADER for the message. The RFC contains a list of standard data items that you can extract from a message .

If you need all the formatted message in mime format (both the header and body), then the BODY request should do. You can get the headers and body of the message separately by requesting BODY[HEADER] and BODY[TEXT] respectively. Changing the sample program to use one of these data elements should get the data that you are after.

+1
source

Source: https://habr.com/ru/post/1210438/


All Articles