Question

I am using this code for display body content and attachment. Its working properly everything only body content is not display properly.

<?php 
ini_set('max_execution_time', 0); 
require_once('front_include/connect.inc.php');
$mail = new imap_mail();
$configuration_data = $mail->get_configuration();
    $hostname = $configuration_data['host_name'];
    $username = $configuration_data['user_name'];
    $password = $configuration_data['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,'UNSEEN');
    /* if emails are returned, cycle through each... */
    if($emails) {
          /* begin output var */
          $output = '';
          /* put the newest emails on top */
          rsort($emails);
          /* for every email... */
          $m=1;
          foreach($emails as $email_number) {
                /* output the email body */
                $message = imap_fetchbody($inbox,$email_number,2);

                //$output.= '<div class="body">'.utf8_encode(quoted_printable_decode($message)).'</div>';

                // start for detail
                $header = imap_header($inbox, $email_number);
                //print_r($header);
                $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;
                $email[$m]['fromaddress'] = $header->from[0]->personal;
                $email[$m]['to'] = $header->to[0]->mailbox;
                $email[$m]['subject'] = $header->subject.'</br>';
                $email[$m]['message_id'] = $header->Msgno;
                $email[$m]['date'] = $header->MailDate;             

                // Start for attachment
                $structure = imap_fetchstructure($inbox, $email_number);
                $message = imap_fetchbody($inbox,$email_number,2);
                echo $message.'First';

                $message2 = imap_fetchbody($inbox,$email_number,1.2);
                echo $message2.'Second';

                $attachments = array();
                if(isset($structure->parts) && count($structure->parts)) {

                if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
                    $part = $structure->parts[1];
                    $message1 = imap_fetchbody($inbox,$email_number,2);

                    if($part->encoding == 3) {
                        $message1 = imap_base64($message1);
                    } else if($part->encoding == 1) {
                        $message1 = imap_8bit($message1);
                    } else {
                        $message1 = imap_qprint($message1);
                    }
                    echo $message1.'Hello';
                }


                    for($i = 0; $i < count($structure->parts); $i++) {

                        $attachments[$i] = array(
                            'is_attachment' => false,
                            'filename' => '',
                            'name' => '',
                            'attachment' => ''
                        );

                        if($structure->parts[$i]->ifdparameters) {
                            foreach($structure->parts[$i]->dparameters as $object) {
                                if(strtolower($object->attribute) == 'filename') {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['filename'] = $object->value;
                                }
                            }
                        }

                        if($structure->parts[$i]->ifparameters) {
                            foreach($structure->parts[$i]->parameters as $object) {
                                if(strtolower($object->attribute) == 'name') {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['name'] = $object->value;
                                }
                            }
                        }

                        if($attachments[$i]['is_attachment']) {
                            $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
                            if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                            }
                            elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                            }
                        }
                    }

                }
                /********* insert data in databse ***********/
                $mail->mail_insert($email[$m]['from'],$email[$m]['fromaddress'],$email[$m]['to'],$email[$m]['subject'],$email[$m]['date'],$email[$m]['message_id'],$message);
                $mail_id = mysql_insert_id();
                /********* insert data in databse ***********/

                foreach ($attachments as $key => $attachment) {
                  if($attachment['name'] != '')
                  {
                    $name = $header->Msgno.date('d-m-y').time().$attachment['name'];
                    $display_name = $attachment['name'];
                    $contents = $attachment['attachment'];
                    $mail->attachment_insert($header->Msgno,$mail_id,$display_name,$name);
                    file_put_contents($name, $contents);
                  }
                }
            $m++;   
          }
    } 
    /* close the connection */
imap_close($inbox);
?>

Sometimes it display properly content using this code

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

But sometime its not working why this issue is creating. I tried three 3 types of display body content but no one of display body content properly. Can any one suggest me how i can display properly body content of message. Please help me...

Thanks in Advance.

Was it helpful?

Solution

    $message = imap_fetchbody($inbox,$email_number, 1.2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top