I've been implementing some PHP/IMAP-based email handling functionality lately, and have most everything working great, except for message body decoding (in some circumstances).
I think that, by now, I've half-memorized RFC 2822 (the 'Internet Message Format' document guidelines), read through email-handling code for half a dozen open source CMSes, and read a bajillion forum posts, blog posts, etc. dealing with handling email in PHP.
I've also forked and completely rewritten a class for PHP, Imap, and the class handles email respectably well—I have some helpful methods in there to detect autoresponders (for out of office, old addresses, etc.), decode base64 and 8bit messages, etc.
However, the one thing I simply can't get to work reliably (or, sometimes, at all) is when a message comes in with Content-Transfer-Encoding: 7bit
.
It seems that different email clients/services interpret 7BIT
to mean different things. I've gotten some emails that are supposedly 7BIT
that are actually Base64-encoded. I've gotten some that are actually quoted-printable-encoded. And some that are not encoded in any way whatsoever. And some that are HTML, but aren't indicated as being HTML, and they're also listed as 7BIT
...
Here are a few examples (snips) of message bodies received with 7Bit encodings:
1:
A random message=20
Sent from my iPhone
2:
PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwi
IHhtbG5zOm89InVybjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9m
3:
tangerine apricot pepper.=0A=C2=A0=0ALet me know if you have any availabili=
ty over the next month or so. =0A=C2=A0=0AThank you,=0ANames Withheld=0A908=
-319-5916=0A=C2=A0=0A=C2=A0=0A=C2=A0=0A=0A=0A______________________________=
__=0AFrom: Names Witheld =0ATo: Names Withheld=
These are all sent with '7Bit' encodings (well, at least according to PHP/imap_*
), but they're obviously in need of more decoding before I can pass them along as plaintext. Is there any way to reliably convert all messages with supposedly-7Bit encodings to plaintext?
mail
on the command line, the world would be a better place :) – Unscientific