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�