0

Friends, I'd appreciate any insight into this vexing problem. I'm using PHP to scrap emails out of a Gmail account, using pretty standard code. It seems to work with text and HTML emails, but NEVER with any email with an attachment or embedded image. Worse yet, it works properly with the exact same code, run from the same platform, on a different Gmail account.

Here's an example of what it displays when I output the scraped $message of an email with an embedded image or attachment: --Sorry, Stackoverflow refuses to let me include images. But it's a pile of binary rendered as ASCII.

Here is my code:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'XXXX';
$password = 'XXXX';
$good = 0;
$max_emails = 25;
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to     Gmail: ' . imap_last_error());
$datelimit = date ( "d M Y", strToTime ( "-1 days" ) );
$emails = imap_search($inbox,"ALL");
if($emails) {
    $output = '';
    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $structure = imap_fetchstructure($inbox, $email_number);
        if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
            $part = $structure->parts[1];
            $message = imap_fetchbody($inbox,$email_number,2);

            if($part->encoding == 3) {
                $message = imap_base64($message);
            } else if($part->encoding == 1) {
                $message = imap_8bit($message);
            } else {
                $message = imap_qprint($message);
            }
        }    
        echo "Message: " . $message . "<br><br>";
...

I appreciate any insights as to what is missing that would cause it to fail to read an email. Thank you!

Here's what the gibberish output looks like:

�PNG IHDR��(/=�sRGB��� pHYs���+tEXtSoftwareMicrosoft Office�5qh\IDATx^��Uu��1���� ��ݭk��ݵcm]�s�\��cm�VT���a:��Ϲ�o��`�}��'0s߽�{~�s~'���F ˌ�(�v�Z[�(צN�n+W��.]:ZӦM�ެl^�r��6s���-[n��̵Y�f[^�R�<[�j�eUɲƍ‌​YŊ��d�DO�0��h�2S-L�u‌​��ڴi3l�����ʕ��+T�Z�k‌​�

Cameron Hurd
  • 4,836
  • 1
  • 22
  • 31
A Smith
  • 11
  • 2
  • 1
    The *gibberish* you see there is not *gibberish*, it's the actual binary content of the image that is attached to this mail. – Dekel Jan 05 '17 at 01:40
  • 1
    Note the 3 characters `PNG` its a bit of a clue – RiggsFolly Jan 05 '17 at 01:40
  • Dekel: Right, that's what I said in OP. RiggsFolly: Just now found link. – A Smith Jan 05 '17 at 01:45
  • Here is a link to a set of classes I made a wile back ( git hub ) https://github.com/ArtisticPhoenix/MISC/tree/master/IMAP to handle GMAIL, not sure if they will work for you but should give you some help... There are some dependencies ( mainly exception classes, not included ) so I'm providing them as an example not a working solution. – ArtisticPhoenix Jan 05 '17 at 02:44

0 Answers0