2

I have send emails using SMTP gmail settings which is working fine and i fetched the emails using IMAP function of php and that also return listing of emails. Now i have list of emails with their uids. I want to send the reply to that received email. Below is my cakephp IMAP code :

public function listMessages($params=array()) {

    $page = (isset($params['page'])) ? $params['page'] : 1;
    $per_page = (isset($params['per_page'])) ? $params['per_page'] : 10; 
    $sort = (isset($params['sort'])) ? $params['sort'] : null;
    $sorted = null;
    $limit = ($per_page * $page);
    $start = ($limit - $per_page) + 1;
    $start = ($start < 1) ? 1 : $start;
    $limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
    $info = imap_check($this->imapStream);
    $limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;

    if(true === is_array($sort)) {
        $sorting = array(
                    'direction' => array(   'asc' => 0,
                                            'desc' => 1),

                    'by'        => array(   'date' => SORTDATE,
                                            'arrival' => SORTARRIVAL,
                                            'from' => SORTFROM,
                                            'subject' => SORTSUBJECT,
                                            'size' => SORTSIZE));
        $by = (true === is_int($by = $sorting['by'][$sort[0]]))
                        ? $by
                        : $sorting['by']['date'];
        $direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
                        ? $direction
                        : $sorting['direction']['desc'];

        $sorted = imap_sort($this->imapStream, $by, $direction);

        $msgs = array_chunk($sorted, $per_page);
        $msgs = $msgs[$page-1];
    }
    else
        $msgs = range($start, $limit); //just to keep it consistent

    $result = imap_fetch_overview($this->imapStream, implode($msgs, ','), 0);
    if(false === is_array($result)) return false;

    //sorting!
    if(true === is_array($sorted)) {
        $tmp_result = array();
        foreach($result as $r)
            $tmp_result[$r->msgno] = $r;

        $result = array();
        foreach($msgs as $msgno) {
            $result[] = $tmp_result[$msgno];
        }
    }

    $return = array('res' => $result,
                    'start' => $start,
                    'limit' => $limit,
                    'sorting' => array('by' => $sort[0], 'direction' => $sort[1]),
                    'total' => imap_num_msg($this->imapStream));
    $return['pages'] = ceil($return['total'] / $per_page);
    return $return;
}
Mr. AK
  • 89
  • 11
  • Is there something about this code that isn't working? Or were you hoping someone would write a function for you that adds functionality you need? – Greg Schmidt Apr 13 '18 at 18:18
  • What is your _question_? – Max Apr 13 '18 at 20:48
  • @GregSchmidt: Sorry for the late response, But i found the solution myself :) You can check the answer below. My query : **I wanted to send the reply to that received email** – Mr. AK Sep 16 '20 at 14:28
  • @Max: Sorry for the late response, My question was : **I wanted to send the reply to that received email** And fortunately, i found the solution myself :) You can check the answer below. – Mr. AK Sep 16 '20 at 14:28

1 Answers1

2

I have made this functionality works. Just by adding "Re:" infront of subject before sending email. Gmail automatically consider as reply and continue on the same thread instead showing it as new separate email.

Mr. AK
  • 89
  • 11
  • could you please post your result? – Demetrio Guilardi Mar 21 '20 at 17:52
  • @DemetrioGuilardi : Sorry for the late response, May i know, what results you would like to see ? As mentioned by using "Re:" infront of Subject of email, then GMAIL automatically consider we are sending reply to that subject's email we received. – Mr. AK Sep 16 '20 at 14:31
  • You should be manipulating In-Reply-To: headers for broad support and to meet the standards for threading. – Max Sep 16 '20 at 14:35
  • @Max : I would request you to please use code from my question and edit that and post as an answer. So that new users can use that method for broad support. Because i finished that project since 2 years :) But i understand your answer (Y) – Mr. AK Sep 16 '20 at 14:39