user1187274
user1187274

Reputation: 23

PHP Imap reading replies in inbox

I have written/copied a script that reads emails from an inbox and updates a ticket and then moves the email to a proccessed folder. This all works perfectly on new emails to the inbox but when someone replys to an email and it ends up in the inbox my scrtipt reads nothing on the email.

Is there something different to the way an email is structured when its a reply? I need a way of reading whats in the email so I can update a ticket with the contents. Knowing which email it is to update is all taken care of its just purely reading the content Im struggling with.

Here is the code

class Email 
{

    // imap server connection
    public $conn;

    // inbox storage and inbox message count
    public $inbox;
    private $msg_cnt;

    // email login credentials

        private $server = '????????????';

    private $user   = '????????';
    private $pass   = '?????????????';
    private $port   = ??;

    // connect to the server and get the inbox emails
    function __construct() 
        {
        $this->connect();
        $this->inbox();

        }

        function getdecodevalue($message,$coding) 
        {
            switch($coding) {
                    case 0:
                    case 1:
                            $message = imap_8bit($message);
                            break;
                    case 2:
                            $message = imap_binary($message);
                            break;
                    case 3:
                    case 5:
                            $message=imap_base64($message);
                            break;
                    case 4:
                            $message = imap_qprint($message);
                            break;
            }
            return $message;
        }

    // close the server connection
    function close() 
        {
        $this->inbox = array();
        $this->msg_cnt = 0;

        imap_close($this->conn);
    }

    // open the server connection
    // the imap_open function parameters will need to be changed for the particular server
    // these are laid out to connect to a Dreamhost IMAP server
    function connect() 
        {
        $this->conn = imap_open("{".$this->server.":".$this->port."/imap/novalidate-cert}INBOX", $this->user, $this->pass);

    }

    // move the message to a new folder
    function move($msg_index, $folder='Read') 
        {
        // move on server
            imap_mail_move($this->conn, $msg_index, $folder);


        // re-read the inbox
        //$this->inbox();
    }

    // get a specific message (1 = first email, 2 = second email, etc.)
    function get($msg_index=NULL) 
        {
        if(count($this->inbox) <= 0) 
    {
            return array();
        }
        elseif( ! is_null($msg_index) && isset($this->inbox[$msg_index])) 
    {
            return $this->inbox[$msg_index];
        }

        return $this->inbox[0];
    }

    // read the inbox
    function inbox() 
        {
        $this->msg_cnt = imap_num_msg($this->conn);

        $in = array();
        for($i = 1; $i <= $this->msg_cnt; $i++) 
    {
                $in[] = array(
                        'index'     => $i,
                        'header'    => imap_headerinfo($this->conn, $i),
                        'body'      => $this->cleanBody(imap_fetchbody($this->conn, $i,1)),
                        'structure' => imap_fetchstructure($this->conn, $i)
                    );
        }

        $this->inbox = $in;
    }


        function cleanBody($body) 
        {


            $delimiter = '#';
            $startTag = '----------START REPLY----------';
            $endTag = '----------END REPLY----------';
            $regex = $delimiter . preg_quote($startTag, $delimiter) 
                                . '(.*?)' 
                                . preg_quote($endTag, $delimiter) 
                                . $delimiter 
                                . 's';
            preg_match($regex,$body,$matches);

            $ret = trim($matches[1]);

            return $ret;
    }

}




$emails = new Email();



$email = array();
$emailCount = 1;
foreach($emails->inbox as $ems => $em)
{

            $email[$emailCount]['subject'] = $sub = $em['header']->subject;

            //echo $sub;

            $subParts = explode('-',$sub);

            $ticketUniqueCode = trim($subParts[1]);

            $sql = "SELECT * FROM ticket_main WHERE uniquecode = '".mysql_escape_string($ticketUniqueCode)."' LIMIT 1";

            $query = mysql_query($sql);

            if(mysql_num_rows($query))
            {
                $res = mysql_fetch_object($query);

                $ticketBody = $em['body'];
                $customerID = $res->customerID;


                $sql2 = "INSERT INTO ticket_updates SET ticketID = '".$res->ticketID."' , submitted = NOW() , submittedBy = '".$res->customerID."' , message = '".mysql_escape_string($ticketBody)."' , inhouse = 0";
                $query = mysql_query($sql2);


                // attachment section
                $message_number = $em['index'];

                $attachments = array();
                if(isset($em['structure']->parts) && count($em['structure']->parts)) 
                {
                        //echo 'hi';
                        for($i = 0; $i < count($em['structure']->parts); $i++) 
                        {
                                $attachments[$i] = array(
                                        'is_attachment' => false,
                                        'filename' => '',
                                        'name' => '',
                                        'attachment' => ''
                                );

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

                                if($em['structure']->parts[$i]->ifparameters) {
                                        foreach($em['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($emails->conn, $message_number, $i+1);
                                        if($em['structure']->parts[$i]->encoding == 3) 
                                        { // 3 = BASE64
                                                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                                        }
                                        elseif($em['structure']->parts[$i]->encoding == 4) 
                                        { // 4 = QUOTED-PRINTABLE
                                                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                                        }
                                }

                                if(isset($em['structure']->parts[$i]->disposition) && $em['structure']->parts[$i]->disposition == "attachment") 
                                {
                                        $filename = $attachments[$i]['name'];
                                        $mege="";
                                        $data="";
                                        $mege = imap_fetchbody($emails->conn, $message_number, $i+1);
                                        $filename= $filename;
                                        $fp=fopen('???????????????'.$filename,"w");
                                        $data=$emails->getdecodevalue($mege,$em['structure']->parts[$i]->type);

                                        fputs($fp,$data);
                                        fclose($fp);
                                        $email[$emailCount]['attachment'] = $attachments;



                                }


                        }


                }

            }

            $emailCount++;

}
$emailNumbers = imap_search($emails->conn,'ALL');

if(!empty($emailNumbers))
{
foreach($emailNumbers as $a)
{
    $emails->move($a);
}
imap_expunge($emails->conn);
}
$emails->close();

Hope that makes some sense and someone can actually help.

Many many thanks in advance

Jon

Upvotes: 1

Views: 2963

Answers (1)

Rudu
Rudu

Reputation: 15892

Well the most obvious thing is your code assumes a message is always in part 1, your line:

'body'      => $this->cleanBody(imap_fetchbody($this->conn, $i,1)),

Means it's only looking at body 1. If it's a pure text email body 1 will be right, but if it's multipart/alternative (text & html) body 1 would be the base MIME message which tells you that there are sub bodies (body 1.1, 1.2) that actually contain the content.

Further if the reply includes embedded images, or includes the message it's replying to as an attachment then you could have even more bodies/locations.

So how do I find the body? (you ask)... well you can use imap_fetchstructure to learn about all the body parts, then search through it to find a piece with type=0 (text) and then download that body part. (The first found text should be the right one, but note there could be more than one text body type in an email).

Upvotes: 2

Related Questions