I need to be able to use my gmail from a php script. But whatever I try, the message body comes out all crappy with characters like =3D and random equals signs. Sometimes it
You're assuming your mail message is always at position 2.2? There's absolutely no guarantees... depends on what message is sent you plan text (just one body), html/text (two), with attachment (three plus) in reply to another email (then the other email will be a body with it's own sub bodies).
The =3D
(quoted printable) and base64 data are because of message body encodings. Have a look at imap_fetchstructure which explains how many parts their are on a message (you can search through it to find text), and review the ->type
component of each body to learn about it's encoding (type=4 is quoted printable, type=3 is base64)