phpencryptionimapgmail-apimbox

imap_fetchbody vs Gmail api get method


The return format of html imap_fetchbody vs Gmail api GET method are different.

I write the emails to a file and then re-import into Thunderbird. With imap_fetchbody written emails are loaded into Thunderbird with no padding issues while the same email returned by the API has padding issue.

The most difference I see is 3D.

Here's a few lines from imap_fetchbody:

<html lang=3Den><head><meta content=3D"date=3Dno" name=3D"format-detection"=
><meta content=3D"email=3Dno" name=3D"format-detection"><style>.awl a {colo=
r: #FFFFFF; text-decoration: none;}.abml a {color: #000000; font-family: Ro=
boto-Medium,Helvetica,Arial,sans-serif; font-weight: bold; text-decoration:=
 none;}.adgl a {color: rgba(0, 0, 0, 0.87); text-decoration: none;}.afal a =
{color: #b0b0b0; text-decoration: none;}@media screen and (min-width: 600px=
) {.v2sp {padding: 6px 30px 0px;} .v2rsp {padding: 0px 10px;}}</style></hea=
d>

And a few lines from GMAIL api GET method:

<html lang=en><head><meta content="date=no" name="format-detection"><meta 
 content="email=no" name="format-detection"><style>.awl a {color: #FFFFFF; 
 text-decoration: none;}.abml a {color: #000000; font-family: Roboto-
 Medium,Helvetica,Arial,sans-serif; font-weight: bold; text-decoration: 
 none;}.adgl a {color: rgba(0, 0, 0, 0.87); text-decoration: none;}.afal a 
 {color: #b0b0b0; text-decoration: none;}@media screen and (min-width: 
 600px) {.v2sp {padding: 6px 30px 0px;} .v2rsp {padding: 0px 10px;}}</style> 
 </head>`

While Gmail api GET method returns base64 encode version which I converts the following way:

function decode($data)
{
    return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}

What really is difference between both and why really is the difference?

Screen shot of the mail fetched through gmail api

Screen shot of the mail fetched through imap_fetchbody

Your comments and answers will be appreicated.

Thanks


Solution

  • The difference between both is that the imap_fetchbody returns:

    It's quoted-printable text, probably the result of passing through a "dumb" mail client. = is used to encode/escape unprintable/metacharacters, much like &...; is used in HTML. =3d is the quoted-printable representation of an =: 3d hex = 61 ascii = equals sign.

    Reference: imap_fetchbody HTML with strange chars

    While Gmail api GET method returns base64_encoded string which needs to be decoded. We can add the same quoted-printable effect by just:

    quoted_printable_encode($string)