How to decode mail body in Go

net/mail golang
golang mail addressparser
parsemail golang
golang contact
golang .eml parser
golang send email gmail
quoted printable decode golang
mail parseaddresslist

I'm working on an email client and part of this I need to decode the email body. I'm using the IMAP package to fetch the messages but there is no "decode" method. I also checked the net/mail package with no luck either. Only the headers seem to have a parser. Is there any lib that I can use?

Example of parsing a mime email message with Go (original: https , NewReader(msg.Body, params["boundary"]). for {. p, err := mr.NextPart(). if err == io.EOF {. return. } if err != nil {. log.Fatal(err). } // decode any Q-encoded values. Let's start with the trickiest one: the body of a Go's http.Request is an io.Reader, which doesn't fit well if you have a struct - you need to write the struct first and then copy that to a reader. Decoding JSON on the server. Let's say you're expecting the client to send JSON data to the server.

I've had some luck using github.com/jhillyerd/enmime to break out both headers and body parts. Given an io.Reader r:

// Parse message body
env, _ := enmime.ReadEnvelope(r)
// Headers can be retrieved via Envelope.GetHeader(name).
fmt.Printf("From: %v\n", env.GetHeader("From"))
// Address-type headers can be parsed into a list of decoded mail.Address structs.
alist, _ := env.AddressList("To")
for _, addr := range alist {
  fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address)
}
fmt.Printf("Subject: %v\n", env.GetHeader("Subject"))

// The plain text body is available as mime.Text.
fmt.Printf("Text Body: %v chars\n", len(env.Text))

// The HTML body is stored in mime.HTML.
fmt.Printf("HTML Body: %v chars\n", len(env.HTML))

// mime.Inlines is a slice of inlined attacments.
fmt.Printf("Inlines: %v\n", len(env.Inlines))

// mime.Attachments contains the non-inline attachments.
fmt.Printf("Attachments: %v\n", len(env.Attachments))

Fetching messages · emersion/go-imap Wiki · GitHub, To fetch the whole message body, use BODY[] . You can also use the go-​message library to parse the body. package main import ( "io  Decoding emails in Python e.g. for GMail and imapclient lib - gist:5389146. """ Decode email body. Detect character set if the header is not set.

You can check if a project like artagnon/ibex, which uses the go-imap package, does provide that feature. See for instance its artagnon/ibex/imap.go#L288-L301 test.

var body []byte
cmd, err = imap.Wait(c.UIDFetch(set, "BODY.PEEK[]"))
if (err != nil) {
    fmt.Println(err.Error())
    return nil
}
body = imap.AsBytes(cmd.Data[0].MessageInfo().Attrs["BODY[]"])
cmd.Data = nil

bytestring, err := json.Marshal(MessageDetail{string(body)})
if (err != nil) {
    fmt.Println(err.Error())
    return nil
}
return bytestring

mail, If there is an error, // respond to the client with the error message and a 400 status code. err := json.NewDecoder(r.Body).Decode(&p) if err != nil  Decoding Encoded Email When you look at the source code of an email, it sometimes contains blocks of what appears to be random alphanumeric characters. It could be an encoded image in the email, an encoded attachment, or the entire email content may be encoded.

How to Parse a JSON Request Body in Go, I am working on an email client and a part of it has to be decoded by email body. I am using the package to fetch the messages but there is no "decode" method. Rather than using message.body.decoded or message.decode_body (which just calls body.decoded), call message.decoded. That checks whether it's a text message [1] and calls message.decode_body_as_text , which decodes the transfer-encoding and sets the Ruby string encoding according to the message's charset [2].

email - How to decode mail body in Go -, This guide walks thru how to decode these messages and make them usable by your programs. We'll go thru how to do that in this guide. MIME supports features like embedded attachments, multiple email body types (plain text and  When 'Create Issue From Email Service' is used, sometimes JIRA may fail to decode the mail subject or mail body when mail is sent from some mail clients, such as Swedish Outlook Express or Lotus Notes. Cause. The encoding pattern of email sent from those mail clients does not follow the standard.

Parse Raw Email (MIME) Guide, We've already seen an example of a Base64-encoded message earlier in this go so far as to automatically drop messages with a Base64-encoded body. it's necessary to spend the processor cycles decoding the message body just to be  Mail_mimeDecode::decode() – perform decoding. Synopsis. If the parameter decode_bodies is specified as TRUE then the body will be decoded.

Comments
  • and if it's encoded using a different thing / charset what should I do ? . See stackoverflow.com/q/24902453/3780579
  • The only other encoding that is likely is 7bit. In this case you do not have to do anything.
  • I finally abandoned Go for this project. ibex lib doesn't seem to help. stackoverflow.com/a/24903176/3780579
  • @hey ok, I have upvoted your other question and answer there.