Search code examples
emailgoiogmailimap

Gmail imap - charset error within body


currently trying to use package "github.com/emersion/go-imap" in order to parse some emails however I've discovered when trying to parse emails from a specific company - they send an automated email on signup, I get returned the error "unhandled charset "ascii" when attempting the following code:

mr, err := mail.CreateReader(r)

    if err != nil {
        log.Fatal(err)
    }

the value r is assigned with the code:

msg := <-messages
    r := msg.GetBody("BODY[]")

    if r == nil {
        log.Fatal("Server didn't returned message body")
    }

which attempts to get the body of the message in my inbox. I'm not sure what's causing this error to be thrown considering every other email I've tested it on works - it's just this specific email that gets sent that causes my code to fail. I've tried having it resend and tried new email addresses. Not sure what I can do


Solution

  • That message seems to be from message.go, and probably says that go-imap only supports ASCII when it's the default, not when it's explicitly specified. In other words, Content-Type: text/plain should work, Content-Type: text/plain; charset=ascii not. The easiest way forward is probably to fix go-imap and submit a pull request.

    FYI, many old charsets have more than one name. You don't need to add support for all the legacy charsets, but for those that are supported, go-imap really should know all the names. The names are kept by IANA and this script is the kind of thing you might want to use to parse the IANA list and generate an array of aliases.