Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:50:56+00:00 2026-06-13T00:50:56+00:00

I have written/copied a script that reads emails from an inbox and updates a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:50:59+00:00Added an answer on June 13, 2026 at 12:50 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a script image.php that is quite long, but I've just copied
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written the html but the contact form is copied from a another
I have written a small batch script which copies files from the a folder
I have written a very simple bash script to help me migrate from dev
I have written a little script which retrieves pictures and movies from my camera
I have written a bash script which installs a number of packages, however for
I have written this code inside a servlet to delete certain records from three
I have written a function that sorts a big scale of data. To test
I have written a very simple CTE expression that retrieves a list of all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.