Search code examples
phpimap

imap_fetchbody not supported


its my first time using imap. . so im trying to retrieve email from gmail as plain text using imap, everything works fine, but i got problem when i try to retrieve direct email,

lets say i have [email protected] and [email protected] and both receiving same email from [email protected]

if i retrieve using imap directly from [email protected], the imap_fetchbody return "Your email client is not supported=" in my native language but if i forwarded the same email from [email protected] to [email protected] then retrive it on the first email, it works

here is my simplified code

$imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';

$username = '[email protected]';

$password = 'mypassword';

$inbox = imap_open($imapPath,$username,$password) or die('Cannot connect : ' . imap_last_error());

$emails = imap_search($inbox,'UNSEEN');

if($emails) 
{

   $output = '';

   rsort($emails);

   foreach($emails as $email_number) 
   {
      $message = imap_fetchbody($inbox,$email_number,1);

      echo $message."<br><hr>";
   }

}

else

{

echo "No unseen mail";

}

imap_close($inbox);

?>

i've tried changing imap_fetchbody from 1 to 0 and it works but its not the result i need. . 1.1 and 1.2 return empty content so is there a way so i can retrieve it directly? thanks advance , sorry if im not describing it clearly/poor english lol


Solution

  • i solved it by changing

    $message = imap_fetchbody($inbox,$email_number,1);
    

    to

    $message = strip_tags(quoted_printable_decode(imap_fetchbody($inbox,$email_number,2)));
    

    kinda dirty lol. . not sure why some email not supported to fetch as plaintext,but works if the email was forwarded. . i noticed the "Your email client not supported=" appear when i tried to echo the imap_fetchbody plaintext result

    solved by fetch it as html and then strip the html tags,maybe its not the appropriate method but i get the result i want using this