Search code examples
phpemailimap

how can I display the contents of an email to a website?


I would like to display the body contents of an email. I have tried IMAP in php but something is VERY wrong. The IMAP isn't picking up the body of my message. It is picking up ONLY the signature in the body. So I am looking for alternative methods of reading email body contents to a webpage.

here is the original document of my email:

http://pastebin.com/WQra335P

the disclaimer/copyright blur is being grabbed by IMAP but nothing else in the body is being displayed. anyone have alternative methods of reading email from gmail or any other site that can display the contents to a webpage?

I have given up on making IMAP read it because no one has been able to figure out the problem...I have spent hours so I give up but here is the code...

<?php
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'username@gmail.com';
    $password = 'password';

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

    /* if emails are returned, cycle through each... */
    if($emails) {

        /* begin output var */
        $output = '';

        /* put the newest emails on top */
        rsort($emails);

        /* for every email... */
        foreach($emails as $email_number) {

            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox, $email_number, 0);
            $message = imap_fetchbody($inbox, $email_number, 2);
            echo $message;
            echo imap_qprint($message);
            $message = imap_qprint($message);
            echo imap_8bit ($message);
            $DateFormatted = str_replace("-0500", "", $overview[0] -> date);

            /* output the email header information */
            $output .=  $overview[0] -> subject ;
            $output .=  $DateFormatted ;

            //$bodyFormatted = preg_replace("/This e-mail(.*)/","",$message);
            //$bodyFormatted = preg_replace("/Ce courriel(.*)/","",$bodyFormatted);
            /* output the email body */
            $output .=  $message;

        }

    echo $output;

    }

    /* close the connection */
    imap_close($inbox);

?>

Solution

  • In Addition to what DmitryK suggested,

    Adding the below makes everything work fine without the random "=" signs. The Str_replace is used to remove the "="s generated over the pages.

    $message = imap_fetchbody($inbox, $email_number, "1.1"); 
    $message = str_replace("=", "", $message);
    

    I dont know 100% why the "="s are generated randomly but this is most likely due to some encryption issue from the Exchange Server's side as our server is about 10 years old.