0

So I don't really want this question to be language specific, however I suspect Go (my language choice) is playing a part here. I'm trying to find a string within the body of a raw email. To do so, I am getting the encoding, and the marjority of cases are quoted-printable. Ok so thats fine, I am encoding my search query quoted printable and then doing a search for it. That works. However. In one specific case the raw email I see in gmail looks fine, however when I retrieve the raw email from the gmail API the although the encoding and everything is identical, its encoding the " as =22 Research shows me thats because the charset is utf-8. I haven't quite got my head around whether thats encoded utf-8 then quoted-printable or the other way around, but thats not quite the question either.... If I look at the email where the " is =22 I see the char set is utf-8 and when I look at another where its not encoded, the charset is UTF-8 (notice the case). I can't believe that the case here is whats causing this to happen, but it doesn't seem a robust enough way to work out if =22 is actually =22 or is a " encoded utf-8.

My original thought was to always decode the quoted-printable and then re-encode it before doing the search but I don't think this is going to be a robust approach going forward and thought others might have a better suggestion?

Conclusion, I'm trying to find a string in a raw email but the encoding is causing me problems getting my search string to match the encoding of the body

amlwwalker
  • 3,161
  • 4
  • 26
  • 47

1 Answers1

0

The =22-type encoding actually has nothing to do with the charset (whether that is utf-8 lowercase or UTF-8 uppercase or any other charset).

It is the Content-Transfer-Encoding: quoted-printable encoding.

The quoted-printable encoding is just a way of hex-encoding octets, typically limited to octets that fall outside of the printable ascii range. It seems odd that the DQUOTE character would be encoded in this way, but it's perfectly legal to do so.

If you want to search for strings in the body of the message, you'll need to first decode the body of the message. Otherwise you will not be successful.

I would recommend reading rfc2045 at a minimum.

You may also need to end up reading rfc2047 if you end up wanting to search headers at some point, but that gets... tricky due to various bugs that sending clients have.

Now that I've been "triggered" into a rant about MIME, let me explain why decoding headers is so hard to get right. I'm sure just about every developer who has ever worked on an email client could tell you this, but I guess I'm going to be the one to do it.

Here's just a short list of the problems every developer faces when they go to implement a decoder for headers which have been (theoretically) encoded according to the rfc2047 specification:

  1. First off, there are technically two variations of header encoding formats specified by rfc2047 - one for phrases and one for unstructured text fields. They are very similar but you can't use the same rules for tokenizing them. I mention this because it seems that most MIME parsers miss this very subtle distinction and so, as you might imagine, do most MIME generators. Hell, most MIME generators probably never even heard of specifications to begin with it seems.

    This brings us to:

  2. There are so many variations of how MIME headers fail to be tokenizable according to the rules of rfc2822 and rfc2047. You'll encounter fun stuff such as:

    a. encoded-word tokens illegally being embedded in other word tokens

    b. encoded-word tokens containing illegal characters in them (such as spaces, line breaks, and more) effectively making it so that a tokenizer can no longer, well, tokenize them (at least not easily)

    c. multi-byte character sequences being split between multiple encoded-word tokens which means that it's not possible to decode said encoded-word tokens individually

    d. the payloads of encoded-word tokens being split up into multiple encoded-word tokens, often splitting in a location which makes it impossible to decode the payload in isolation

    You can see some examples here.

  3. Something that many developers seem to miss is the fact that each encoded-word token is allowed to be in different character encodings (you might have one token in UTF-8, another in ISO-8859-1 and yet another in koi8-r). Normally, this would be no big deal because you'd just decode each payload, then convert from the specified charset into UTF-8 via iconv() or something. However, due to the fun brokenness that I mentioned above in (2c) and (2d), this becomes more complicated.

    If that isn't enough to make you want to throw your hands up in the air and mutter some profanities, there's more...

  4. Undeclared 8bit text in headers. Yep. Some mailers just didn't get the memo that they are supposed to encode non-ASCII text. So now you get to have the fun experience of mixing and matching undeclared 8bit text of God-only-knows what charset along with the content of (probably broken) encoded-words.

If you want to see how to deal with these issues, you can take a look at how I did it using C in my GMime library, here: https://github.com/jstedfast/gmime/blob/master/gmime/gmime-utils.c#L1894 (in case line offsets change in the future, look for _g_mime_utils_header_decode_text() and the various internal methods it uses in that source file - I have written comments explaining how it deals with the above issues).

Or you can see how I did it using C# in my MimeKit library, here: https://github.com/jstedfast/MimeKit/blob/master/MimeKit/Utils/Rfc2047.cs

For more infomation about why & how dealing with email is hard, check out Joshua Cramner's blog series: http://quetzalcoatal.blogspot.com/search/label/email-hard

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • thanks alot @jstedfast yeah my experience is email is built on quick sand. So I don't think I need to look too deeply into the headers at this stage, as I know for sure that I have the body content available (just not encoded in anyway) from the gmail API. I also have the `quoted-printable` header from the gmail API. What I'm wanting to do is find the start of the body in the raw email, which as you say, means encoding the body I have to the same type as in the raw and doing a search..... TBC – amlwwalker Apr 27 '20 at 13:58
  • However I can't work out why the quoted-printable lib I'm using doesn't encode the " and usually clients don't either, but in some rare cases it has been encoded. As you say "it seems odd" - the problem is I cant detect whether it will have been or not... There isn't a sure fire way to know I imagine? I'm going to look at your code now – amlwwalker Apr 27 '20 at 13:59
  • What I suggested was *decoding* the message body, not *encoding* the search string. There's no way to encode the search string in a way that would allow any sort of sane string matching algorithm to work. – jstedfast Apr 27 '20 at 14:42
  • the issue there though, is i would just know where the string was in the decoded version - i need to know in the encoded version and I can't think of a nifty way to "map the two". Perhaps I could look for the headers that I have before the body and use an index from there, but is that safe/robust? – amlwwalker Apr 28 '20 at 15:08
  • What you are trying to do is impossible. – jstedfast Apr 28 '20 at 15:45