Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some example code #6

Closed
elgs opened this issue Jan 5, 2015 · 5 comments
Closed

Need some example code #6

elgs opened this issue Jan 5, 2015 · 5 comments

Comments

@elgs
Copy link

elgs commented Jan 5, 2015

Can you post some example code, especially how to fetch message body? Thanks.

@mxk mxk closed this as completed Jan 5, 2015
@elgs
Copy link
Author

elgs commented Jan 5, 2015

Thanks for the reply. I got the code run and fetched the subject from the code from your second link. However, the example in the second link does not answer the question how to get the body of the mail.

@mxk
Copy link
Owner

mxk commented Jan 13, 2015

You should have some understanding of the underlying protocol (RFC 3501) to use this package. The basic idea is that you would fetch "BODY[]" and then parse the response.

@elgs
Copy link
Author

elgs commented Jan 13, 2015

Thanks. I finally figured out how to get it by reading RFC 3501. Here is some sample code:

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
}

@ggitau
Copy link

ggitau commented Apr 21, 2016

Hi?
When I print the body as shown above it comes in parts that consist of headers and a base64 encoded string...how do I separate the two (headers and base64 encoded string) so that I can decode the contents?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants