Tuesday, May 3, 2011

Gmail IMAP E-mail Content Garbled

I have a custom web based contact management system that we built in PHP to track contacts and recently starting checking our Google e-mail box using IMAP and then, if that contact is in our contact management system:

  1. Copying the message into a MySQL database table that's associated with that contact
  2. Marking that contact to follow up with that day
  3. Archiving the message in Gmail

Everything seems to be working great, EXCEPT... every so many emails we get a really garbled message that looks like this:

FABRRRQAUUUUAJXDjxZrUtzNFa2UMwjYj5YnYgZ74Ndwa4bwfzqmpH3/wDZjTcl CnKdr2Fa7SJP+Ek8S/8AQJX/AMB5P8aZN4s162j33GmxxrnG54XUfqa6ysHxp/yA/wDtqv8AWuej jFUqKDgtSpQsr3L13r4tPDcOoShBcTxgog6FiP5CsrwtpjuzavekvcTZKFuwPf8AH+VZOlwS+Iby 1jlBFnZRKhGeDjt9Sf0Fd0qhVCqAABgA

I go back and check the message and it appears to be only text, so I don't think it is an image. Any idea how to prevent that?

Thanks in advance.

Sincerely,

James

From stackoverflow
  • The example you provided looks like it is base64 encoded. The headers of the email message will tell you how to handle the content of the email message.

    For example, the following defines an email message where the body is plain text, but it is stored as being base64 encoded. I have "x"ed out the privacy sensitive information.

    Received: from xxxxxxxxx ([xxx.xx.xx.xxx]) by xxxxxxxxxx.xxx.xxxxxxxxxxxxxxx.xxx with Microsoft SMTPSVC(6.0.3790.3959);
         Wed, 29 Apr 2009 21:29:16 +0000
    Received: from xxxx-xxx-xxxxxx ([xxx.xx.xxx.xxxx]) by xxxxxxxx ; Wed, 29 Apr 2009 15:29:16
     -0600
    Message-ID: <AADB29A7-AAED-4068-B4A8-300E3B0D93AB@localhost>
    MIME-Version: 1.0
    From: xxxxxxxxxx@xxxxxxxxxxxxxxx.com
    To: xxxxxxxxxx@xxxxxxxxxxxxxxx.com
    Date: 29 Apr 2009 15:29:16 -0600
    Subject: xxxx Account Update
    Content-Type: text/html; charset=utf-8
    Content-Transfer-Encoding: base64
    Return-Path: xxxxxx@xxxxxxxx.com
    X-OriginalArrivalTime: 29 Apr 2009 21:29:16.0374 (UTC) FILETIME=[8C63AF60:01C9C911]
    

    Pay close attention to the Content-Type and Content-Transfer-Encoding headers.

  • I believe the IMAP is over SSL, so it might be the connection to IMAP that gets out of sync. The best solution I have for that is just check to see if the body contains a really long word. Since that garble has no spaces:

    <?php
    function wordlength($txt, $limit)
    {
       $words = explode(' ', $txt);
    
       foreach($words as $v)
       {
           if(strlen($v) > $limit)
           {
                return false;
           }
       }
    
       return true;
    }
    ?>
    

    Usage:

    <?php
    
    $txt = "Message Body would be here";
    
    if(!wordlength($txt, 45))
    {
        //maybe try to pull the message again or
        //send an email to you telling you there is a problem
    }
    
    ?>
    

    I picked 45 just in case some uses the word Pneumonoultramicroscopicsilicovolcanoconiosis in an email. :D

    Jordan might be right though. It may just be base64 encoded. I would just explode() the headers then and search for that and if it's there, a simple base64_decode() will do the trick.

    Alix Axel : +1 for that word

0 comments:

Post a Comment